Valdas S
Valdas S

Reputation: 455

Bootstrap dropdown with php and databases

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.

Upvotes: 0

Views: 560

Answers (1)

Friso van Dijk
Friso van Dijk

Reputation: 669

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;
}

Upvotes: 1

Related Questions