I'm working on a page that displays database results in a basic table one row at a time, top to bottom:
NAME EMAIL SITE
NAME EMAIL SITE
NAME EMAIL SITE
NAME EMAIL SITE
I was wondering if/how you can display results in a table both vertically and horizontally, example:
NAME EMAIL SITE NAME EMAIL SITE
NAME EMAIL SITE NAME EMAIL SITE
NAME EMAIL SITE NAME EMAIL SITE
NAME EMAIL SITE NAME EMAIL SITE
<table>
<tr>
<td>$NAME</td>
<td>$EMAIL</td>
<td>$SITE</td>
</tr>
</table>
and I can't for the life of me understand how to do this. Any tips would be appreciated
Thanks
assuming you are generating the html content in perl, your trick is to have the loop put out the <tr> tag before the first and every third record and then put out the </tr> tag after every third record.
you probably also need to handle the blank cells at the end of the last row if the number of records is not evenly divisible by 3.
hint: modulus operator [perldoc.perl.org]
Lets say I am displaying images... Instead of displaying them one after another, is there a way I can display them 3 per row?
This is a simple solution laying the cells in horizontally. If you want to lay them in vertically, you'll have to use phranque's approach and do some precursory math on the length of the list.
# make a list of all images for the page,
# store them in an array. In this case, I'm using a hash
# that contains filename=>caption
%images = &get_images;
# Num columns
$cols = 3;
# A counter to keep track
$counter = 0;
# Start output:
$out = '<html><body><table>';
foreach $v (sort keys %images) {
if ($count == 0) { $out .= '<tr>'; }
$out .= qq(<td><img src="$v"><br>$images{$v}</td>);
# When $count gets to $col (3), reset it.
# this starts a new row on the next loop.
$count++;
if ($count >= $col) { $out .= '</tr>'; $count=0; }
}
}
if (($count > 0) && ($count < $col)) {
for $i ($count..$col) {
$out .= '<td> </td>';
}
$out .= '</tr>';
}
$out .= '</table></body></html>';
Typed on a whim, may contain syntax errors, but this aproach works.
[edited by: phranque at 10:42 pm (utc) on Nov. 23, 2008]
[edit reason] disabled smileys ;) [/edit]