|
Posted by Hilarion on 12/09/05 14:07
[Message reformatted.]
>>> <table><tr>
>>> <?php
>>> $counter = 1;
>>> while($row = mysql_fetch_array($resultGetPhotos)){
>>> echo "<td>fileFromDB</td>";
>>> if($counter % 5 == 0){
>>> echo "</tr><tr>";
>>> }
>>> $counter++;
>>> $i++;
>> What's that $i for?
> $i is actually a counter for another function of the page - I'm passing it
> within a link for the actual photo display page.
>>> }
>>> ?>
>>> </tr></table>
>> Check the HTML that gets generated by your code when there's
>> 5, 10, 15 etc. photos - you'll get extra "<tr></tr>" in
>> those cases.
> Thanks for the info on the table row issue, I hadn't thought of that.
No problem.
>> Try this:
>>
>> <table>
>> <?php
>> $counter = 0;
>> while($row = mysql_fetch_array($resultGetPhotos))
>> {
>> if ($counter % 5 == 0)
>> echo "<tr>\n";
>> echo "<td>fileFromDB</td>\n";
>> $counter++;
>> if ($counter % 5 == 0)
>> echo "</tr>\n";
>> }
>> ?>
>> </table>
> Also - if I start $counter at 0, won't the first pass automatically ring
> true and start a new row each time?
Yes. Notice that there's no "<tr>" after "<table>", so triggering
at the first pass is a correct behavior.
There's also one more thing that comes to my mind... What if the count
of records returned by the query is not multiplication of 5?
The "<tr>" will not be closed and this way the HTML will be
invalid (in your previous code this would not happen). My solution for this
would be:
<table>
<?php
$counter = 0;
while($row = mysql_fetch_array($resultGetPhotos))
{
if ($counter % 5 == 0)
echo "<tr>\n";
echo "<td>fileFromDB</td>\n";
$counter++;
if ($counter % 5 == 0)
echo "</tr>\n";
}
/// <added_code>
if ($counter % 5 != 0)
{
// fill missing cells
for( ; $counter % 5 != 0; $counter++ )
echo "<td></td>\n";
// add missing row closing tag
echo "</tr>\n";
}
/// </added_code>
?>
</table>
Ths should render valid HTML for any non-zero amount of results.
If it's possible that there'll be no results, then no <table>
should be rendered.
Consider NOT using <table> at all, but wrap each result in <div>
or <span> tags (each with same width and height). They will fill
available width and wrap when needed.
Example:
<html>
<head>
<style type="text/css">
div.images span {
width: 120px;
height: 250px;
padding: 5px;
text-align: center;
}
</style>
</head>
<body>
<div class="images">
<?php
$pattern = <<<EOD
<span>
<a href="%1\$s"><img src="%2\$s"
width="100" height="100" alt="%3\$s" /></a><br />
<a href="%1\$s">%4\$s</a>
</span>
EOD;
// Connect and query here.
while($row = mysql_fetch_array($resultGetPhotos))
{
printf(
$pattern,
htmlspecialchars( $row['image_path'] ),
htmlspecialchars( $row['image_thumbnail_path'] ),
htmlspecialchars( $row['image_name'] ),
htmlspecialchars( $row['image_description'] ),
);
}
?>
</div>
</body>
</html>
The flaw of this solution is that you have to know how much text
can there be as the image description and adjust height of
the <span> tag so that the text fits inside. With <table>
design you do not have this problem (and also when the texts
in some specific row of the table are shorter, then this
specific row will be rendered with lower height and this way
conserve page height). With texts of same average lenght
the <span> solution gives you flexible page width.
Hilarion
[Back to original message]
|