Reputation: 8783
I'm having some trouble with listing the contents of a complex array. The array holds sections and subsections for a website.
The HTML code looks like this:
<table class="listing">
<thead>
<tr>
<th class="checkable">#</th>
<th>Name</th>
<th>Type</th>
<th>Date added</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($sections); $i++): ?>
<tr <?php if ($i % 2 == 0): ?>class="alt"<?php endif; ?>>
<td class="checkable"><?php echo $i+1; ?></td>
<td><?php echo stripslashes($sections[$i]['name']); ?></td>
<td><?php echo $sections[$i]['type_name']; ?></td>
<td><?php echo $sections[$i]['date_added']; ?></td>
<td>
<ul class="listing-actions">
<li class="default">Actions</li>
<li>
<a href="edit-section.php?section_id=<?php echo $sections[$i]['section_id']; ?>">Edit</a>
<form action="delete-section.php" method="post" data-prompt="Are you sure you want to delete this element?" >
<input type="hidden" name="section_id" value="<?php echo $sections[$i]['section_id']; ?>" />
<a href="#">Delete</a>
</form>
</li>
</ul>
</td>
</tr>
<?php if (count($sections[$i]['siblings']) > 0): ?>
<?php for ($j = 0; $j < count($sections[$i]['siblings']); $j++): ?>
<tr <?php if ($i % 2 != 0): ?>class="alt"<?php endif; ?>>
<td class="checkable"><?php echo $j+1; ?></td>
<td> |_ <?php echo stripslashes($sections[$i]['siblings'][$j]['name']); ?></td>
<td><?php echo $sections[$i]['siblings'][$j]['type_name']; ?></td>
<td><?php echo $sections[$i]['siblings'][$j]['date_added']; ?></td>
<td>
<ul class="listing-actions">
<li class="default">Actions</li>
<li>
<a href="edit-section.php?section_id=<?php echo $sections[$i]['siblings'][$j]['section_id']; ?>">Edit</a>
<form action="delete-section.php" method="post" data-prompt="Are you sure you want to delete this element?" >
<input type="hidden" name="section_id" value="<?php echo $sections[$i]['siblings'][$j]['section_id']; ?>" />
<a href="#">Delete</a>
</form>
</li>
</ul>
</td>
</tr>
<?php endfor; ?>
<?php endif; ?>
<?php endfor; ?>
</tbody>
</table>
And the output is this: http://cl.ly/image/240C1S2A3J3J
Now here's my question: how can I get the numbering right and how can I get that alternating row color right?
Upvotes: 0
Views: 128
Reputation: 6356
Your nested loops were causing issues . . . separate variable to keep track of the row is needed . . .
<table class="listing">
<thead>
<tr>
<th class="checkable">#</th>
<th>Name</th>
<th>Type</th>
<th>Date added</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php
$rowNum = 0;
for ($i = 0; $i < count($sections); $i++):
$rowNum++;
?>
<tr <?php if ($rowNum % 2 == 0): ?>class="alt"<?php endif; ?>>
<td class="checkable"><?php echo $rowNum; ?></td>
<td><?php echo stripslashes($sections[$i]['name']); ?></td>
<td><?php echo $sections[$i]['type_name']; ?></td>
<td><?php echo $sections[$i]['date_added']; ?></td>
<td>
<ul class="listing-actions">
<li class="default">Actions</li>
<li>
<a href="edit-section.php?section_id=<?php echo $sections[$i]['section_id']; ?>">Edit</a>
<form action="delete-section.php" method="post" data-prompt="Are you sure you want to delete this element?" >
<input type="hidden" name="section_id" value="<?php echo $sections[$i]['section_id']; ?>" />
<a href="#">Delete</a>
</form>
</li>
</ul>
</td>
</tr>
<?php if (count($sections[$i]['siblings']) > 0): ?>
<?php
for ($j = 0; $j < count($sections[$i]['siblings']); $j++):
$rowNum++;
?>
<tr <?php if ($rowNum % 2 == 0): ?>class="alt"<?php endif; ?>>
<td class="checkable"><?php echo $rowNum; ?></td>
<td> |_ <?php echo stripslashes($sections[$i]['siblings'][$j]['name']); ?></td>
<td><?php echo $sections[$i]['siblings'][$j]['type_name']; ?></td>
<td><?php echo $sections[$i]['siblings'][$j]['date_added']; ?></td>
<td>
<ul class="listing-actions">
<li class="default">Actions</li>
<li>
<a href="edit-section.php?section_id=<?php echo $sections[$i]['siblings'][$j]['section_id']; ?>">Edit</a>
<form action="delete-section.php" method="post" data-prompt="Are you sure you want to delete this element?" >
<input type="hidden" name="section_id" value="<?php echo $sections[$i]['siblings'][$j]['section_id']; ?>" />
<a href="#">Delete</a>
</form>
</li>
</ul>
</td>
</tr>
<?php endfor; ?>
<?php endif; ?>
<?php endfor; ?>
</tbody>
</table>
Upvotes: 1