VN:F [1.9.22_1171]
Rating: 9.3/10 (3 votes cast)

This script will show you how to read and display the values of an xml file using php. My demo.xml looks like this

<message><name>Antonio Paulo/name><website>http://mistonline.in</website><comment>Good One</comment><date>1305127910</date><user_ip>2.241.68.95</user_ip><user_agent>Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13</user_agent><spam>0</spam></name></message>

Here i am just reading the Name and the Comment TAGS.

< ?php
$xml = new SimpleXMLElement('demo.xml', NULL, TRUE);
$testi = $xml->children();
if(count($testi) == 0 )
{
echo "No Entries Yet";
}
else
{
srand(time());
$random = (rand()%count($testi));

for ($i = 0; $i < count($testi); $i++)
{
if ($i == $random)
{
echo "<table><tr><td><img src='img/ico/user-ico-2.png'/></td><td> <b>".$testi[$i]->name . '</b> says <i>' .	 $testi[$i]->comment . '</i></td></tr>';
}
}
}

?>
VN:F [1.9.22_1171]
Rating: 9.3/10 (3 votes cast)
2 thoughts on “Randomly read and display values of an xml file using php”
  1. Thank you for all.
    That’s really help. I was trying to do it alone but don’t work for me.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Your email address will not be published. Required fields are marked *