Multiple Column Output

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Trying to streamline my code to output a "SELECT ALL" query into
multiple columns of the same table.  Right now it's set to put values
into certain columns based on the ID (INT, Autonumber).

However, some of the items got deleted, creating gaps in IDs - messed
everything up.  Better way?

Thank you in advance.

Re: Multiple Column Output

Use a counter while outputting the records..

The counter ($i) will take place of the recordset id's


   if($i == $column_depth_count){
     //switch columns
     $i = 1;

Hope that helps,
-- Steve

TristaSD wrote:
Quoted text here. Click to load it

Re: Multiple Column Output

Thank for the inspiration.  I used this:

    $num_across = 2; // Two column layout

    echo ("<tr>\n"); // Start the row
    while ($row = mysql_fetch_array($query)) {
        $i++; // Start the counter
        if (($i % $num_across) == 0) { // If the last column is reached
            echo "<td>$row[somename]</td></tr>\n<tr>\n"; // Close/open row
        else {
            echo "<td>$row[id]</td><td>$row[artist]</td>"; // Otherwise just
show some cells


Re: Multiple Column Output

TristaSD wrote:
Quoted text here. Click to load it
Looks good!
You may want to account for odd recordsets..

3 records in the db

| td data | td data |
| td data | ........

The code would have to be set a variable that could be read outside the  
while, basically seeing if the last $i iteration was odd, then stuff an  
empty td line at the end.

Some more code would need to be added if you wanted the $num_rows  
variable to be truly dynamic. The last iteration would have to determine  
how many empty cells there were based on the $num_rows variable.  (i.e.  
4 records on a 3 column would result in 2 empty cells)

Will keep your html pretty and not messed up w/ browsers interpreting  
strict html.

-- Steve

Site Timeline