Forum Moderators: coopster

Message Too Old, No Replies

dynamic php table

create dynamic php image table

         

kristie380

3:44 am on Nov 11, 2006 (gmt 0)

10+ Year Member



Hi - I am trying to arrange my images in a dynamic table with 3 columns. I got this code below from this forum and the only problem that I am having with it is that instead of displaying each picture only once, it displays them 5 times each (because I currently have 5 images in the directory). If I add another image to my directory it will display each of my images 6 times, and so on. Can someone please look at my script and tell me how to print each picture only once? Thanks!

$sql = "select * from reunion_photos";
$result = mysql_query($sql);
$tdcount = 1;
$numtd = 3; // number of cells per row
while($row = mysql_fetch_array($result)) {
if ($tdcount == 1)
echo "<tr>";
echo "<td><img src=\"$image\" width=\"$width\" height=\"$height\">
</td>"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1; }
else { $tdcount++; } } // time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td></td>"; $tdcount++; }
echo "</tr>"; }

mcibor

10:02 am on Nov 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the code you provided there is no mistake, at least I couldn't find one.

What exactly do you return from db?

Check it out:

while($row = mysql_fetch_array($result)) {
...
echo "Image: $image Width: $width Height: $height<br>\n";
echo "Direct DB Image: ".$row['image']."<br>\n";
}

And see what happens.

Regards
Michal

kristie380

4:21 am on Nov 13, 2006 (gmt 0)

10+ Year Member



No unfortunately that didn't help. I am requesting data from this table because eventually I'm going to add the photo comment/description under each photo. I just haven't added that part to the script yet. Here is another script I have tried:

$sql = "select * from reunion_photos";
$result = mysql_query($sql);

$countImages= count("$image");
echo "<table><tr>";

