?
Solved

LastRSS Limit to One Item

Posted on 2006-07-05
6
Medium Priority
?
349 Views
Last Modified: 2012-06-27
I'm using LastRSS (http://lastrss.webdot.cz/) to display RSS feeds from sites. The trouble is that some feeds contain long lists of links. I don't want to clutter a page with all of the links. Does anyone have a solution for limiting the display to just the first link, title and description?

I've been told that since lastRSS returns an associative array with RSS items, I can just select the first array index ( 0 ) ?

Here's the code I'm using:

1.      <?php
2.      // include lastRSS
3.      include "lastRSS.php";
4.      // Create lastRSS object
5.      $rss = new lastRSS;
6.      // Set cache dir and cache time limit (1200 seconds)
7.      // (don't forget to chmod cache dir to 777 to allow writing)
8.      $rss->cache_dir = './temp';
9.      $rss->cache_time = 1200;
10.      // Try to load and parse RSS file
11.      if ($rs = $rss->get('http://www.clickfire.com/viewpoints/blog/feed/')) {
12.          // Show last published articles (title, link, description)
13.          echo "<ul>\n";
14.          foreach($rs['items'] as $item) {
15.              echo "\t<li> <a title=\"$item[description]\" href=\"$item[link]\">".$item['title']."</a></li>\n";
16.              }
17.          echo "</ul>\n";
18.          }
19.      else {
20.          echo "Error: It's not possible to reach RSS file...\n";
21.      }
22.      ?>

Thanks.
0
Comment
Question by:emory7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:rhickmott
ID: 17048926
It returns an Associate Array ( Items ) with each individual news item. it returns a numbered array 'items' with each story in its own sub array beyond that. To output only the furst you will need to drop the loop and just print the first cell from the array.

<?php

// include lastRSS library
include './lastRSS.php';

// create lastRSS object
$rss = new lastRSS;

// setup transparent cache
$rss->cache_dir = './cache';
$rss->cache_time = 3600;       // 1 hour

// load some RSS file
if ($rs = $rss->get('http://www.clickfire.com/viewpoints/blog/feed/')) {
       
             // Dump the first artical
            echo "<ul>\n";
            echo "\t<li> <a title=\"$rs['items'][0][description]\" href=\"$rs['items'][0][link]\">".$rs['items'][0]['title']."</a></li>\n";
            echo "</ul>\n";

}



?>
0
 

Author Comment

by:emory7
ID: 17052881
Looks good but I get this error when I try:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17054240
Sorry my bad :)

Try

<?php

// include lastRSS library
include './lastRSS.php';

// create lastRSS object
$rss = new lastRSS;

// setup transparent cache
$rss->cache_dir = './cache';
$rss->cache_time = 3600;      // 1 hour

// load some RSS file
if ($rs = $rss->get('http://www.clickfire.com/viewpoints/blog/feed/')) {
       
             // Dump the first artical
            echo "<ul>\n";
            echo "\t<li> <a title=\"$rs[items][0][description]\" href=\"$rs[items][0][link]\">".$rs['items'][0]['title']."</a></li>\n";
            echo "</ul>\n";

}



?>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:emory7
ID: 17055709
The above prints out the anchor text of the link perfectly. However, the actual link and title attribute of the link are appending this:

Array[0][link]
0
 
LVL 13

Accepted Solution

by:
rhickmott earned 1200 total points
ID: 17056767
Arrays in Strings are a pain :)

<?php

// include lastRSS library
include './lastRSS.php';

// create lastRSS object
$rss = new lastRSS;

// setup transparent cache
$rss->cache_dir = './cache';
$rss->cache_time = 3600;      // 1 hour

// load some RSS file
if ($rs = $rss->get('http://www.clickfire.com/viewpoints/blog/feed/')) {

             // Dump the first artical
            echo "<ul>\n";
            echo "\t<li> <a title=\"{$rs['items'][0]['description']}\" href=\"{$rs['items'][0]['link']}\">".$rs['items'][0]['title']."</a></li>\n";
            echo "</ul>\n";

}


?>
0
 

Author Comment

by:emory7
ID: 17062763
Works great! Thanks so much.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question