homepage Welcome to WebmasterWorld Guest from 54.205.241.107
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Dynamic PHP table
How to get it to work
geeklike

5+ Year Member



 
Msg#: 3874097 posted 10:40 am on Mar 19, 2009 (gmt 0)

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>";
?>

 

coopster

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



 
Msg#: 3874097 posted 11:31 am on Mar 19, 2009 (gmt 0)

Welcome to WebmasterWorld, geeklike.

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

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 11:41 am on Mar 19, 2009 (gmt 0)

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.

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 11:44 am on Mar 19, 2009 (gmt 0)

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.

coopster

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



 
Msg#: 3874097 posted 12:01 pm on Mar 19, 2009 (gmt 0)

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.

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 12:09 pm on Mar 19, 2009 (gmt 0)

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 :)

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 12:16 pm on Mar 19, 2009 (gmt 0)

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.

penders

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



 
Msg#: 3874097 posted 12:25 pm on Mar 19, 2009 (gmt 0)

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

geeklike

5+ Year Member



 
Msg#: 3874097 posted 12:25 pm on Mar 19, 2009 (gmt 0)

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

geeklike

5+ Year Member



 
Msg#: 3874097 posted 12:26 pm on Mar 19, 2009 (gmt 0)

- and the thing with joining the variables wouldn't work, either.

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 12:44 pm on Mar 19, 2009 (gmt 0)

..And you did echo them? :P And the gal_img-field in the database does contain image url?

penders

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



 
Msg#: 3874097 posted 12:46 pm on Mar 19, 2009 (gmt 0)

View the source/HTML of your generated page, see how the table is constructed (and where the problems are) and work backwards.

geeklike

5+ Year Member



 
Msg#: 3874097 posted 12:48 pm on Mar 19, 2009 (gmt 0)

Thanks everybody - it works now (:

geeklike

5+ Year Member



 
Msg#: 3874097 posted 12:52 pm on Mar 19, 2009 (gmt 0)

...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>";

Jsyvanne

5+ Year Member



 
Msg#: 3874097 posted 1:02 pm on Mar 19, 2009 (gmt 0)

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

penders

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



 
Msg#: 3874097 posted 1:02 pm on Mar 19, 2009 (gmt 0)

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

5+ Year Member



 
Msg#: 3874097 posted 2:53 pm on Mar 19, 2009 (gmt 0)

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 (:

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved