SF23103 Posted May 17, 2011 Share Posted May 17, 2011 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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/ Share on other sites More sharing options...
phppaper Posted May 17, 2011 Share Posted May 17, 2011 $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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216353 Share on other sites More sharing options...
SF23103 Posted May 17, 2011 Author Share Posted May 17, 2011 Awesome, thanks! I just added the LIMIT 5... which is so simple! Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216354 Share on other sites More sharing options...
phppaper Posted May 17, 2011 Share Posted May 17, 2011 add ORDER BY RAND() into the sql if you want it to be random Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216356 Share on other sites More sharing options...
SF23103 Posted May 17, 2011 Author Share Posted May 17, 2011 Where does the ORDER BY RAND() go exactly? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216363 Share on other sites More sharing options...
phppaper Posted May 17, 2011 Share Posted May 17, 2011 before limit 5 Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216365 Share on other sites More sharing options...
SF23103 Posted May 17, 2011 Author Share Posted May 17, 2011 Awesome!! Quote Link to comment https://forums.phpfreaks.com/topic/236607-display-certain-number-of-database-entries-random/#findComment-1216366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.