ashrobbins87 Posted April 6, 2009 Share Posted April 6, 2009 I have a feature on my site which displays images for events in a gallery. Users can upload images to a directory on the server and the path to the file is saved in the DB. This path is then used to call the images to the gallery. At the moment though I can only get the images to display in a long list, and I would like to put them in table perhaps 2 or 3 columns wide. I dont know how to alter my loop so that it puts the first two images in one table row, then the next two in another row etc etc. As you can see I have already had a go, but it displays the same image twice in a row, then the next image twice in the next row. Any help please? Code is below.... <?php //Display images for this event $mysqli = mysqli_connect("localhost", "root", "Cadbury5", "opp_group"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $eventImages_sql = "SELECT path FROM event_galleries WHERE event_title='".$event_title."' ORDER BY path DESC"; $eventImages_res = mysqli_query($mysqli, $eventImages_sql) or die (mysqli_error($mysqli)); if ($eventImages_res) { while ($newArray = mysqli_fetch_array($eventImages_res, MYSQLI_ASSOC)) { echo "<table align=\"center\"><tr>"; for ($x=1; $x<=2; $x++) { $path = $newArray["path"]; echo "<td><a href=\"cms/".$path."\" target=\"_blank\"><img src=\"cms/".$path."\" width=\"150\" height=\"100\"></a><br/>"; } echo "</tr></table>"; } } else { printf("Could not retreive records: %s\n", mysqli_error($mysqli)); } mysqli_free_result($eventImages_res); mysqli_close($mysqli); } ?> Link to comment https://forums.phpfreaks.com/topic/152769-display-db-images-in-a-grid/ Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 6, 2009 Share Posted April 6, 2009 <?php ... $MAX_COLUMNS = 3; /* Max row to display*/ $columns = 1; /* start at row 1 */ echo "<table align=\"center\"><tr>"; while ($newArray = mysqli_fetch_array($eventImages_res, MYSQLI_ASSOC)) { $path = $newArray["path"]; echo "<td><a href=\"cms/".$path."\" target=\"_blank\"><img src=\"cms/".$path."\" width=\"150\" height=\"100\"></a></td>"; $columns = $columns + 1; if ($columns > $MAX_COLUMNS) { $columns = 1; echo "</tr><tr>"; } } /* Fill the missing cell if image numbers aren't multiple of $MAX_COLUMNS to make valid HTML markup and be sure it will be display properly on all browser */ while ($columns > $MAX_COLUMNS) { $columns = $columns + 1; echo "<td></td>"; } echo "</tr></table>"; ... ?> May not be the most efficient way to do it but it should work. May need some tweak as i haven't tested it. You should maybe add a mysql_num_rows and skip the whole thing if you don't have any image to display. Link to comment https://forums.phpfreaks.com/topic/152769-display-db-images-in-a-grid/#findComment-802384 Share on other sites More sharing options...
ashrobbins87 Posted April 6, 2009 Author Share Posted April 6, 2009 Mate you're a genius that works perfectly!! Didnt even have to tweak it at all! Thank you Link to comment https://forums.phpfreaks.com/topic/152769-display-db-images-in-a-grid/#findComment-802575 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.