melting_dog Posted October 23, 2011 Share Posted October 23, 2011 Hi guys, Just after a bit of advice really. I want to create an image gallery. Currently, my images (which are sourced from a DB) are brought about in a loop and set in their own div, side by side. I was just thinking though: would it be better to do this with an array? Its a bit difficult coding for this just using the basic loop. Anyone have any suggestions to make this better? PS, here is my current code: $sql = "SELECT * FROM imagegal"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)){ echo '<span class="imagegal">'; echo '<span><img src="' . $row['image'] . '"</span>'; echo '</span>'; }}} Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/ Share on other sites More sharing options...
freelance84 Posted October 23, 2011 Share Posted October 23, 2011 $sql = "SELECT * FROM imagegal"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)){ echo '<span class="imagegal">'; echo '<span><img src="' . $row['image'] . '"</span>'; echo '</span>'; }}} If all you are getting there is the location of the image and nothing else, don't bother selecting everything from your table, just get the location. Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281536 Share on other sites More sharing options...
melting_dog Posted October 23, 2011 Author Share Posted October 23, 2011 Thanks, I'm actually getting the image and all other information (caption, alt text, etc) fine. Im just having a good deal of difficulty with the styling - I want to create rows of three images next to each other (so if I had 12 images I'd get 4 rows of three) . I was wondering if there was a better way to go about the gallery that I was missing. List? Tables? Or maybe arrays? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281657 Share on other sites More sharing options...
litebearer Posted October 23, 2011 Share Posted October 23, 2011 Perhaps not the best; however, might be a start for you http://www.nstoia.com/sat/disp_pag/ Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281658 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 I would use tables. You are presenting data in a grid. Here's a simple way to output a group of data as a table <?php // Make dummy array $array = range(0,32); // This will keep track of how many rows we've echo'd $i = 0; // This tells us how many columns $cols = 3; $total = count( $array ); echo '<table><tr>'; foreach( $array as $value ) { // Echo the value in a cell echo '<td style="border:1px solid black;">'.$value.'</td>'; // Increase our counter by 1 before we check $i++; // Check to see if we need to start a new row by using the modulus operator // It'll give us the remainder of the two numbers divided. // We check if $i != $total to make sure we don't have a redundant empty row // at the end if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row } // Now after the foreach, if we have an odd number of results, we'll have an // extra empty column we need to fill, we can do this automatically while( ($i % $cols) != 0 ) { echo '<td> </td>'; $i++; } echo '</tr></table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281659 Share on other sites More sharing options...
bspace Posted October 24, 2011 Share Posted October 24, 2011 one of the many online tutorials to do something like you want http://dustinbrewer.com/creating-a-photo-gallery-in-css-without-tables/ assuming you can get your data into a list (a simple loop will do this) like in this example just use css to format it principal is to "display;inline:" and make a bounding div just wider than the width of eg 3 images Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281668 Share on other sites More sharing options...
melting_dog Posted October 24, 2011 Author Share Posted October 24, 2011 Thanks guys, Was able to do it just with CSS and display:inline. Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/249634-best-way-for-image-gallery/#findComment-1281680 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.