Forum Moderators: coopster
<?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>";
?>
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.
$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 :)
...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]
$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>";
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>";