Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Create PHP Testimonials

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Intermediate Networking, Programming and Graphics - Tutorials Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2009-Mar-05
Networking, Programming and Graphics - Tutorials
Visited: 3997 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

Many websites have special section for displaying Testimonials and other greetings from visitors, users, clients, partners, etc . This will show on new visitors how useful an service or product and what benefit visitors can get when using it.

With this tutorial I will show you how to create simple PHP Testimonials for your website.
Main concept for displaying testimonials is to place all testimonial items in some database, array or file. Next with some random function to get one item and to show it in website.

First we have to prepare all testimonials to be well organized for fast and easy extraction. For this case we will use simple PHP Array
$testimonials=array('Thanks. Have a Nice Day!', 'Very useful Website', 'Greetings from NY', 'Go Yonkees! Go');
Now we have to prepare function for getting random testimonial from the array. This function will use our already prepared array with some testimonials and using rand() function will select one testimonial and return it for later displaying. This function have to be positioned between <?php and ?> tags.
function GetRandomTestimonials() {
    global $testimonials;
    $TestimonialsQty = count($testimonials);
    $randomPos = rand(0,($TestimonialsQty-1));
    return $testimonials[$randomPos];
}
Displaying the result is quite easy with echo function. This have to be positioned between <?php and ?> tags and on suitable place on the website.
echo GetRandomTestimonials();
Now we will place array with testimonials, function for getting random testimonial and exact displaying of testimonials in separate <?php ... ?> section and all of this in one simple html file to test the entire functionality. Copy all code showed below to an php file. When you ready your php file have to look like this:
<html>
<head>
<title>Displaying PHP Testimonials</title>
<?php
$testimonials
=array('Thanks. Have a Nice Day!''Very useful Website''Greetings from NY''Go Yonkees! Go');

function 
GetRandomTestimonials() {
    global 
$testimonials;
    
$TestimonialsQty count($testimonials);
    
$randomPos rand(0,($TestimonialsQty-1));
    return 
$testimonials[$randomPos];
}
?>
</head>
<body>
<?php
echo GetRandomTestimonials();
?>
</script>
</body>
</html>
Now lets check how all of this will work. Press F5 (Refresh) several times to get random testimonials each time when this page is load.
Displaying Java Script Testimonials
Rate this tutorial:                    
Post Comment

    • ( aa [ at ] gmail . com ),

      posted on 2009-Aug-03 | 10:49:48 AM
      just trying...
    • ( g00rmed [ at ] hotmail . com ),

      posted on 2009-Oct-15 | 12:12:32 PM
      Thank you for this nice script. I have just one issue with it.
      If I set it up and I try to refresh the page, at some point one of the testimonials appears blank.

      Please help me to solve this.

    • posted on 2009-Oct-15 | 10:59:43 PM
      Hm, strange. Check for empty element (single testimonial) in the array.

    • posted on 2009-Oct-16 | 01:51:29 AM
      Yes I tried to debug the testimonial script, and I think it happens when rand takes zero value.

    • posted on 2009-Oct-16 | 07:11:40 AM
      You are right. Yes. I found a bug in the function. Now the function is corrected and have to work fine.

      Thanks for the note.
    • ( sddsd [ at ] lkdsfl . com ),

      posted on 2009-Nov-03 | 11:02:54 AM
      wahoo!
    • ( yogesh . sanger [ at ] gmail . com ),

      posted on 2009-Nov-23 | 06:00:03 AM
      hello yogesh
    • ( luvvips [ at ] gmail . com ),

      posted on 2010-Jan-19 | 11:43:59 PM
      Hey! g8 stuff, what i was looking for. I would like to ask you, I have 500 testimonials in my database and I want to show 10 testimonials at one page and rest make pages itself and every time when page refreshes all testimonials refresh too. Can we do that?
    • ( luvvips [ at ] gmail . com ),

      posted on 2010-Jan-19 | 11:44:54 PM
      How can we refresh number of testimonials and show 10 testimonials at each page?

    • posted on 2010-Jan-20 | 01:37:31 AM
      Hi Vipul Sharma,

      We work on tutorial based on your case and very soon you can see it ready and working. Now I will tell you only that, the solution will be AJAX based ;-). Please check our website after one-two weeks.

      Best regards,

    • posted on 2010-Feb-01 | 06:10:14 AM
      Hi again Vipul,

      You can check http://www.onlinehowto.net/Tutorials/AJAX/Paged-Result-Query-with-AJAX-using-PHP-MySQL/1357

      I think there you can find a solution for your case
    • ( info [ at ] pitteamsolution . info ),

      posted on 2010-Feb-08 | 07:25:56 AM
      I will try it on my website...

      Thanks
    • ( mani87 . ibs [ at ] gmail . com ),

      posted on 2010-Mar-23 | 01:19:00 AM
      testimonal code detail send my mail i am php deloper plese send

    • posted on 2010-Mar-23 | 05:34:17 AM
      Hi Raja,

      PHP testimonal code detail is here. On the same webpage in this tutorial. Just copy and paste the code to your application.
    • ( jabs4chat [ at ] gmail . com ),

      posted on 2010-Mar-30 | 04:04:20 AM
      woe
    • ( victor [ at ] gmail . com ),

      posted on 2010-Jun-20 | 10:50:14 AM
      this is usefull
    • ( imad_baloch [ at ] yahoo . com ),

      posted on 2010-Jul-07 | 03:23:34 AM
      nice site.
    • ( josh . smith [ at ] web . net ),

      posted on 2010-Jul-13 | 11:08:16 PM
      Great Tutorial!
    • ( justy [ at ] yahoo . com ),

      posted on 2010-Jul-15 | 04:32:59 AM
      wow nice little script, simple and easy!!
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: