Roating testimonials (text) in PHP

Reply

Join Date: May 2004
Posts: 193
Reputation: Danny is an unknown quantity at this point 
Solved Threads: 0
Danny's Avatar
Danny Danny is offline Offline
The Press Release Guy

Roating testimonials (text) in PHP

 
0
  #1
Apr 29th, 2005
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
:cool: The Press Release Guy :(
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: Roating testimonials (text) in PHP

 
0
  #2
Apr 30th, 2005
Put each testimonial into an array eg

  1. <?PHP
  2. $testimonials = array();
  3. $testimonials[] = 'I love you';
  4. $testimonials[] = 'You did a great job';
  5. $testimonials[] = 'You did an ok job';
  6. $testimonials[] = 'I would hire you again';
  7. //then get the random number
  8. $rand = rand(0, count($testimonials));
  9. // and echo out the corresponding testimonial
  10. echo $testimonials[$rand];
  11. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 12
Reputation: robbyd is an unknown quantity at this point 
Solved Threads: 0
robbyd robbyd is offline Offline
Newbie Poster

Re: Roating testimonials (text) in PHP

 
0
  #3
Apr 30th, 2005
depending how how big your testimonials are, you could store them as variables and use a switch function .

[PHP]
$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;
}
[/PHP]
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 193
Reputation: Danny is an unknown quantity at this point 
Solved Threads: 0
Danny's Avatar
Danny Danny is offline Offline
The Press Release Guy

Re: Roating testimonials (text) in PHP

 
0
  #4
Apr 30th, 2005
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?
:cool: The Press Release Guy :(
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 12
Reputation: robbyd is an unknown quantity at this point 
Solved Threads: 0
robbyd robbyd is offline Offline
Newbie Poster

Re: Roating testimonials (text) in PHP

 
0
  #5
Apr 30th, 2005
Actually you would echo the text that you wanted to display

[PHP]
$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;
}
[/PHP]

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

[PHP]
$var = rand(1,3);
switch($var) {
case 1: include('testimonial1.txt'); break;
case 2: include('testimonial2.txt'); break;
case 3: include('testimonial3.txt'); break;
}
[/PHP]
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 193
Reputation: Danny is an unknown quantity at this point 
Solved Threads: 0
Danny's Avatar
Danny Danny is offline Offline
The Press Release Guy

Re: Roating testimonials (text) in PHP

 
0
  #6
Apr 30th, 2005
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.
:cool: The Press Release Guy :(
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 193
Reputation: Danny is an unknown quantity at this point 
Solved Threads: 0
Danny's Avatar
Danny Danny is offline Offline
The Press Release Guy

Re: Roating testimonials (text) in PHP

 
0
  #7
May 1st, 2005
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
:cool: The Press Release Guy :(
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC