用于列出关联数组的格式输出

时间:2012-08-31 19:41:30

标签: php arrays

我在列出复杂数组的内容时遇到了一些麻烦。该数组包含网站的部分和子部分。

HTML代码如下所示:

<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>&nbsp;&nbsp;&nbsp;&nbsp;|_&nbsp;&nbsp;&nbsp;&nbsp;<?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>

输出结果为:http://cl.ly/image/240C1S2A3J3J

现在我的问题是:如何才能正确编号,如何才能获得交替的行颜色?

1 个答案:

答案 0 :(得分:1)

您的嵌套循环导致问题。 。 。需要单独的变量来跟踪行。 。 。

<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>&nbsp;&nbsp;&nbsp;&nbsp;|_&nbsp;&nbsp;&nbsp;&nbsp;<?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>