0

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.

1
  • i am not sure but you want like this : Apple, Samsung and Huawei not have a dropdown and for Other you want displayed on dropdown. Commented Oct 24, 2017 at 10:57

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.