dty47696 2015-11-25 20:16
浏览 15
已采纳

PHP - 下拉不显示mysql中的所有项目

I am Trying to make a drop down menu, and list multiple items as well.

like for example: here is my database: enter image description here

if you see in that image you can see only one of the rows have a parent if i want multiple rows to have a parent of 'Far Far Away123' it only shows one of the items

<?php

  function build_dropdown ($parent, $pages){
  $items = "";
    foreach($pages as $page){
    // $items = "";
  if($page['parent'] == $parent){
    $items = $page;
  } // END if
    } // END foreach

    if(is_array($items)){ // If a sub 
      echo '<ul id="sub_menu" class="sub_navagation'. $items['id'] .'">';
        echo '<li>'.$items['page_name'].'</li>';
      echo '</ul>'; 
    } // END if 
  }// End Function

  $sql = "SELECT * FROM pages ORDER by item_order";
  $result = mysqli_query($db, $sql);
  confirm_query($result);

  while ($row = mysqli_fetch_assoc($result)) {
    $pages[] = $row; // Add each row to $pages array to use later
  }


  foreach($pages as $key => $page){

    if($page['parent'] == 'none'){ ?>

      <li id = "<?php echo $page['id']; ?>">
        <a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo     $page['page_title']; ?>">
          <?php echo $page['page_name']; ?>
        </a>
        <?php  ?>


<?php 
    } // END if
    build_dropdown($page['page_name'], $pages); // If there are child items     then build them out
echo "</li> ";
  } // END foreach
?>

Thanks

  • 写回答

2条回答 默认 最新

  • dongxian5735 2015-11-25 20:33
    关注

    It is because you add only one.

    You have a foreach searching all pages for subitems and remember only one of them for the latter if (is_array())

    function build_dropdown($parent, $pages)
    {
        // item is an array
        $items = array();
        foreach($pages as $page) {
            if ($page['parent'] == $parent) {
                // add an element to the array
                $items[] = $page;
            } // END if
        } // END foreach
        if ($items) {
            echo '<ul id="sub_menu" class="sub_navagation">';
            foreach ($items as $item) {
                echo '<li>'.$item['page_name'];
                build_dropdown($item['page_name'], $pages);
                echo '</li>';
            } // END foreach
            echo '</ul>'; 
        } // END if 
    }// End Function
    

    By the way, it would be better to start with the function giving build_dropdown($parent = 'none', $pages)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