944,218 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5330
  • PHP RSS
Apr 29th, 2005
0

Roating testimonials (text) in PHP

Expand Post »
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
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
Danny is offline Offline
193 posts
since May 2004
Apr 30th, 2005
0

Re: Roating testimonials (text) in PHP

Put each testimonial into an array eg

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Junior Poster
sarahk is offline Offline
138 posts
since Apr 2005
Apr 30th, 2005
0

Re: Roating testimonials (text) in PHP

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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
robbyd is offline Offline
12 posts
since Apr 2005
Apr 30th, 2005
0

Re: Roating testimonials (text) in PHP

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?
Reputation Points: 8
Solved Threads: 0
Junior Poster
Danny is offline Offline
193 posts
since May 2004
Apr 30th, 2005
0

Re: Roating testimonials (text) in PHP

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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
robbyd is offline Offline
12 posts
since Apr 2005
Apr 30th, 2005
0

Re: Roating testimonials (text) in PHP

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.
Reputation Points: 8
Solved Threads: 0
Junior Poster
Danny is offline Offline
193 posts
since May 2004
May 1st, 2005
0

Re: Roating testimonials (text) in PHP

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
Reputation Points: 8
Solved Threads: 0
Junior Poster
Danny is offline Offline
193 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Netbpm
Next Thread in PHP Forum Timeline: include() and require()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC