homepage Welcome to WebmasterWorld Guest from 23.20.149.27
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Displaying Results in a table
MizzBia




msg:3792382
 9:04 am on Nov 23, 2008 (gmt 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

 

MizzBia




msg:3792409
 11:27 am on Nov 23, 2008 (gmt 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.

phranque




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

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]

rocknbil




msg:3792504
 4:57 pm on Nov 23, 2008 (gmt 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]

MizzBia




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

Thank you both! I'll go through your tips and see if I can figure this out.

rocknbil




msg:3793930
 3:32 pm on Nov 25, 2008 (gmt 0)

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

ACK! I used the checkbox too!

phranque




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

if you edited afterwards you have to recheck the checkbox.

Global Options:
 top home search open messages active posts  
 

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

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved