Php 列出关联数组的格式输出
我在列出复杂数组的内容时遇到了一些问题。该数组包含网站的节和子节 HTML代码如下所示:Php 列出关联数组的格式输出,php,arrays,Php,Arrays,我在列出复杂数组的内容时遇到了一些问题。该数组包含网站的节和子节 HTML代码如下所示: <table class="listing"> <thead> <tr> <th class="checkable">#</th> <th>Name</th>
<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>
#
名称
类型
添加日期
行动
>
- 操作
-
您的嵌套循环导致了问题。需要单独的变量来跟踪行
<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>
#
名称
类型
添加日期
行动
>
- 操作
-
对编号列表使用
。您希望使用哪种编号和突出显示?@ernie,我在谈论第一列中的数字。我希望他们都井然有序。通过突出显示,我的意思是有一个灰色的行,然后是白色的行,然后是灰色的行,然后是白色的行,依此类推。现在,编号和突出显示都不正确。@psyche嵌套元素的顺序应该不同,还是在您的示例中应该从1到12?@ernie,它们应该从1到12。@psyche哎呀,忘记增加子部分的行了。更新的代码。