PHP 5 and XML
Oct. 25th, 2006 08:32 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
On the site I'm working on for gneech.com, I'm going to have a box where I load a random quote from
the_gneech's writing. I'm thinking of having the quotes in an XML file, generating a random number, then loading the quote from the XML file that has an ID that matches the random number.
This is the first bit of functionality I'm trying to write in PHP. This is what I have so far:
<?php
// generate a random number
$random_number = mt_rand(1, 3);
// set name of XML file
$file = "quotes.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// pull the quote that matches the random number
Edit: I think this might work.$quote = $xml->quote[$random_number]
//print the quote
print "$quote\n";
?>
The code I haven't come up with yet is the part where I say, "Give me the quote with the ID that matches the random number I generated earlier."
Anyone know how to do this? Anyone know if this is even possible?
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
This is the first bit of functionality I'm trying to write in PHP. This is what I have so far:
<?php
// generate a random number
$random_number = mt_rand(1, 3);
// set name of XML file
$file = "quotes.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// pull the quote that matches the random number
Edit: I think this might work.$quote = $xml->quote[$random_number]
//print the quote
print "$quote\n";
?>
The code I haven't come up with yet is the part where I say, "Give me the quote with the ID that matches the random number I generated earlier."
Anyone know how to do this? Anyone know if this is even possible?
no subject
Date: 2006-10-28 04:00 pm (UTC)no subject
Date: 2006-10-28 07:19 pm (UTC)