driz Posted June 9, 2008 Share Posted June 9, 2008 I wrote this JS randomizer, and wanted to make it in PHP, any help?It needs to be as simple as the JS version. x <!-- var how_many_quotes = 6 var now = new Date() var sec = now.getSeconds() var quote = sec % how_many_quotes; quote +=1; if (quote==1){ title="One"; } if (quote==2){ title="Two"; } if (quote==3){ title="Three"; } if (quote==4){ title="Four"; } if (quote==5){ title="Five"; } if (quote==6){ title="Six"; } document.write('' + title + ' '); --> Link to comment Share on other sites More sharing options...
Ingolme Posted June 9, 2008 Share Posted June 9, 2008 Well, you know, Javascript can create a random number without the timer. And so can PHP. <?php$quotes = array();$quotes[0] = "One";$quotes[1] = "Two";$quotes[2] = "Three";$quotes[3] = "Four";$quotes[4] = "Five";$quotes[5] = "six";$N = array_rand($quotes);$title = $quotes[$N];?> Link to comment Share on other sites More sharing options...
Jesdisciple Posted June 9, 2008 Share Posted June 9, 2008 That could be further shortened to: $quotes = array("One", "Two", "Three", "Four", "Five", "Six");$title = $quotes[array_rand($quotes)]; And you could also get your $quotes from a database. Link to comment Share on other sites More sharing options...
Ingolme Posted June 9, 2008 Share Posted June 9, 2008 I'd have done it that way, but I figured it would be easier to read long quotes if each one were on its own line:...$quotes[0] = "Insert a long quote here";$quotes[1] = "Insert another long quote here";... Link to comment Share on other sites More sharing options...
Jesdisciple Posted June 9, 2008 Share Posted June 9, 2008 Oh, I see. Then I would do this (still preserving the efficiency and some neatness): $quotes = array( "One", "Two", "Three", "Four", "Five", "Six" ); Link to comment Share on other sites More sharing options...
midnite Posted June 10, 2008 Share Posted June 10, 2008 driz,FYI, Math.random() and var quote = Math.ceil (Math.random() * how_many_quotes); http://www.w3schools.com/jsref/jsref_obj_math.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.