I am trying to make a new releases lister, which can be seen here http://koti.mbnet.fi/nanime/uudetsivut/index4.php on the right side of the page. It is working fine, in fact too fine. I want it only to return 5 first matches, but the code I made returns all the matches it gets from the RSS file that it is parsed from.
Here is the code:
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$out = "";
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,$out;
// Lets find all the data from between %5BNanime%5D_ and _-_ from the description
preg_match_all("/\%5BNanime\%5D\_(.*)\_\-\_/i", $description, $thumb, PREG_SET_ORDER);
// Now we place the data here to get a file path
$nail = 'img/' . 'releases/' . $thumb[0][1] . '.jpg';
// Is there a file with that name and path? If there is lets save it to the picture variable.
if (file_exists($nail)) {
$picture = '<img src="' . $nail . '" width="120" height="70" border="0">';
} else {print '';}
if ($name == "ITEM") {
// Now we get the file size and the release name from the RSS feed
preg_match_all("/\>.\((.*?)iB\)/i", $description, $out, PREG_SET_ORDER);
preg_match_all("/\[Nanime\].(.*)(.Finnish|.\()/i", $title, $linktitle, PREG_SET_ORDER);
// Then finally we print all of it in the table
printf("<tr><td>%s</td></tr><tr><td class=\"style6\" width=\"620\"><div align=\"left\"><a class=\"release\" href='%s'>%s</a> (%sb)</div><br></td></tr>",
$picture,trim($link),htmlspecialchars(trim($linktitle[0][1])),trim($out[0][1]
));
$title = "";
$description = "";
$link = "";
$insideitem = false;
$out = "";
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$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("http://nordic-anime.edwardk.info/?rss","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);
?>
What should I do ? Thank you very much for any assistance 🙂