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++;
}
?>

Link to comment
Share on other sites

$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 } ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.