Welcome to WebmasterWorld Guest from 23.20.37.222

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Dynamic PHP table

How to get it to work

     
10:40 am on Mar 19, 2009 (gmt 0)

5+ Year Member



Hi,
I'm trying to make a PHP gallery, using images from my database, but with the codes I have, it doesn't show the most recently uploaded image, and it doesn't break it down to just three images on each line. Can anybody help?

<?php

include('inc/db_connect.inc');

$gallery_query="SELECT * FROM gallery ORDER BY gal_id DESC";
$gallery_result=mysql_query($gallery_query);
$gallery_data = mysql_fetch_assoc($gallery_result);

$image = $gallery_data['gal_img'];
$size = getimagesize($image);
$height = $size[1];
$width = $size[0];

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

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

$out .="<table>"
. "<tr>";


for ($i = 1; $gallery_data = mysql_fetch_assoc($gallery_result); $i++) {

$outtd.="<td class='img'>"
."<a href='" . $gallery_data['gal_img'] . "'>"
."<img src='" . $gallery_data['gal_img'] . "' width='" . $width . "px' height='" . $height . "px' alt='" . $gallery_data['gal_name'] . "'/>"
."</a>"
."</td>";

if($i % 3 == 0) {
$outtr .='</tr>'
. '<tr>';
}
}
$outslashtable="</table>";
?>

11:31 am on Mar 19, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld, geeklike.

You have your "for" loop after you have already fetched the first data result.

11:41 am on Mar 19, 2009 (gmt 0)

5+ Year Member



Actually that for loop almost works...didn't know you could do it like that. I tried it and got 3 column table, but with broken html...and error messages, and notices...

and no images, but placeholders for them.

11:44 am on Mar 19, 2009 (gmt 0)

5+ Year Member



perhaps geeklike should search for some image-gallery scripts and learn from there. Unless theres more new things to me than that for-loop, that will not display any images.
12:01 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Actually that for loop almost works

Except for the fact that the first result row has already been fetched. The first result row contains "the most recently uploaded image" and the for loop is going to start processing at the next row in the result set because the internal pointer has been moved forward.

You are only seeing image placeholders because you don't have the full link to the image in your src attribute as geeklike will -- it's being pulled from a database table.

12:09 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



I was thinking that all image data was on the database. In which case he will not see images. I thought that this had something to do with image data being in the database:

$image = $gallery_data['gal_img'];
$size = getimagesize($image);

I tried this with db containing image data. So the for-loop magic didn't work after all :)

12:16 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



Oh and I'm still missing things :P Not my day. The for-loop magic would work if that first row wasn't fetched in the beginning of the script...

anyways. 3 columns. almost :) got something almost correctly.

12:25 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...and it doesn't break it down to just three images on each line.

You appear to be using 3 separate variables $out, $outtd and $outtr and you don't seem to be joining these together in any way - so that looks like your problem. I think you might just be able to use the one variable $out throughout....?

$newrow = true; 
$out .= "<table>";
for ($i = 1; $gallery_data = mysql_fetch_assoc($gallery_result); $i++) {
if ($newrow) {
$out .= '<tr>';
$newrow = false;
}
$out .= "<td class='img'>"
."<a href='" . $gallery_data['gal_img'] . "'>"
."<img src='" . $gallery_data['gal_img']
. "' width='" . $width . "px' height='"
. $height . "px' alt='" . $gallery_data['gal_name'] . "'/>"
."</a>"
."</td>";
if ($i % 3 == 0) {
$out .= '</tr>';
$newrow = true;
}
}
$out = '</tr></table>';

Sorry, I added some more bits when I realised you would have a mismatch of opening/closing TR's.

(hhhmm, bit slow posting...)

[edited by: penders at 12:40 pm (utc) on Mar. 19, 2009]

[edited by: coopster at 4:22 pm (utc) on Mar. 19, 2009]
[edit reason] fixed sidescroll [/edit]

12:25 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



I tried what you guys said, and it still doesn't work
- if it helps, this is what I have now;

$gallery_query="SELECT * FROM gallery ORDER BY gal_id DESC";
$gallery_result=mysql_query($gallery_query);

$out .="<table>"
. "<tr>";

for ($i = 0; $gallery_data = mysql_fetch_assoc($gallery_result); ++$i) {

$image = $gallery_data['gal_img'];
$size = getimagesize($image);
$height = $size[1];
$width = $size[0];

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

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

$outtd .= "<td class='img'>"
. "<a href='" . $gallery_data['gal_img'] . "'>"
. "<img src='" . $gallery_data['gal_img'] . "' width='" . $width . "px' height='" . $height . "px' alt='" . $gallery_data['gal_name'] . "'/>"
. "</a>"
. "</td>";

if($i == 2) {
$outtr .= "</tr>"
. "<tr>";
}
}

$outslashtable .= "</tr>"
. "</table>";

12:26 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



- and the thing with joining the variables wouldn't work, either.
12:44 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



..And you did echo them? :P And the gal_img-field in the database does contain image url?
12:46 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



View the source/HTML of your generated page, see how the table is constructed (and where the problems are) and work backwards.
12:48 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



Thanks everybody - it works now (:
12:52 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



...or, apparently not - when I make it resize the images, it still doesn't show the most recent one :/

This is the code;
include('inc/db_connect.inc');

$gallery_query="SELECT * FROM gallery ORDER BY gal_id DESC";
$gallery_result=mysql_query($gallery_query);
$gallery_data = mysql_fetch_assoc($gallery_result);

$image = $gallery_data['gal_img'];
$size = getimagesize($image);
$height = $size[1];
$width = $size[0];

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

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

$newrow = true;
$out .="<table>";

for ($i = 0; $gallery_data = mysql_fetch_assoc($gallery_result); ++$i) {
if($newrow) {
$out .= '<tr>';
$newrow = false;
}

$out .= "<td class='img'>"
. "<a href='" . $gallery_data['gal_img'] . "'>"
. "<img src='" . $gallery_data['gal_img'] . "' width='" . $width . "px' height='" . $height . "px' alt='" . $gallery_data['gal_name'] . "'/>"
. "</a>"
. "</td>";

if($i % 3 == 0) {
$out .= "</tr>";
$newrow = true;
}
}

$out .= "</tr>"
. "</table>";

1:02 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



$gallery_data = mysql_fetch_assoc($gallery_result);

this moves the pointer one step.

this after the first fetch should fix it?
mysql_data_seek($gallery_result,0)

1:02 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You have reverted back to using the part of your code that pulls the first image
mysql_fetch_assoc()
from the data before looping through your data - see coopster's first post.
2:53 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



It worked, thanks Jsyvanne (:
And penders - I know, but it didn't show anything when I removed it, which is why I put it back (:
 

Featured Threads

Hot Threads This Week

Hot Threads This Month