Page 1 of 1

create dynamic html table with images inside loop

Posted: Tue Mar 10, 2015 6:48 pm
by servified
Hello,

I am new to PHP and am currently stuck :banghead: . I am trying to create a script to take

image data from mysql(image url, gallery url, description) and display it in a

dynamic table grid. I am having some trouble getting my $columns and $rows to print

correctly inside a table and while inside a loop. I am stumped so I am finally

reaching out for some help.

I have the code setup to print 5 images wide x 5 columns, so for example it looks

like this:
[] [] [] [] []
[] [] [] [] []
works fine.. however when I change my columns and rows... 7 and 7 it looks like

this:
[] [] [] [] []
[] []
[] [] []
[] [] [] []
[]
[] [] [] [] []
[]
here is my code:

Code: Select all

include"./mysql.php";

//define variables
$uniqhash = "";
$modelname = "";
$email = "";
$galleryurl = "";
$gallerydesc = "";
$category = "";
$datesubmitted = "";
$gallerythumb = "";
$rows = 0;
$count = 0;
$columns = 0;

$select = "SELECT * From `models`";
$images_rows = mysqli_query($conn, $select);
$images = array();
if( empty( $images ) )
{
     echo 'Sorry, there was a fatal error in selecting images from database.';
}
while ($image = mysqli_fetch_assoc($images_rows)) {
   $images[] = $image;
}
echo '<table align="center" border="0"><tr>';

$size = sizeOf($images) - 1; //get count of array and put into $size
foreach($images as $image){
    if($rows == 7) { 
     echo '</tr><tr>'; // if our count is x wide, start a new row
        $rows = 0; 
    }
    if ($columns == 7){
        echo '</tr><tr>';
        $columns = 0;
    }
     echo "<td><a href=" . $image['galleryurl'] . " target=\"new\"><img src=" . 

$image['gallerythumb'] . " height=\"160\" width=\"160\" alt=\"" . $image

['gallerydesc'] . "\"></a></td>";
        $rows++; 
        $count++; 
        $columns++;
     echo ($rows == 7 && $columns == 7 && $count != $size) ? '</tr>': '';
    }
echo '</tr></center></table>';
If anyone could point a new php programmer in the right direction I would very much

appreciate it.

P.S I am aware of sanitizing my data when working with mysql but I want to make sure my core function works first.

Thanks!

Re: create dynamic html table with images inside loop

Posted: Tue Mar 10, 2015 7:35 pm
by Celauran
You might do better to display the images in floating divs instead of a table. That way you can display 3 or 5 or 7 or whatever your screen size supports.

Re: create dynamic html table with images inside loop

Posted: Tue Mar 10, 2015 9:26 pm
by servified
Is using the css/floating images in divs friendly with displaying an image in a loop?