如何停止子课程的foreach循环?

时间:2016-08-10 06:54:12

标签: php html

当我点击Linuxbasics时,如果默认页面在那里,它应该打开。我正在获取默认页面,但我的li正在循环。在我的代码中,我使用了多维数组。下面是我的代码和图像。我在Image中显示我得到两次linux基础知识: enter image description here

<ul class="sub-menu" style="border-right: 2px solid #012340;">
<?php foreach($outContentArrResults AS $keyCmsId => $valArrMenu){?>
    <?php if($valArrMenu['cPId']==0){?>
        <li class="menu-item-has-children">
            <a href="#"><?php echo isset($valArrMenu['cName']) ? $valArrMenu['cName']:'';?></a>
            <?php $slugName = isset($valArrMenu['cSName']) ? $valArrMenu['cSName']:'';
            $id =isset($valArrMenu['cId']) ? $valArrMenu['cId']:'';
            $courseChild =isset($valArrMenu['courseChild']) ? $valArrMenu['courseChild']:'';?>
        <?php foreach($courseChild AS $keyId => $courseResult){
        if($courseResult['cPId']==$id){?>
            <ul class="sub-menu" style="border-right: 2px solid #012340;border-left: 2px solid #012340;">
                <?php foreach($courseChild AS $keyId => $courseResult){?>
                <?php if($courseResult['cPId']==$id){?>
                <?php $courseSubChild =isset($courseResult['courseChild']) ? $courseResult['courseChild']:'';
                    foreach($courseSubChild AS $keyIdsss => $courseSubResult){
                    $Subid =isset($courseSubResult['cId']) ? $courseSubResult['cId']:'';
                    $SubslugName =isset($courseSubResult['cSName']) ? $courseSubResult['cSName']:'';?>
                        <li>
                            <a href="<?php echo $config['LIVE_URL'];?>courses/<?php echo isset($slugName) ? $slugName :'';?>/<?php echo isset($courseResult['cSName']) ? $courseResult['cSName']:'';?>/<?php echo $SubslugName;?>/<?php echo isset($courseSubResult['conSName']) ? $courseSubResult['conSName']:'';?>"><?php echo isset($courseResult['cName']) ? $courseResult['cName']:'';?></a>
                        </li>
                    <?php }?>
                <?php }?>
                <?php }?>
            </ul>
        <?php }?>
        <?php }?>
        </li>
    <?php }?>
<?php }?>

这是我的阵列:

  Array
 (
[0] => Array
    (
        [cId] => 1
        [cName] => Linux
        [cSName] => linux
        [isTryItEditor] => 0
        [cImg] => 
        [cCDate] => 2016-08-10 11:47:53
        [cPId] => 0
        [conSName] => 
        [conIDPage] => 
        [courseChild] => Array
            (
                [0] => Array
                    (
                        [cId] => 2
                        [cName] => Linux Basics
                        [cSName] => linux-basics
                        [isTryItEditor] => 0
                        [cImg] => 
                        [cCDate] => 2016-08-10 11:48:51
                        [cPId] => 1
                        [conSName] => 
                        [conIDPage] => 
                        [courseChild] => Array
                            (
                                [0] => Array
                                    (
                                        [cId] => 4
                                        [cName] => basic commands
                                        [cSName] => basic-commands
                                        [isTryItEditor] => 0
                                        [cImg] => 
                                        [cCDate] => 2016-08-10 11:56:41
                                        [cPId] => 2
                                        [conSName] => hii
                                        [conIDPage] => 1
                                        [courseChild] => Array
                                            (
                                            )

                                    )

                                [1] => Array
                                    (
                                        [cId] => 3
                                        [cName] => Filter commands
                                        [cSName] => filter-commands
                                        [isTryItEditor] => 0
                                        [cImg] => 
                                        [cCDate] => 2016-08-10 11:49:14
                                        [cPId] => 2
                                        [conSName] => 
                                        [conIDPage] => 
                                        [courseChild] => Array
                                            (
                                            )
                                    )
                            )
                    )
            )
        )
     )

2 个答案:

答案 0 :(得分:0)

您可以将数值存储在数组casue中,而不是它的初始状态 foreach循环导致问题

然后使用if(!in_array())仅打印不存在的值

答案 1 :(得分:0)

As&#34; BeetleJuice&#34;提到,尝试首先使用纯PHP代码测试逻辑,然后与HTML集成,这将简化工作。

关于你的代码,你的内心循环似乎有错误的数据打印。只需在代码中进行以下更改即可确定问题:而不是

<li>
                    <a href="<?php echo $config['LIVE_URL'];?>courses/<?php echo isset($slugName) ? $slugName :'';?>/<?php echo isset($courseResult['cSName']) ? $courseResult['cSName']:'';?>/<?php echo $SubslugName;?>/<?php echo isset($courseSubResult['conSName']) ? $courseSubResult['conSName']:'';?>"><?php echo isset($courseResult['cName']) ? $courseResult['cName']:'';?></a>
                </li>

                run this,

                <li>
                    <?php echo $courseResult['cId']?>
                </li>

由于每个级别的记录都有唯一的cid,因此您可以轻松找到问题。

希望这有帮助。

由于