0

Hi all,

I have quite a few testimonials I would like to rotate. Each testimonial is just a block of text. As it stands, all are displayed on every page. I want only one to be displayed.

Currently:
I have about 20 pages that all have an include of "testimonials.php". Within this file, is all of the testimonials, in basic HTML.

The goal:
I would like to, in PHP, display a random testimonial on each page. I'd prefer to keep all content within testimonials.php.

The thought:
From my limited PHP experience, I was thinking I could assign each testimonial a $variable. ($test1="I love you...") and then randomly display each variable somehow. Possibly assign each variable a number, and use rand() to call up a random number.

Any thoughts / help?

Thanks,
Danny

3
Contributors
6
Replies
7
Views
12 Years
Discussion Span
Last Post by Danny
0

Put each testimonial into an array eg

<?PHP
$testimonials = array();
$testimonials[] = 'I love you';
$testimonials[] = 'You did a great job';
$testimonials[] = 'You did an ok job';
$testimonials[] = 'I would hire you again';
//then get the random number
$rand = rand(0, count($testimonials));
// and echo out the corresponding testimonial
echo $testimonials[$rand];
?>

Ref: http://www.php.net/rand

If you decide to store the testimonials text in a MySQL database the method of getting the random row is totally different so let us know if you decide to go down that path.

Sarah

0

depending how how big your testimonials are, you could store them as variables and use a switch function .

$var = rand(1,5);
switch($var) {
 	case 1: echo 'Testimonial 1...';  break;
	case 2: echo 'Testimonial 2...';  break;
	case 3: echo 'Testimonial 3...';  break;
	case 4: echo 'Testimonial 4...';  break;
	case 5: echo 'Testimonial 5...';  break;
	}
0

Thanks for the replies. My testimonials are small paragraphs. (3 or four sentences)

Will the array method mentioned still work? Or is there a size limit?

If I did go with robbyd's code due to the size of the testimonials, would I simply echo $var?

0

Actually you would echo the text that you wanted to display

$var = rand(1,3);
switch($var) {
    case 1: echo 'It was very good service';  break;
    case 2: echo 'I loved it';  break;
    case 3: echo 'Everything was great';  break;
    }

Or if the testitmonials are larger you could put each into a text file and include them...

$var = rand(1,3);
switch($var) {
    case 1: include('testimonial1.txt');  break;
    case 2: include('testimonial2.txt');  break;
    case 3: include('testimonial3.txt');  break;
    }
0

Ah it makes sense. I'm going to try these two solutions out and see how well they work.

Thanks so much for all of your help.

0

Well I ended up going with the array. The only problem is, every 4-5 times it displays nothing (its just blank)

Is there a reason for this?

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.