Display images with change when refresh
<?php
$sql1= mysql_query("SELCT * FROM adds ORDER BY adds_id DESC ");
while($row=mysql_fetch_array($sql1)){?>
<tr>
<td width="134" valign="top" style="padding-left:10px;">
开发者_StackOverflow社区 <?php echo $row['image_name'];?>
</td>
when I use this code I able to display a single image, but I want to display an image with refresh, means when user refresh - a new image will display.
Can this will be happen with rand()
or need to any JavaScript
Your WHILE loop is looping over all the rows,and echoing the last row's image. The required change in your code can be to modify your query little bit:
Select * from adds order by rand() limit 1;
This will select only 1 row,which is random from the data set. You do not need to iterate over everything if you do not need that data. And it will display a random image as required.
TRY
<?php
$sql1= mysql_query("SELCT * FROM adds ORDER BY RAND() LIMIT 1");
while($row=mysql_fetch_array($sql1)){?>
<tr>
<td width="134" valign="top" style="padding-left:10px;">
<?php if(!empty($row['image_name'])) { ?>
<img src="images/thumbs/thumb_<?php echo $row['image_name'];?>" />
<?php } else {
echo "text wahatever";
?>
</td>
For a really simple fix, change your query to ORDER BY RAND()
perhaps?
精彩评论