Skip to content Skip to sidebar Skip to footer

Displaying Two Column Html Table While Php Loop

Am creating a newsletter template that can display news in tabular form, but i want the news to be displayed in two column per row. Please check this url to see what i am talking a

Solution 1:

This will get you most of the way there:

echo '<tr>';
$i = 0;
while(...) {
    if($i > 0 and $i % 2 == 0) {
        echo '</tr><tr>';
    }
    echo '<td>My data</td>';
    $i++;
}
echo '</tr>;

Solution 2:

Use rowspan and colspan html property

 <tr>
  <td>A</td>
  <td colspan="2">&nbsp;</td>
 </tr>

Solution 3:

After you do your query you can write this:

<table>
<?php
$rows = 1;
$while($news = @mysql_fetch_array($query)){
  if($rows % 2 != 0){
    echo "<tr><td>" . $news['details'] . "</td>";
  }
  else {
   echo "<td>" . $news['details'] . "</td></tr>";
  }
  $row++;
}

if($rows % 2 != 0){echo "<td>&nbsp;</td></tr>"; }
?>
</table>

With this code you create a table an initialize a variable with 1, just for checking the number of rows.

Al the end if you have 3 news, the script will complete the row and an empty cell.


Post a Comment for "Displaying Two Column Html Table While Php Loop"