Solved

LastRSS Limit to One Item

Posted on 2006-07-05
6
335 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking CSRF token within a function 36 76
preventing spam with mailto 22 36
How would I do a website like this? 5 60
How can I make this form submit to itself? 10 28
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

821 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