for($x=1; $x<=$countImages; $x++) {

echo "<td><img src=\"$image\" width=\"$width\" height=\"$height\">
</td>"; // display as you like

if($x % 3 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>"; }

This script will print each picture only once but it just lines them up vertically instead of the 3 pictures per row. Any suggestions here?

eelixduppy

4:56 am on Nov 13, 2006 (gmt 0)




$sql = "select * from reunion_photos";
$result = mysql_query($sql);
echo "<table>";
$counter = 1;
while($row = mysql_fetch_array($result)) {
if(($counter % 3) == 0 ¦¦ $counter == 1) {
echo '<tr>';
}
echo "<td><img src='".$row['image']."' width='".$row['width']."' height='".$row['height']."'></td>";
if(($counter % 3) == 0) {
echo '</tr>';
}
counter++;
}
echo "</table>";

...I think that should do the trick, but I'm tired and cannot think straight!

Good luck

mcibor

1:20 pm on Nov 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The error was here:

if($x % 3 == 0) {

First is done comparison - and so you get:

if($x % false) {

then
if($x % 0) {
- simple type resolution
if(0) {
- omits the tr ending

Correction is:

if(($x % 3) == 0) {

adding another break.

Eelixduppy, sometimes it's better to understand the mistake, than writing the correct, totally different version :)
Regards
Michal

PS. 5:56 am on Nov 13, 2006 is not a time to think ;)

kristie380

6:38 pm on Nov 13, 2006 (gmt 0)

10+ Year Member



I really appreciate all of your suggestions but what is happening is that instead of placing all of the pictures in the same table, my code is starting a new table for each picture. Maybe it will help to show the code above the table...here is the most recent code I am working with:

<?php
if ((is_dir("/reunion_photos/$id")) ¦¦ (file_exists("/reunion_photos/$id"))) {

$dirname = "/reunion_photos/$id";
$dh = opendir($dirname) or die("<i>There was an error</i>");

while (false!== ($file = readdir($dh))) {
if ($file!= "." && $file!= "..") {

$image = "$dirname/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$height = 150;
$percent = ($size[1] / $height);
$width = ($size[0] / $percent);
}
else if ($width > 150)
{
$width = 150;
$percent = ($size[0] / $width);
$height = ($size[1] / $percent);
}

$sql = "select * from reunion_photos";
$result = mysql_query($sql);

$countImages= count("$image");
echo "<table width=\"100%\"><tr>";

for($x=1; $x<="$countImages"; $x++) {

echo "<td align=\"center\"><img src=\"$image\" width=\"$width\" height=\"$height\"> </td>";

if(($x % 3) == 0) {
echo "</tr><tr>";
}

}
}
}
echo "</tr></table>";
closedir($dh);
?>

I tried changing the table tags around but can't seem to get that to work either.

mcibor

8:59 pm on Nov 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. So now we see the problem.

1. What is sql doing there? Nothing
2. While looping through files you count one file count($image) = 1 - it's string, not an array
3. Open dir doesn't work on a single file, so file exists you can disregard

Try this out:

if (is_dir("/reunion_photos/$id") {

$dirname = "/reunion_photos/$id";
$count = 1;

echo "<table width=\"100%\"><tr>";

$dh = opendir($dirname) or die("<i>There was an error</i>");

while (false!== ($file = readdir($dh))) {
if ($file == "." ¦¦ $file == "..") continue;

$image = "$dirname/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$width = $width * 150 / $height; //php can do math, it's not assembler
$height = 150;
}

if ($width > 150)
{
$height = ($height * 150 / $width);
$width = 150; //this way it shouldn't be bigger in any direction than 150px
}

echo "<td align=\"center\"><img src=\"$image\" width=\"$width\" height=\"$height\"> </td>";

if(($count % 3) == 0) echo "</tr><tr>";
$count++;
}//end while

echo "</tr></table>";
closedir($dh);

Isn't that easier?
Michal

PS. I hope I didn't make any mistakes - I didn't check this code

kristie380

1:00 am on Nov 14, 2006 (gmt 0)

10+ Year Member



You're a genius! Thank You! I can't tell you how long I've been working on this problem. For anyone else that wants to use this code, here is the final script...

<?php

if (is_dir("/reunion_photos/$id")) {
$dirname = "/reunion_photos/$id";
$count = 1;

echo "<table width=\"100%\"><tr>";

$dh = opendir($dirname) or die("<i>There was an error</i>");

while (false!== ($file = readdir($dh))) {
if ($file == "." ¦¦ $file == "..") continue;

$image = "$dirname/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$width = $width * 150 / $height; //php can do math, it's not assembler
$height = 150;
}

if ($width > 150)
{
$height = ($height * 150 / $width);
$width = 150; //this way it shouldn't be bigger in any direction than 150px
}

echo "<td align=\"center\"><img src=\"$image\" width=\"$width\" height=\"$height\"> </td>";

if(($count % 3) == 0) echo "</tr><tr>";
$count++;
}//end while

echo "</tr></table>";
closedir($dh);
}
?>

mcibor

9:54 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to be of help.

Sometimes asking the right question is the hardest thing to do.
Keep the good work and have fun with PHP

And Welcome to Webmasterworld!

Michal

kristie380

4:19 am on Nov 16, 2006 (gmt 0)

10+ Year Member



Ok so what if I wanted to get fancier and add the file comment that the user submits underneath the photo? I added an sql statement for this but it is only posting the same comment for each picture...which means it is only looping through the database once. How can I fix this so that it doesn't mess up my table and at the same time, loops through the database and prints each comment under the appropriate picture? See my code below...

<?php
$sql = "select * from reunion_photos WHERE id LIKE '%".$id."%'";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result)) {

$id = $newArray['id'];
$file_comment = $newArray['file_comment'];
$firstname = $newArray['firstname'];
$lastname = $newArray['lastname'];
$maidenname = $newArray['maidenname'];

}

if (is_dir("/reunion_photos/$id")) {
$dirname = "/reunion_photos/$id";
$count = 1;

echo "<table width=\"100%\"><tr>";
echo "<td colspan=\"3\" align=\"center\"><h3>$firstname $lastname $maidenname's Reunion Photo Gallery</h3>
<i>Click on a picture to see it enlarged</i><br><br></td></tr><tr>";

$dh = opendir($dirname) or die("<i>There was an error</i>");

while (false!== ($file = readdir($dh))) {
if ($file == "." ¦¦ $file == "..") continue;

$image = "$dirname/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$width = ($width * 150 / $height);
$height = 150;
}

if ($width > 150)
{
$height = ($height * 150 / $width);
$width = 150;
}

echo "<td align=\"center\"><a href=\"http://www.sbhs76.com/$image\" target=\"_blank\">
<img src=\"$image\" width=\"$width\" height=\"$height\" border=\"0\"></a>
<br>$file_comment<br><br></td>";

if(($count % 3) == 0) echo "</tr><tr>";
$count++;
}//end while
echo "</tr></table>";

}
?>

mcibor

5:20 pm on Nov 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is in the first while loop - after running the loop in $id you've got only last received value.

You need to change few things:
add a column `file` to combine files with comments. It has to contain exact names of files that are in the directory.

You could also add author of the comment if you wish

<?php
$sql = "select * from reunion_photos WHERE id LIKE '%".$id."%'";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result)) {

$file = $newArray['file'];
$file_comment[$file][] = $newArray['file_comment']; //this way you can have more comments to one file
$comment_author[$file][] = $newArray['coment_author'];
$firstname = $newArray['firstname'];
$lastname = $newArray['lastname'];
$maidenname = $newArray['maidenname'];

}

if (is_dir("/reunion_photos/$id")) {
$dirname = "/reunion_photos/$id";
$count = 1;

echo "<table width=\"100%\"><tr>";
echo "<td colspan=\"3\" align=\"center\"><h3>$firstname $lastname $maidenname's Reunion Photo Gallery</h3>
<i>Click on a picture to see it enlarged</i><br><br></td></tr><tr>";

$dh = opendir($dirname) or die("<i>There was an error</i>");

while (false!== ($file = readdir($dh))) {
if ($file == "." ¦¦ $file == "..") continue;

$image = "$dirname/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$width = ($width * 150 / $height);
$height = 150;
}

if ($width > 150)
{
$height = ($height * 150 / $width);
$width = 150;
}

echo "<td align=\"center\"><a href=\"http://www.sbhs76.com/$image\" target=\"_blank\">
<img src=\"$image\" width=\"$width\" height=\"$height\" border=\"0\"></a>
<br>";
foreach($file_comment[$file] as $key => $comment)
{
echo "$comment<br /><b>". $comment_author[$file][$key]."</b><br><br>";

}
echo "</td>";

if(($count % 3) == 0) echo "</tr><tr>";
$count++;
}//end while
echo "</tr></table>";

}
?>

