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

使用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条回答 默认 最新

  • donglu1971 2017-10-24 12:31
    关注

    I see your issue. You're getting all the items without parent and give them the same function, which means they all get assigned a parent class.

    Basically, you need to find the parents that have children and then assign the correct class to them. Like so:

    <?php
      // Check if the child-query result is larger than 0
      if(mysqli_num_rows($cquery) > 0) {
        // Run your code for 'parents' here.
        <li class="dropdown">
        </li>
      } else {
        // Run your code for 'noparents' here.
        <li class="nodropdown">
        </li>
      }
    ?>
    

    If I were you, I would also prepare the statements in clean PHP code, so you only have to call some variables in the HTML. This makes your code much more readable. In the below example, you have the database call and the transforming code all in one place. In case you change one, you'd probably have to change the other, so it makes sense. Also, if someone else looks at your code, they understand it much quicker.

    <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 echo get_dropdown_children(); ?>
        </ul>
    <li>
    
    <?php
    function get_dropdown_children($parent['id']) {
        $sql2 = "SELECT * FROM categories WHERE parent = '$parent_id' ";
        $cquery = $db->query($sql2);
        $result = "";
    
        while($child = mysqli_fetch_assoc($cquery)) {
            $result .= "<li><a href='" . $child['adresas'] . "'>";  // Opening tags
            $result .= $child['kategorija'];                        // Content
            $result .= "</a></li>";                                 // Closing tags
        }
    
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?