How to display only 10 results with this PHP RSS script.
03-13-2008, 09:58 PM
|
How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
I have this script on my site. It used to display only ten results then all of a sudden it started displaying 50+ I'm not sure why. (no changes were made to the site) I have this same script running a few inches below and its still displaying only 10 results. Can anyone figure out why this is?
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false; $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title)); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($descrip tion,0,100)))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc; if ($insideitem) { switch ($tag) { case "TITLE": if($currentlyinli == false){ $currentlyinli = true; $title .= "<li>"; } $title .= $data; break; case "DESCRIPTION": if($currentlyinli == true){ $description .= ""; $currentlyindesc = true; $currentlyinli = false; } $description .= $data; break; case "LINK": $currentlyindesc = false; $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("RSS URL GOES HERE","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
Last edited by collegec; 03-13-2008 at 11:40 PM..
|
|
|
|
03-13-2008, 10:52 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
The only thing I can see is that the RSS url happen to return a larger RSS feed, it would display more. There is no other element in your code to restrict results output other than the feed itself.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
03-13-2008, 11:01 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Any suggestions on a piece of a code I could add in order to limit it?
|
|
|
|
03-13-2008, 11:10 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
That really depends upon the structure of the RSS result feed.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
03-13-2008, 11:14 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by mgraphic
That really depends upon the structure of the RSS result feed.
|
hmmm, i'm not sure I follow.
|
|
|
|
03-13-2008, 11:28 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
I think what mgraphic means is, are you just spitting out the parsed xml as you go or are you appending it into a big string then doing the output all at once. At a glance it seems like you are just spitting it out as you go. What I recommend you do instead is build an array of strings. So each time endElement gets called instead of just using printf to display the result, just add it to an array. This way you can just output however many items in the array as you want.
By the way this code is in serious need of some spacing and indenting... its difficult to read. If I hadn't worked with rss feeds before I don't think I would have been able to figure out what it does without reformatting it myself.
Hope that helps 
|
|
|
|
03-13-2008, 11:43 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by NullPointer
I think what mgraphic means is, are you just spitting out the parsed xml as you go or are you appending it into a big string then doing the output all at once. At a glance it seems like you are just spitting it out as you go. What I recommend you do instead is build an array of strings. So each time endElement gets called instead of just using printf to display the result, just add it to an array. This way you can just output however many items in the array as you want.
By the way this code is in serious need of some spacing and indenting... its difficult to read. If I hadn't worked with rss feeds before I don't think I would have been able to figure out what it does without reformatting it myself.
Hope that helps 
|
Sorry, I used a quote instead of the PHP tags to display it. I changed the first post so hopefully it is easier to read.
If I was going to add the array string is that difficult or will I need to start over? I'm not very good with PHP.
|
|
|
|
03-13-2008, 11:50 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
it would be pretty simple... just initialize an array and then instead of using printf to spit out your string just add it as a new element in the array.
Then depending on how many tags that exist in a single rss report, just spit out 10 times that many array elements. So if a report has 3 tags that you are parsing, just output 30 elements... sorry if I'm confusing you I'm on my way out the door... later I'll explain what I mean better and write up some code.
|
|
|
|
03-14-2008, 08:21 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by NullPointer
later I'll explain what I mean better and write up some code.
|
Thanks, that would be very helpful.
I did some research, but I'm pretty lost.
|
|
|
|
03-14-2008, 09:43 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
Ok sorry about my obscure answer before, I'll give it another go.
Right now the function endElement is handling all of your output, so most of your modifications will occur within this function. All you need to do is rather than output your data as you go, store it into an array and then output only what you want at the end.
First you will need to initialize an array and modify your endElement function to insert data into that array:
PHP Code:
//initialize an array
$data = array();
//Notice how much easier this is to read once I add tabs
function endElement($parser, $name)
{
//add that array to your globals
global $insideitem, $tag, $title, $description, $link, $data;
if ($name == "ITEM")
{
//Rather than using printf I use sprintf which returns a formatted string rather
//than outputing one.
$data[] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title));
$data[] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ;
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
$data should contain all of your output now. Note that for every time endElement is called, two elements are added to $data.
PHP Code:
//this function will handle the output
function doOutput($numberOfElements)
{
global $data;
for($i = 0; $i < (2 * $numberOfElements); $i++)
{
echo $data[$i];
}
}
Hope that helps. I didn't test this code so it might be a bit buggy.
Last edited by NullPointer; 03-14-2008 at 09:52 PM..
|
|
|
|
03-14-2008, 11:02 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Thanks for the in depth reply. I added those lines to the RSS, but its giving me an error.
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false; $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } $data = array(); function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link, $data; if ($name == "ITEM") { $data[] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title)); $data[] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function doOutput($numberOfElements) { global $data; for($i = 0; $i < (2 * $numberOfElements); $i++) { echo $data[$i]; } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://feeds.collegepublisher.com/index.cfm?&zip=&uglobal_section_id=&poolid=","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
|
|
|
|
03-15-2008, 12:18 AM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
What's the error?
|
|
|
|
03-15-2008, 02:22 AM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
For each individual item (50+ times) I get this error:
Warning: xml_parse() [ function.xml-parse]: Unable to call handler characterData() in /home/collegec/public_html/testmain233.php on line 167
Then at the very end of the feed I get this:
Fatal error: [] operator not supported for strings in /home/collegec/public_html/testmain233.php on line 145
|
|
|
|
03-15-2008, 02:40 AM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
the warning you get is because you you left out the characterData() function in your code. Its in the origional version you posted but not in the version you posted last. Fix that and see if you still get the error on line 145. I'm not sure if building an array without specifying an index is legal in this context, though I've never had trouble with it before. What version of PHP are you using?
|
|
|
|
03-15-2008, 12:42 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
I'm running PHP5. I added the "characterdata" function, however now its giving me another error:
Parse error: syntax error, unexpected $end in /home/collegec/public_html/testmain233.php on line 253
However I have no code on line 253. (its the end of the page)
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false; $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } $data = array(); function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link, $data; if ($name == "ITEM") { $data[] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title)); $data[] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc; if ($insideitem) { switch ($tag) { case "TITLE": if($currentlyinli == false){ $currentlyinli = true; $title .= "<li>"; } $title .= $data; break; case "DESCRIPTION": if($currentlyinli == true){ $description .= ""; $currentlyindesc = true; $currentlyinli = false; } $description .= $data; break; case "LINK": $currentlyindesc = false; $link .= $data; break; } function doOutput($numberOfElements) { global $data; for($i = 0; $i < (2 * $numberOfElements); $i++) { echo $data[$i]; } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://feeds.collegepublisher.com/index.cfm?&zip=&uglobal_section_id=&poolid=","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
|
|
|
|
03-15-2008, 03:18 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
The while loop at the bottom of your code is missing brackets.
PHP Code:
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
The error become much more apparent when I use proper formatting. Code with no indentations or spacing is a mess, especially when it comes to debugging.
Last edited by NullPointer; 03-15-2008 at 03:19 PM..
|
|
|
|
03-15-2008, 05:38 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
I added the updated code with the brackets, however I'm still getting the syntax error on line 253. Is it possible I'm missing an end bracket?
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false; $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } $data = array(); function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link, $data; if ($name == "ITEM") { $data[] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title)); $data[] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc; if ($insideitem) { switch ($tag) { case "TITLE": if($currentlyinli == false){ $currentlyinli = true; $title .= "<li>"; } $title .= $data; break; case "DESCRIPTION": if($currentlyinli == true){ $description .= ""; $currentlyindesc = true; $currentlyinli = false; } $description .= $data; break; case "LINK": $currentlyindesc = false; $link .= $data; break; } function doOutput($numberOfElements) { global $data; for($i = 0; $i < (2 * $numberOfElements); $i++) { echo $data[$i]; } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://feeds.collegepublisher.com/index.cfm?&zip=&uglobal_section_id=&poolid=","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) { xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } fclose($fp); xml_parser_free($xml_parser); ?>
|
|
|
|
03-15-2008, 05:55 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
I reformatted your code... you were missing two more brackets.
PHP Code:
<?php
//vars
$currentlyinli = false;
$currentlyindesc = false;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$data = array();
//functions
function startElement($parser, $name, $attrs)
{
global $insideitem, $tag, $title, $description, $link;
if ($insideitem)
{
$tag = $name;
}
else if ($name == "ITEM")
{
$insideitem = true;
}
}
function endElement($parser, $name)
{
global $insideitem, $tag, $title, $description, $link, $data;
if ($name == "ITEM")
{
$data[] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title));
$data[] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ;
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data)
{
global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc;
if($insideitem)
{
switch ($tag)
{
case "TITLE":
if($currentlyinli == false)
{
$currentlyinli = true;
$title .= "<li>";
}
$title .= $data;
break;
case "DESCRIPTION":
if($currentlyinli == true)
{
$description .= "";
$currentlyindesc = true;
$currentlyinli = false;
}
$description .= $data;
break;
case "LINK":
$currentlyindesc = false;
$link .= $data;
break;
}
}
}
function doOutput($numberOfElements)
{
global $data;
for($i = 0; $i < (2 * $numberOfElements); $i++)
{
echo $data[$i];
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://feeds.collegepublisher.com/index.cfm?&zip=&uglobal_section_id=&poolid=","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
doOuput(10);
?>
|
|
|
|
03-15-2008, 07:49 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by NullPointer
I reformatted your code... you were missing two more brackets.
|
The page loaded with that code, but it produced this error where the RSS feed would display.
Fatal error: [] operator not supported for strings in /home/collegec/public_html/testmain233.php on line 153
|
|
|
|
03-15-2008, 09:48 PM
|
Re: How to display only 10 results with this PHP RSS script.
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
Try this version:
PHP Code:
<?php
//vars
$currentlyinli = false;
$currentlyindesc = false;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$data = array();
$counter = 0;
//functions
function startElement($parser, $name, $attrs)
{
global $insideitem, $tag, $title, $description, $link;
if ($insideitem)
{
$tag = $name;
}
else if ($name == "ITEM")
{
$insideitem = true;
}
}
function endElement($parser, $name)
{
global $insideitem, $tag, $title, $description, $link, $data, $counter;
if ($name == "ITEM")
{
$data[$counter] = sprintf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title));
$data[$counter + 1] = sprintf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,100)))) ;
$counter += 2;
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data)
{
global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc;
if($insideitem)
{
switch ($tag)
{
case "TITLE":
if($currentlyinli == false)
{
$currentlyinli = true;
$title .= "<li>";
}
$title .= $data;
break;
case "DESCRIPTION":
if($currentlyinli == true)
{
$description .= "";
$currentlyindesc = true;
$currentlyinli = false;
}
$description .= $data;
break;
case "LINK":
$currentlyindesc = false;
$link .= $data;
break;
}
}
}
function doOutput($numberOfElements)
{
global $data;
for($i = 0; $i < (2 * $numberOfElements); $i++)
{
echo $data[$i];
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://feeds.collegepublisher.com/index.cfm?&zip=&uglobal_section_id=&poolid=","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
doOuput(10);
?>
|
|
|
|
|
« Reply to How to display only 10 results with this PHP RSS script.
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|