删除最后一个菜单的分隔符

时间:2016-01-22 06:26:57

标签: php mysql phpmyadmin

我想在登录菜单后删除最后一个分隔符。 这是我的代码和输出。

              <?php
                if (!empty($topmenu) && !empty($menulist)) {
                    foreach ($topmenu as $mainparent) {
                        $arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
                        if (isset($mainparent->children) && !empty($mainparent->children)) {
                            echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>';

                            echo '</li>';
                            echo '<li> | </li>';
                        } else {
                            echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>';
                            echo '<li> | </li>';
                        }
                    }
                }
                ?>

此代码的结果是

Home | Register | Login |

我想在登录菜单后删除最后一个分隔符。 我想要这样的结果。

Home | Register | Login

4 个答案:

答案 0 :(得分:1)

您可以按照以下示例:

<?
$array = array('One','Two','Three'); // your array

$count = count($array); // check the array count
$i = 1; // use incremental
foreach ($array as $value) {
    $separator = ($i == $count ? '' : '|'); // compare if last index use empty else separator 
    echo $value. $separator; // print separator with value
    $i++; // +1 in every iteration. 
}
?>

<强>结果:

One|Two|Three

更新1:

您的代码示例

<?
if (!empty($topmenu) && !empty($menulist)) {

    $count = count($topmenu);
    $i = 1;
    foreach ($topmenu as $mainparent) {
        $arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
        if (isset($mainparent->children) && !empty($mainparent->children)) {
            echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>';

            echo '</li>';            
        } else {
            echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>';
            //echo '<li> | </li>';
        }
        if($i == $count ? '' : '<li> | </li>');
        $i++;
    }
}
?>

旁注:

我不确定$topmenu支票count($topmenu);,如果你得到它将起作用的计数。试试吧。

答案 1 :(得分:1)

试试这个

<?php
                if (!empty($topmenu) && !empty($menulist)) {
                   $count = count($topmenu);
                   $i = 1;
                    foreach ($topmenu as $mainparent) {

                        $arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
                        if (isset($mainparent->children) && !empty($mainparent->children)) {

                            echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>';

                            echo '</li>';
                            if($count != $i)  
                            echo '<li> | </li>';
                        } else {
                            echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>';
                            if($count != $i)
                            echo '<li> | </li>';
                        }
                     $i++;
                    }
                }
                ?>

答案 2 :(得分:0)

您可以使用implode方法收集列表项并对其进行连接:

<?php
    if (!empty($topmenu) && !empty($menulist)) {

        $listItems = array();
        foreach ($topmenu as $mainparent) {
            $arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
            if (isset($mainparent->children) && !empty($mainparent->children)) {
                $listItems[] = '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a></li>';
            } else {
                $listItems[] = '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>';
            }
        }

        echo implode('<li> | </li>', $listItems);
    }
?>

答案 3 :(得分:0)

你可以做这样的事情

<?php
    if (!empty($topmenu) && !empty($menulist)) {
        foreach ($topmenu as $key => $mainparent) {
            $arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
            if (isset($mainparent->children) && !empty($mainparent->children)) {
                echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>';

                echo '</li>';
                //echo '<li> | </li>';
            } else {
                echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>';
                //echo '<li> | </li>';
            }

            end($topmenu);
            if ($key !== key($topmenu)) {
                echo '<li> | </li>';
            } 

        }
    }
?>