Jump to content

Display certain number of database entries (random)


SF23103

Recommended Posts

Hello,

 

I am using the following code to display images managed by a MySQL database.  Basically another program manages a bunch of images, but this script displays certain ones (ones with INCLUDE = 1 in the database) on my main page.  My question is, is there an easy way to limit the number of images it displays, say to 5?  I'm not too concerned which images actually display (ascending or descending)... or better yet, random!  Most importantly, I only want five to display.  Each image will be linked to the full page, which displays all the images.

 

Any ideas?

 

Thanks!

 

 

<?php
$username="XXXXXXX";
$password="XXXXXXX";
$database="XXXXXXX";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM ft_form_12 WHERE col_24='1'";
// $query="SELECT * FROM ft_form_12";
// SELECT * FROM ft_form_12 WHERE col_24='1'
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<?php
$i=0;
while ($i < $num) {

$f20=mysql_result($result,$i,"col_23"); //Photo file name
$f21=mysql_result($result,$i,"col_24"); //INCLUDE
?>

<a href="http://www.domain.com/display_whole_page.shtml"><img src="http://www.domain.com/the_file/pictures/<?php echo $f20; ?>" height="50" border="0"></a>   
                
<?php
$i++;
}
?>

$query="SELECT * FROM ft_form_12 WHERE col_24='1' ORDER BY RAND() LIMIT 5";

$result = mysql_query($query);

while($row = mysql_fetch_array($result)){?>

<a href="http://www.domain.com/display_whole_page.shtml"><img src="http://www.domain.com/the_file/pictures/<?php echo $row['col_23']; ?>" height="50" border="0"></a>

<?php } ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.