Solved

LastRSS Limit to One Item

Posted on 2006-07-05
6
329 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now