Solved

LastRSS Limit to One Item

Posted on 2006-07-05
6
323 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now