Welcome to WebmasterWorld Guest from 23.20.116.7

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Displaying Results in a table

     
9:04 am on Nov 23, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 21, 2008
posts:55
votes: 0


Hi everyone!
bear with me, I'm not a programmer :(

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

Is this possible? How would I go about doing this?
My current code is

<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

11:27 am on Nov 23, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 21, 2008
posts: 55
votes: 0


What I actually meant is...
How to display multiple results/data per row.

Lets say I am displaying images... Instead of displaying them one after another, is there a way I can display them 3 per row?

Sorry about my first post, I'm exhausted and it probably didn't make much sense.

2:20 pm on Nov 23, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10551
votes: 10


if you were displaying 3 records per row:
<table>
<tr>
<td>$NAME</td>
<td>$EMAIL</td>
<td>$SITE</td>
<td>$NAME</td>
<td>$EMAIL</td>
<td>$SITE</td>
<td>$NAME</td>
<td>$EMAIL</td>
<td>$SITE</td>
</tr>
</table>

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]

4:57 pm on Nov 23, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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

# The images are done, now let's address the last row.
# If $col == 0 or 3, this will do nothing. BUT if
# it's less than 3, create empty cells to even out
# the columns of the last row.

if (($count > 0) && ($count < $col)) {
for $i ($count..$col) {
$out .= '<td>&nbsp;</td>';
}
$out .= '</tr>';
}
$out .= '</table></body></html>';

print "content-type: text/html\n\n";
print $out;

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]

8:22 pm on Nov 23, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 21, 2008
posts: 55
votes: 0


Thank you both! I'll go through your tips and see if I can figure this out.
3:32 pm on Nov 25, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


[edit reason] disabled smileys ;) [/edit]

ACK! I used the checkbox too!

9:54 pm on Nov 25, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10551
votes: 10


if you edited afterwards you have to recheck the checkbox.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members