Solved

LastRSS Limit to One Item

Posted on 2006-07-05
6
339 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
Industry Leaders: 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

733 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