</tr> after every two <td>- how do I do it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

</tr> after every two <td>- how do I do it?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: </tr> after every two <td>- how do I do it?

Post 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>
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: </tr> after every two <td>- how do I do it?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply