嵌套的foreach循环菜单导航php

时间:2013-11-19 10:25:18

标签: php php-5.3

我在opencart中有以下代码

<table class="cat_lists">
                    <tr>
                        <?php foreach ($categories as $key=>$category) { ?>
                            <td>
                                <?php echo $key%2; ?>
                                <div class="c_menus">
                                    <h5><?php echo $category['name']; ?></h5>
                                    <?php if ($category['children']) { ?>
                                        <ul>
                                            <?php for ($i = 0; $i < count($category['children']);) { ?>
                                                <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
                                                <?php for (; $i < $j; $i++) { ?>
                                                    <?php if (isset($category['children'][$i])) { ?>
                                                        <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
                                                    <?php } ?>
                                                <?php } ?>
                                            <?php } ?>
                                        </ul>
                                    <?php } ?>
                                </div>
                            </td>
                        <?php } ?>
                    </tr>
                </table>

实际上,菜单的UI应如下图enter image description here

但我的循环输出为

enter image description here

如何修改循环以获得所需的结果。

修改-1

这是应该转换为动态

的html
<td>
                        <div class="c_menus">
                            <h5>Necklace</h5>
                            <ul>
                                <li><a href="">Beaded Necklaces</a></li>
                                <li><a href="">Long Necklaces</a></li>
                                <li><a href="">Pendant Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                            </ul>
                        </div> 
                        <div class="c_menus">
                            <h5>Rings</h5>
                            <ul>
                                <li><a href="">Beaded Necklaces</a></li>
                                <li><a href="">Long Necklaces</a></li>
                                <li><a href="">Pendant Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                            </ul>
                        </div> 
                    </td>
                    <td>
                        <div class="c_menus">
                            <h5>Necklace</h5>
                            <ul>
                                <li><a href="">Beaded Necklaces</a></li>
                                <li><a href="">Long Necklaces</a></li>
                                <li><a href="">Pendant Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                            </ul>
                        </div> 
                        <div class="c_menus">
                            <h5>Rings</h5>
                            <ul>
                                <li><a href="">Beaded Necklaces</a></li>
                                <li><a href="">Long Necklaces</a></li>
                                <li><a href="">Pendant Necklaces</a></li>
                                <li><a href="">Statement Necklaces</a></li>
                            </ul>
                        </div> 
                    </td>

1 个答案:

答案 0 :(得分:0)

删除此行:

<?php echo $key%2; ?>

这会删除菜单顶部的01