开发者

PHP MySQL Query - Array places data into one column in table , wanting two columns in the table

开发者 https://www.devze.com 2023-02-27 04:11 出处:网络
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 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 '<开发者_JAVA技巧;/tr>';      
        }      
        $i++;  
    }
echo "</table>";

Thanks Again,


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>';
}


$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>";
0

精彩评论

暂无评论...
验证码 换一张
取 消