Hi everyone,
I am new in using the magicparser. I have an xml file of my own and i want to show random results from this file 1 or 2 or 3.
Is there a way i am able to reach this goal using magicparser. In my case i know how many feed items are there in my xml file. I have tried the following code which david have mentioned here 'http://www.magicparser.com/node/248'
<?php
// global array to hold all records
$records = array();
function myRecordHandler($record)
{
// add this record to the global array
global $records;
$records[] = $record;
}
// parse the feed to load all records
MagicParser_parse("feed.xml","myRecordHandler");
// use PHPs array_rand function to extract n random records
$random = array_rand($records,5);
foreach($random as $i)
{
$record = $records[$i];
// display $record here just as in myRecordHandler
}
?>
I modified it to this;
<?php
require("MagicParser.php");
// global array to hold all records
$records = array();
function myRecordHandler($record)
{
// add this record to the global array
global $records;
$records[] = $record;
}
// parse the feed to load all records
MagicParser_parse("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","myRecordHandler");
// use PHPs array_rand function to extract n random records
$random = array_rand($records,5);
foreach($random as $i)
{
$record = $records[$i];
// display $record here just as in myRecordHandler
}
?>
But it is giving me a blank page. I don't know about php programming, but i do understant a bit. Please let me know if anyone in this forum can write me the right code.
Thanks,
A Khan
Hi,
There isn't actually any output code in that snippet, so you would need to add code to display the record. For example:
<?php
require("MagicParser.php");
// global array to hold all records
$records = array();
function myRecordHandler($record)
{
// add this record to the global array
global $records;
$records[] = $record;
}
// parse the feed to load all records
MagicParser_parse("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","myRecordHandler");
// use PHPs array_rand function to extract n random records
$random = array_rand($records,5);
foreach($random as $i)
{
$record = $records[$i];
// display $record here just as in myRecordHandler
print "<h2>".$record["TITLE"]."</h2>";
print "<p>".$record["DESCRIPTION"]."</p>";
}
?>
Notice how at the point where I have added the print lines, you can use $record exactly as you would have done in the myRecordHandler() function. Simply change the number 5 in the following line for more or less random records...
$random = array_rand($records,5);
Hope this helps!
Cheers,
David.