| | |
Roating testimonials (text) in PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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"
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
Put each testimonial into an array eg
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
PHP Syntax (Toggle Plain Text)
<?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
•
•
Join Date: Apr 2005
Posts: 12
Reputation:
Solved Threads: 0
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]
[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]
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?
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"
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
•
•
Join Date: Apr 2005
Posts: 12
Reputation:
Solved Threads: 0
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]
[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]
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.
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"
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
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
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"
NEW: Press Advance - Geek Valley - Freedom Tower
"Longer Boats are coming to win us"
![]() |
Similar Threads
- Seacrch text using php (PHP)
- Text formatting With PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: Netbpm
- Next Thread: include() and require()
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error fcc file files folder form forms freelancing function functions google href htaccess html image include incode insert integration ip java javascript joomla limit link login loop mail menu method mlm mod_rewrite multiple mysql oop pageing pagerank parse paypal pdf php printer problem query radio random recursion recursiveloop regex remote script search server sessions sms soap source space sql structure support! syntax system table template tutorial update upload url validation validator variable video web xml youtube





