alinash33 Posted October 24, 2017 Share Posted October 24, 2017 I'm building a site with bootstrap and trying to make the menu dynamic.created a database with xampp.it seems that there is connection because it showed some errors that I fixed.But now there are no erorrs but the data doesn't show on the website. it should be navigation menu with sublinks. Here is my code: would be very grateful for your help <?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="index.php" class="navbar-brand" id="text">Kippah store</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); ?> <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" id="text"><?php echo $parent['category'];?><span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <?php while($child = mysqli_fetch_assoc($cquery));?> <li><a href="#"><?php echo $child['category'];?></a></li> </ul> </li> </ul> </div> </nav> Quote Link to comment https://forums.phpfreaks.com/topic/305441-there-is-no-errors-but-data-from-database-doesnt-show-on-the-site/ Share on other sites More sharing options...
Barand Posted October 24, 2017 Share Posted October 24, 2017 Do you have any categories where the parent_id is equal to the string "parent_id"? 1 Quote Link to comment https://forums.phpfreaks.com/topic/305441-there-is-no-errors-but-data-from-database-doesnt-show-on-the-site/#findComment-1552987 Share on other sites More sharing options...
Sepodati Posted October 24, 2017 Share Posted October 24, 2017 SELECT * FROM categories WHERE parent='parent_id' You don't see an issue with that? Quote Link to comment https://forums.phpfreaks.com/topic/305441-there-is-no-errors-but-data-from-database-doesnt-show-on-the-site/#findComment-1552994 Share on other sites More sharing options...
alinash33 Posted October 25, 2017 Author Share Posted October 25, 2017 Hi I'm a beginner so maybe I didn't understand the logic -what I have is a categories table within it I have a 3 fields : id , category, parent the id is the primary key the items in category have id(primary key) and they also have a number of parent from 0 to 4 . The category that assigned parent=0 should be the header link in the and below it the category with parent 1 2 3 4 et... so variable parent_id receives fro the parent[id] the 1,2,3,4 and from there it goes to the query of sql2 that puts in $sql 2 all the category that have parent 1,2,3,4 .I'm really beginner so maybe something is wrong with my logic.Would be very grateful for your help. Quote Link to comment https://forums.phpfreaks.com/topic/305441-there-is-no-errors-but-data-from-database-doesnt-show-on-the-site/#findComment-1553020 Share on other sites More sharing options...
Barand Posted October 25, 2017 Share Posted October 25, 2017 Your logic is fine (although it's not the most efficient way to do it). It's your implementation of that's at fault. Above you mention the variable $parent_id and using that in the second query. But you aren't doing that. As two of us have pointed out, you are searching for categories whose parent matches the literal string value "parent_id" and not the variable. The efficient way is not to run queries inside a loop. You should run a single query, using a JOIN, to get all your required data. Quote Link to comment https://forums.phpfreaks.com/topic/305441-there-is-no-errors-but-data-from-database-doesnt-show-on-the-site/#findComment-1553022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.