0

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

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

现在这是我的问题:我怎样才能得到正确的编号,怎样才能得到正确的交替行颜色?

4

1 回答 1

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>
于 2012-08-31T20:41:33.620 回答