Jump to content

Best way for image gallery?


melting_dog

Recommended Posts

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>';

 

}}}

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>';

?>

Link to comment
Share on other sites

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

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.