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 about http://www.ipaidabribenaija.com/newsletter.php Thanks.
<?php
$conn = mysql_connect("localhost", "rppt", "peep") or die(mysql_error());
mysql_select_db('news', $conn) or die(mysql_error());
$query = mysql_query ("Select i.nid, LEFT(i.fulltext, 350), UPPER(i.title),
LOWER(c.name) from nl i JOIN jos_k2_categories c ON c.id=i.catid ORDER BY i.id LIMIT 0, 16") or die ('Error');
$href = "http://www.ipaidabribenaija.com/index.php";
?>
<table width="500px" align="center">
<tbody>
<tr>
<td width="400px" height="344px" valign="top">
<table cellspacing="5">
<tbody>
<?php
while(list($id, $fulltext, $title, $name)=mysql_fetch_array($query))
{
$i = 0;
?>
<?php
$replacename = eregi_replace(" ", "-", $name);
?>
<tr>
<td height="34"> </td>
</tr>
<?php
if($i%2 == 0)
{
?>
<tr>
<tr>
<td height="34">
<font color="#FF0000" size="+2"><strong><?php echo $title; ?></strong></font> </td>
</tr>
<tr>
<td height="34"><p><?php echo $fulltext; ?>...</p>
<p><a href="<?php echo $href ."/". $replacename ."/item/". $id; ?>">read more...</a></p>
</td>
</tr>
<?php
}
else{
?>
<tr>
<td height="34"><font color="#FF0000" size="+2"><strong><?php echo $title; ?></strong></font> </td>
</tr>
<tr>
<td height="34"><p><?php echo $fulltext; ?>...</p>
<p><a href="<?php echo $href ."/". $replacename ."/item/". $id; ?>">read more...</a></p></td>
</tr>
<?php
}
}
?>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
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>;
Use rowspan and colspan html property
<tr>
<td>A</td>
<td colspan="2"> </td>
</tr>
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> </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.