Looking for PHP Script, Serve "random" content |
Looking for PHP Script, Serve "random" content |
Josh Thomas |
Sep 19 2008, 01:23 PM
Post
#1
|
Member Group: Members Posts: 59 Joined: 13-November 07 Member No.: 4,306 |
I would like to write a php script to pull strings randomly from a text file to serve to the visitor. I currently have client testimonials hard-coded into my html (see the right-side on www.foxhilldesign.com). I would like to build a text file so I can add more testimonies and have them displayed randomly without repeating. I have the logic pretty much worked out, but am not familiar enough with php to know how to code this or how best to set up the text file. Can anybody help or point me to a script that has already been written to do this?
|
Josh Thomas |
Sep 19 2008, 01:45 PM
Post
#2
|
Member Group: Members Posts: 59 Joined: 13-November 07 Member No.: 4,306 |
I have the following script that serves one random testimony. How can I send a variable to it to determine how many testimonies to serve?
$quote[] = 'blah blah blah html code'; $quote[] = 'blah blah blah more html code'; $quote[] = 'blah blah blah some other html code'; srand ((double) microtime() * 1000000); $random_number = rand(0,count($quote)-1); echo ($quote[$random_number]); |
Darin McGrew |
Sep 19 2008, 03:11 PM
Post
#3
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
Well, the simple approach would be to repeat the line
CODE echo ($quote[$random_number]); once for each quote that you want to appear. However, that doesn't guarantee that the quotes will be different. To do that, you'll need a function that picks n random numbers, and guarantees that all the random numbers are different from each other. Then you can call $quote[] with each of the selected random numbers.If n is greater than or equal to the number of quotes, then you could just print out all the quotes. |
Josh Thomas |
Sep 19 2008, 10:24 PM
Post
#4
|
Member Group: Members Posts: 59 Joined: 13-November 07 Member No.: 4,306 |
Thanks for your help, Darin. I posted here because you and Pandy are always so fast and so helpful. I actually posted on a php help forum also, and someone there actually beat you guys to the punch this time, and I was able to get a script up and running.
So far I am not sending a value to the script to determine the number of quotes to display, but I will work on that later. |
Lo-Fi Version | Time is now: 17th April 2024 - 01:51 PM |