Ell20 Posted December 30, 2007 Share Posted December 30, 2007 Hi, I have created a form for users to upload photos. When they upload photos the photo name and caption is stored in the database along with club_id. Now I want the images to be displayed on the page on the page where club_id = '$id'. I could just do a simple while loop to show all the images and captions WHERE club_id = '$id' but this is an untidy way of displaying them, as I dont no how many images the club has. Is there anyway I can display the images so that they show 4 across with the caption below it, then say there is 5 images it creates a new row, otherwise it dosent? Then is it possible to say if there are more than 16 images create another page rather than keep displaying them down the page? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/ Share on other sites More sharing options...
MadTechie Posted December 30, 2007 Share Posted December 30, 2007 for the 16 images your need to readup on pagination, as for the displaying them in a grid you could to something like this <?php $dbitems = array("img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg"); $maxcol = 4; echo "<table>"; $c = $maxcol; foreach($dbitems as $img) { if($c==0) echo "<tr>"; echo "<td>"; echo "<img src=\"$img\">"; echo "</td>"; if($c==0) { echo "</tr>"; $c = $maxcol; }else{ $c--; } } echo "</table>"; ?> untested (used a array to same me typing the sql stuff) Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-425919 Share on other sites More sharing options...
Ell20 Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks for the help. I added this mysql in replace of the array: $query = "SELECT * FROM images WHERE club_id = '$id'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $dbitems = $row['picture_name']; $caption = $row['caption']; And it gives the error: An error occured in script /home/mysport/public_html/gallery.php on line 99: Invalid argument supplied for foreach() Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-425928 Share on other sites More sharing options...
MadTechie Posted December 30, 2007 Share Posted December 30, 2007 your need a while loop with the DB, try this (again untested) <?php $query = "SELECT * FROM images WHERE club_id = '$id'"; $result = mysql_query($query); //$dbitems = array("img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg"); $maxcol = 4; echo "<table>"; $c = $maxcol; //foreach($dbitems as $img) while($img = mysql_fetch_assoc($result)) { $imgName= $img['picture_name']; $caption = $img['caption']; if($c==0) echo "<tr>"; echo "<td>"; echo "$imgName<br>";//debugging echo "$caption <br>";//debugging echo "<img src=\"$imgName\">"; echo "</td>"; if($c==0) { echo "</tr>"; $c = $maxcol; }else{ $c--; } } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-425930 Share on other sites More sharing options...
Ell20 Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks for your help! Managed to get it working well, with the caption displayed at the bottom too. Will look into the maximum of 16 per page. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-425937 Share on other sites More sharing options...
MadTechie Posted December 30, 2007 Share Posted December 30, 2007 The basic Idea is to use mysql_num_rows() to count the rows, if their over 16, then use "Limit 0,16" (for page 1) then "Limit 16,32" for page 2 etc, just search here or google for pagination Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-425941 Share on other sites More sharing options...
Ell20 Posted December 30, 2007 Author Share Posted December 30, 2007 Excellent, thanks will look into it. Ive progressed a bit further now with it, however I have just noticed that when you have say 5 images. So that will be 4 across the top then another 1 on the next row? If I add another image in this situation it creates a new row again rather than putting the picture alongside the 5th image? Appreciate your help Quote Link to comment https://forums.phpfreaks.com/topic/83709-solved-displaying-images/#findComment-426095 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.