Here is a function I use. Assuming data is stored in an array called $items
you will call out horizontalTable() like this:

horizontalTable(3,600,1,3,3, $items);

Here is the horizontalTable() function code:

// $size: number of columns 
// $width: table width
// $border: table border
// $cpadding: table cellpadding
// $cspacing: table cellspacing
// $data: array with data you want to display

function horizontalTable($size, $width, $border, $cpadding, $cspacing,
$data)
{

        $table_width = $width;
        $width = intval($table_width / $size);

        $tr = '<TR ALIGN="left">';
        $td = "<TD WIDTH=\"$width\">%s</TD>";
        
        $table = "<TABLE WIDTH=\"$table_width\" BORDER=\"$border\"
CELLPADDING=\"$cpadding\" CELLSPACING=\"$cspacing\">" . $tr;

        $i = 0;
        foreach($data as $value)
        {
                $table .= sprintf($td, $value);
                $i++;

                if(!($i % $size)) {
                        $table .= '</TR>' . $tr;
                }
        }

        while($i % $size)
        {
                $table .= sprintf($td, '&nbsp;');
                $i++;
        }

        $end_tr = strlen($tr) * -1;
        if(substr($table, $end_tr) != $tr){
                $table .= '</TR>';
        } else {
                $table = substr($table, 0, $end_tr);
        }

        $table .= '</TABLE>';
        
        return $table;
        
}

-----Original Message-----
From: Raditha Dissanayake [mailto:[EMAIL PROTECTED] 
Sent: Thursday, January 08, 2004 5:30 AM
To: dareal hamsta
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Sorting data into columns vertically

Where is your data stored? if you are using a DB surely using order by 
would less painfull?

all the best

dareal hamsta wrote:

> [ Please copy me off list. ]
>
> Say I have 7 items of data and I wish to sort them into 3 columns in a 
> HTML table. The items are unevenly sized, so rather than print them 
> out in rows - resulting in lots of wasteful whitespace - I would like 
> to output them in vertical order. However if I use the modulus 
> operator to check for when to break into a new column...
>
> foreach ($items as $item) {
>     $counter++;
>     if ( ($counter % $columns) == 0) {
>         print "</td><td>";
>     }
> }
>
> ...the output will be something like this...
>
>  +-------+-------+-------+
>  | Item1 | Item4 | Item7 |
>  | Item2 | Item5 |       |
>  | Item3 | Item6 |       |
>  +-----------------------+
>
> ...when what I'm really looking for is this...
>
>  +-------+-------+-------+
>  | Item1 | Item4 | Item6 |
>  | Item2 | Item5 | Item7 |
>  | Item3 |       |       |
>  +-----------------------+
>
> Obviously if the number of items and columns are static, I have no 
> problem, but how do I get a layout that appeals to people and not 
> computers if they're dynamic? This has me befuddled, I'm wondering is 
> there an algorithm for doing it or is it effectively a Turing Test.
>
> Thanks,
> adam
>
>
> <?php
> $s=array(74,65,112,104,112,72,32,59,45,41);
> for($i=0;$i<count($s);$i++){echo'&#'.$s[$i].';';}
> ?>
>
> _________________________________________________________________
> MSN 8 with e-mail virus protection service: 2 months FREE* 
> http://join.msn.com/?page=features/virus
>


-- 
Raditha Dissanayake.
------------------------------------------------------------------------
http://www.radinks.com/sftp/         | http://www.raditha.com/megaupload
Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader
Graphical User Inteface. Just 150 KB | with progress bar.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to