[英]Getting random array from a while loop in PHP
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?
只是标准消息的名字,姓氏,消息等等……无论如何,我都可以拉出消息并显示一个条目,但是,我希望它随机生成一个“推荐”,我知道已经有random_array函数包含在PHP中,但是如何在while循环中处理呢? Thats the only way really I have learned to pull information from a MySQL database.
那是我真正学会了从MySQL数据库提取信息的唯一方法。 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 } ?>
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. 另一种更好的方法是,如果您有一个ID列,则可以生成一个随机数并根据该数字获得一行。
$sql="SELECT * FROM `testimonials` WHERE `id`=".mt_rand(1,500);
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.