0

I have a page set up for testimonials for a company website. Just the standard message first name, last name, message, blah blah..anyways I can pull the message up and have a single entry show up however, I would like it to randomly generate a 'testimonial' I know there is random_array functions already included in PHP but how would I go about it in a while loop? Thats the only way really I have learned to pull information from a MySQL database. So if there is a simpler way I am all ears.

<?php 
    mysql_select_db("test", $link); 
    $sql= "SELECT * FROM testimonials LIMIT 1";

    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)){ 
        // var_dump($row['message']); die;
?>                      
    <p>
<?php
    echo $row['message']; ?>"<br/>
    <div id="quote"><?php echo $row['first_name']. " " .$row['last_name'];?></div><div id="location"><?php echo $row['city']. " , " .$row['state'];?></div><br/>

    <div class="readmore">
        <a href="greenInformation.php">Click Here to view more</a>
    </div></p>

<?php } ?>
2
  • It depends a bit on what you want to achieve. You can use "shuffle" to keep changing your array, or shuffle just once an then access in sequence. What do you want to do if new items are entered? Give them priority? Lots of ways to do this... Commented May 4, 2013 at 2:58
  • I was hoping there my be a way to just make it ramdom based on the ID in the table. These are not going to be 'real' testimonials these are going to be created and just stored in this static table that will be manually updated if someone has a kind word to say. So I was thinking if there was a way to make it random based off the PK ID it would be easiest Commented May 4, 2013 at 3:03

2 Answers 2

1

Like this?

$sql="SELECT * FROM `testimonials` ORDER BY RAND() LIMIT 1";

Another, and better method, is if you have a ID column, then you could generate a random number and get a row based on that.

$sql="SELECT * FROM `testimonials` WHERE `id`=".mt_rand(1,500);
Sign up to request clarification or add additional context in comments.

10 Comments

only use ORDER BY RAND() for small data sets
oh come on! I assumed he wouldn't have like, 1000 testimonials, if this was from a twitter feed I would have done it differently.
i voted up, agreed i would suspect it was fine, its just worth noteing that it would scale poorly.
LOL. Def not a Twitter feed...maybe 10 testimonials that might get updated once a month max I would say 50-100
If it's 10, when not just have a neat animation with javascript to cycle through them?
|
0

If the number of testimonials increase in a unbelievable way, you can use this:

$answer = mysql_query("SELECT count(*) FROM `testimonials`");  
$nb = mysql_fetch_row($answer);  
$rand = mt_rand(0,$nb[0] - 1);  
$answer = mysql_query("SELECT * FROM `testimonials` LIMIT $rand, 1");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.