Page 1 of 1
</tr> after every two <td>- how do I do it?
Posted: Mon May 12, 2014 4:54 am
by simonmlewis
Code: Select all
$count ++;
if ($count == 1) { echo "<tr>";}
if (($count % 2) == 0) echo "<tr bgcolor='#ffffff'>";
echo "<td><div class='submenu'><a href='/categ/$row->catid/$categreplace'>$row->catname $count</a></div></td>";
if (($count % 2) == 0) { echo "</tr>";}
I'm trying to create a page where there are two columns in a table. This is because I need to have two columns of DIVs and doing it just thru CSS won't work because of certain text wrapping.
So most secure means is by a table.
So I needs to be <tr><td>test</td><td><test</td></tr>..... and so on.
So the first one obviously needs to be shown if $count == 1, and then after every two rows.
Each way I try it, it goes wrong.
Re: </tr> after every two <td>- how do I do it?
Posted: Mon May 12, 2014 6:09 am
by Celauran
Opening row logic needs to be the opposite of closing row logic. You're starting a new row when count equals 1, and when it's even.
Code: Select all
<?php $cols = ['a','b','c','d','e','f','g','h']; $count = 1; ?>
<table>
<?php foreach ($cols as $col): ?>
<?php if ($count & 1): ?>
<tr>
<?php endif; ?>
<td><?= $col; ?></td>
<?php if (!$count & 1): ?>
</tr>
<?php endif; ?>
<?php $count++; ?>
<?php endforeach; ?>
</table>
Re: </tr> after every two <td>- how do I do it?
Posted: Mon May 12, 2014 7:54 am
by simonmlewis
Cool thanks.
Trying something now which sounds every simpler - but can I figure it out??
I need to set the background of a <div> grey, every other row.
white, grey, white, grey.
I thought it would straight forward............. and makes me feel daft not knowing how to do this.