dqwyghl0649
2017-10-24 10:34
采纳率: 0%
浏览 69
已采纳

使用php和数据库的Bootstrap下拉列表

I have a problem with drop down menu using twitter bootstrap. In my database I have table called categories. In it I have 8 records:

ID | Category | parent
1  | Apple    | 0
2  | Samsung  | 0
3  | Huawei   | 0
4  | Others   | 0
5  | Sony     | 4
6  | LG       | 4
7  | Lenovo   | 4
8  | Nokia    | 4

By using this data I have created a navbar. However I have this problem: Since 'others' have parent, they appear in their parents dropdown. Apple, Samsung and Huawei does not have any children so I would like them to not have a dropdown at at all. However my code generates an empty dropdown for these categories.

<body>
<?php
    $sql = "SELECT * FROM categories WHERE parent = 0";
    $pquery = $db->query($sql);
  ?>
  <nav class="navbar navbar-default navbar-fixed-top" id="navbar">
    <div class="container">
      <a href="/e-shop/index.php" class="navbar-brand" id="logo">Mobile e-shop</a>
      <ul class="nav navbar-nav">
        <?php while($parent = mysqli_fetch_assoc($pquery)) : ?>
        <?php
          $parent_id = $parent['id'];
          $sql2 = "SELECT * FROM categories WHERE parent = '$parent_id' ";
          $cquery = $db->query($sql2);
        ?>

<!-- Drop down meniu -->
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" id="text"><?php echo $parent['kategorija']; ?><span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <?php while($child = mysqli_fetch_assoc($cquery)) : ?>
            <li>
            /*I assume i should do if statement or something here to check is its null? I tryied to use is_null() but it did not work for me*/
              <a href="<?=$child['adresas']; ?>">
                <?php echo $child['kategorija'] ?>
              </a>
            </li>
          <?php endwhile; ?>
          </ul>
        </li>
      <?php endwhile; ?>
      </ul>
    </div>
  </nav>

I assume i should do if statement or something to check is its null? I tryied to use is_null() but it did not work for me.

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题