使用foreach循环显示菜单项中数组的数据

时间:2015-11-03 13:49:16

标签: php html

我有一个菜单我想用joomla制作动态(所以我可以在某个类别下添加一篇文章,该类别是顶级菜单级别,该文章显示在该类别的下拉列表中。)

这是我以前尝试过的一些基本信息。

输出正确数据的查询:

// Het bedrijf
$bedrijf                = "SELECT * FROM `lb_content` WHERE catid = 26 and state = 1";
$bedrijfcon             = $conn->query($bedrijf);
$bedrijfcr              = array();
while ($bedrijfcr[]     = $bedrijfcon->fetch_array());

26是正确id的id。

这是我想要制作动态的HTML代码:

<li class="relative f_xs_none m_xs_bottom_5"><a href="javascript:void()" onclick="javascript:goToURL('http://www.website.nl')" class="tr_delay_hover color_light tt_uppercase"><b>Het bedrijf</b></a>
    <!--sub menu-->
    <div class="sub_menu_wrap top_arrow d_xs_none type_2 tr_all_hover clearfix r_corners">
        <ul class="sub_menu">
            <li><a class="color_dark tr_delay_hover" href="home/wie-zijn-wij">Wie zijn wij?</a></li>
            <li><a class="color_dark tr_delay_hover" href="home/onze-showroom">Onze showroom</a></li>
            <li><a class="color_dark tr_delay_hover" href="home/het-bedrijf/de-beste-keus">De beste keus</a></li>
            <li><a class="color_dark tr_delay_hover" href="home/diensten/leveren-producten-voor-doe-het-zelf">Leveren producten voor doe-het-zelf</a></li>
            <li><a class="color_dark tr_delay_hover" href="home/diensten/informatie-voor-aannemers">Informatie voor aannemers</a></li>
            <li><a class="color_dark tr_delay_hover" href="home/diensten/informatie-voor-architecten">Informatie voor architecten</a></li>
        </ul>
    </div>
</li>

我现在的方法是上面的查询是在这个文件中包含的另一个文件中,这样查询就可以使用了。

然后我制作了这段代码:

<? 

$i = 1;
$arraylist = array();
    foreach($bedrijfcr as $menu1)
    {
        if(!in_array($menu1['id'],$arraylist) && $menu1['id'] != '')
        {
                $arraylist[] = $menu1['id'];

                $menuitem1 = 
                            '<li class="relative f_xs_none m_xs_bottom_5"><a href="http://vanroonenzoon.nl/" class="tr_delay_hover color_light tt_uppercase"><b>'.$menu1['title'].'</b></a>
                            <div class="sub_menu_wrap top_arrow d_xs_none type_2 tr_all_hover clearfix r_corners">
                            <ul class="sub_menu">';

                $sub1 = "SELECT * FROM `lb_content` WHERE catid = 26 order by created DESC";
                $subcon1            = $conn->query($sub1);
                $subcr1             = array();
                while ($subcr1[]    = $subcon1->fetch_array());

        }

        $i++;

        foreach($subcr1 as $submenu1)
        {
            $menuitem1 = '<li><a class="color_dark tr_delay_hover" href="'.GetSubNaam($submenu1['catid']).'/'.$submenu1['alias'].'">'.$submenu1['title'].'</a></li>';
        }

        $menuitem1 = '</ul>
        </div>
        </li>';
    }
    echo = $menuitem1;

?>

它没有给出任何错误,但也没有给出正确的数据。有谁知道我做错了什么?

提前致谢。

感谢您的回答。

这是我现在得到的输出。它循环遍布这个部分并不是我想要的。他循环所有$ menuitem1,而我只希望它循环$ subcr1 foreach中的部分。

<li class="relative f_xs_none m_xs_bottom_5"><a href="javascript:void()" onclick="javascript:goToURL(http://www.vanroonenzoon.nl)" class="tr_delay_hover color_light tt_uppercase"><b>Wie zijn wij</b></a>
    <div class="sub_menu_wrap top_arrow d_xs_none type_2 tr_all_hover clearfix r_corners">
    <ul class="sub_menu">
    <li><a class="color_dark tr_delay_hover" href="/"></a></li></ul>
    </div>
</li>

2 个答案:

答案 0 :(得分:2)

你没有连接你的字符串。看:

$menuitem1 = '<li class="relative...';

...

$menuitem1 = '<li><a class="col ...';

...

$menuitem1 = '</ul>...';

使用.=运算符追加字符串值。

答案 1 :(得分:1)

  <? 
    menuitem1 = "";
    $i = 1;
    $arraylist = array();
        foreach($bedrijfcr as $menu1)
        {
            if(!in_array($menu1['id'],$arraylist) && $menu1['id'] != '')
            {
                    $arraylist[] = $menu1['id'];

                    $menuitem1 .= 
                                '<li class="relative f_xs_none m_xs_bottom_5"><a href="http://vanroonenzoon.nl/" class="tr_delay_hover color_light tt_uppercase"><b>'.$menu1['title'].'</b></a>
                                <div class="sub_menu_wrap top_arrow d_xs_none type_2 tr_all_hover clearfix r_corners">
                                <ul class="sub_menu">';

                    $sub1 = "SELECT * FROM `lb_content` WHERE catid = 26 order by created DESC";
                    $subcon1            = $conn->query($sub1);
                    $subcr1             = array();
                    while ($subcr1[]    = $subcon1->fetch_array());

            }

            $i++;

            foreach($subcr1 as $submenu1)
            {
                $menuitem1 .= '<li><a class="color_dark tr_delay_hover" href="'.GetSubNaam($submenu1['catid']).'/'.$submenu1['alias'].'">'.$submenu1['title'].'</a></li>';
            }

            $menuitem1 .= '</ul>
            </div>
            </li>';
        }
        echo $menuitem1;

    ?>