Help - Search - Members - Calendar
Full Version: Looking for PHP Script
HTMLHelp Forums > Programming > Server-side Scripting
Josh Thomas
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
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
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
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.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.