Forum Moderators: coopster & phranque

Message Too Old, No Replies

columnar data -> table

         

bryndyment

2:21 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Hi,

I've retrieved some values from a DB, and want to display them in a table. The data is sorted alphabetically in an array, and I want the results to appear as follows (two columns):

A F
B G
C H
D I
E J

I want each value to be in its own cell. What's a good programmatic way to build a table, since it kinda wants me to finish each ROW in its entirety before moving on? (Wheras I want to build the <td> for "A", then for "B", since I'm iterating through my loop... it would be hard to build the <td> for "A", then for "F", then for "B"... etc.)

In other words, it would be trivial to contruct the following:

A B
C D
E F
G H
I J

But what's a cool way to build a column-centric table, rather than row-centric?

Hope I've described it well (enough).

andreasfriedrich

2:32 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at the PHP [php.net] code I posted here:

Perl Table Column Coding [webmasterworld.com]

Andreas

bryndyment

5:48 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



Thanks... very helpful. I'm wondering, though, for the Perl examples in that post, if it's efficient to use "shift" to work your way through the arrays. It's logically a nice algorithm, but, since you're just reading from the array for output purposes, wouldn't it be better to just iterate through the arrays?

(I'm assuming that 'shift' is expensive because it's moving stuff around...)

Just curious.

My implementation:

@array = ( 1, 2, 3, 4, 5 );
$count = @array;
$half = ( $count + 1 ) / 2;
@col_1 = @$section_ref[ 0 .. $half - 1 ];
@col_2 = @$section_ref[ $half .. $count - 1 ];
for ( $i = 0; $i < @col_2; $i++ ) {
# <tr><td> $col_1[$i] </td><td> $col_2[$i] </td></tr>
}
if ( @col_1 > @col_2 ) {
# <tr><td> $col_1[-1] </td></tr>
}