0

I have a query, it is outputting the results into one column, I would like to have the results shown in two columns. Here is the code:

$i = 0; 
$columnCount = 2;
echo "<table>";
    while($row = $result->fetch_assoc()) 
    { 
        $newRow = ( $i % $columnCount == 0 );      
        if( $newRow ) {         
        echo '<tr>';      
        }      
        echo '<td>' . $row['text'] . '</td>';      
        if( $newRow ) {           
        echo '</tr>';      
        }      
        $i++;  
    }
echo "</table>";

Thanks Again,

1
  • Do you want the data to go vertical first, or horizontal first? Commented Apr 20, 2011 at 18:49

2 Answers 2

1

You are only defining a single column in your script. Change:

echo '<td>' . $row['text'] . '</td>';      

to:

echo '<td>' . $row['text'] . '</td><td>' . $row['othercolumn'] . '</td>';

Also I don't think your tests for defined a new row are necessary unless you want to alternate tr definitions so the following will work:

echo '<tr><td>' . $row['text'] . '</td><td>' . $row['othercolumn'] . '</td></tr>';

or if you really want different rows do:

while($row = $result->fetch_assoc()) 
    if ( $i % 2 == 0 ) {
        echo '<tr class="red">';
    } else {
        echo '<tr class="blue">';
    }
    echo '<td>' . $row['text'] . '</td><td>' . $row['othercolumn'] . '</td></tr>';
}
Sign up to request clarification or add additional context in comments.

Comments

0
$i = 0; 
$columnCount = 2;
echo "<table>";
    while($row = $result->fetch_assoc()) 
    { 

        if( $i % $columnCount == 0 ) {   
           if( $i != 0 ){      
             echo '</tr><tr>'; 
           else{
              echo '<tr>';
           }     
        }

        echo '<td>' . $row['text'] . '</td>';      

        $i++;  
    }
echo "</table>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.