I hope it helps you.
Michal

kristie380

7:48 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



Ok I changed it up a bit and it is working except for my opening <tr> tag. If I place it above the while statement, it lines up all photos in 1 horizontal line. If I place it in the while statement, it places each photo on a new line.

<?php
$sql = "select * from reunion_photos WHERE id LIKE '%".$id."%'";
$result = mysql_query($sql,$db);

echo "<tr>";

while ($newArray = mysql_fetch_array($result)) {

$file= $newArray[file];
$file_comment = $newArray['file_comment'];

$count = 1;

$image = "reunion_photos/$id/$file";
$size = getimagesize("$image");
$height = $size[1];
$width = $size[0];
if ($height > 150)
{
$width = ($width * 150 / $height);
$height = 150;
}

if ($width > 150)
{
$height = ($height * 150 / $width);
$width = 150;
}

echo "<td align=\"center\"><a href=\"$image\" target=\"_blank\">
<img src=\"$image\" width=\"$width\" height=\"$height\" border=\"0\"></a>
<br>$file_comment<br><br></td>";

if(($count % 3) == 0) echo "</tr><tr>";
$count++;
}//end while

echo "</tr></table>";

?>

eelixduppy

8:50 pm on Nov 16, 2006 (gmt 0)




.......code....blah...blah
[b]if(($count % 3) == 0 ¦¦ $count == 1) {
echo '<tr>';
}[/b]
echo "<td align=\"center\"><a href=\"$image\" target=\"_blank\">
<img src=\"$image\" width=\"$width\" height=\"$height\" border=\"0\"></a>
<br>$file_comment<br><br></td>";
[b]
if(($count % 3) == 0) {
echo "</tr>";
}[/b]
$count++;
}//end while

......code blah blah....

That should do the trick :)

Nazaret2005

1:14 am on Nov 18, 2006 (gmt 0)

10+ Year Member



This for you :)


$columns = 4; //how many columns we want

echo "<table>\n";

$sql = "select * from reunion_photos";
$result = mysql_query($sql);

for ($i = 0; $row = mysql_fetch_assoc($result); $i++) {

if (($i % $columns) == 0) {
echo " <tr>\n";
}

echo "<td><img src=\"" . $row['image'] . "\" width=\"" . $row['width'] . "\" height=\"" . $row['height'] . "\">
</td>\n";

if (($i % $columns) == ($columns - 1)) {
echo " </tr>\n";
}
}

kristie380

2:29 am on Nov 18, 2006 (gmt 0)

10+ Year Member



Thank you everyone! It is working now :)