I'm trying to create an html table from database records. I want the table to be 4 cells wide, so I figured if the array index of the row I'm looping through is a multiple of 4, then add a tr tag at the beginning. However, it doesn't seem to be working correctly. Can anyone help me out? (This is CodeIgniter, so the echo anchor, etc. just creates an a href tag.)
<table width="80%" border="1">
<tr> <!-- create initial tr tag, since we haven't started the loop yet -->
<?php foreach($projects as $index=>$project) : ?>
<?php echo ((($index + 1) % 4 == 0) ? '<tr>' : ''); ?>
<td>
<?php echo anchor('project/view/'.$project->id, $project->project_name, 'title='.$project->project_name); ?>
</td>
<?php echo ((($index + 1) % 4 == 0) ? '</tr>' : ''); ?>
<?php endforeach; ?>
</table>
The $index + 1 is because dividing by 0 (where the array index starts) causes an error. I echoed out the values of $index+1 on each row, and if I have 5 rows in my table I get 1, 2, 3, 4, 5. But the line that should create the tr tag if $index+1 does not divide evenly by 4 isn't evaluating; I'm getting a single row table that just keeps getting wider.
Here's what I'd expect to see as rendered code:
<table width="80%" border="1">
<tr>
<td><a href="http://localhost/ignite/index.php/project/view/1" title=Basil's Beatnik Turtle>Basil's Beatnik Turtle</a></td>
<td><a href="http://localhost/ignite/index.php/project/view/2" title=Mr. Werewolf Genes>Mr. Werewolf Genes</a></td>
<td><a href="http://localhost/ignite/index.php/project/view/3" title=Romeo+Juliet>Romeo+Juliet</a></td>
<td><a href="http://localhost/ignite/index.php/project/view/4" title=Basic Hat>Basic Hat</a></td>
</tr>
<tr>
<!-- I'm not trying to auto-generate empty cells to fill in a final row that contains fewer than 4 at this point, although it would be nice -->
<td><a href="http://localhost/ignite/index.php/project/view/5" title=Flutterby Hat>Flutterby Hat</a></td>
</tr>
</table>
Here's what I'm actually getting:
<table width="80%" border="1">
<tr>
<td><a href="http://localhost/ignite/index.php/project/view/1" title=Basil's Beatnik Turtle>Basil's Beatnik Turtle</a></td>
<td><a href="http://localhost/ignite/index.php/project/view/2" title=Mr. Werewolf Genes>Mr. Werewolf Genes</a></td>
<td><a href="http://localhost/ignite/index.php/project/view/3" title=Romeo+Juliet>Romeo+Juliet</a></td>
<!-- note the lack of closing </tr> tag on previous chunk - plus it's only 3 cells, not 4 -->
<tr>
<td><a href="http://localhost/ignite/index.php/project/view/4" title=Basic Hat>Basic Hat</a></td>
</tr>
<!-- this previous chunk has both <tr> and </tr>, but only contains one <td> -->
<td><a href="http://localhost/ignite/index.php/project/view/5" title=Flutterby Hat>Flutterby Hat</a></td>
<!-- the previous chunk is missing both <tr> and </tr> tags, and is only a single cell -->
</table>
Basil's Beatnik Turtle
Mr. Werewolf Genes
Romeo+Juliet
tag on previous chunk - plus it's only 3 cells, not 4 -->
Basic Hat
and , but only contains one -->
Flutterby Hat
and tags, and is only a single cell -->
Where am I going wrong?
print_r(projects)o/p ? Are you sure the$indexis a number?