php - Fetch subcategory in fetched category -
what trying build next. fetch menu categories , display them, fetch subcategories each category special display also. hope understand want , saying. got lost in code , don't how this. here code, hope can me.
here table structure.
subcategory table
| id | subcategory_name | id_category |
category table
| id | category |
id_category connected id in category table
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-left"> <?php $pdo = connect(); $sql = "select * category"; $query = $pdo->prepare($sql); $query->execute(); $row = $query->fetchall(); foreach ($row $rs) { ?> <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php echo $rs['category'] ?><span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <?php $sql = "select subcategory.subcategory_name, subcategory.id_category, category.id, category.category subcategory inner join category subcategory.id_category = category.category"; $query = $pdo->prepare($sql); $query->execute(); $subcat = $query->fetchall(); foreach ($subcat $sub) { ?> <li><a href="#"><?php echo $sub['subcategory_name'] ?></a></li> <?php } ?> </ul> </li> <?php } ?> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="includes/logout.php">logout</a></li> </ul> </div><!-- /.navbar-collapse --> </div>
i think didn't understand inner join sql select statement. have declare relation of second table first one:
select column_list t1 inner join t2 on join_condition1 where_conditions;
join condition in want put "subcategory.id_category = category.category" part of query. maybe leave out actual clause? hope helps. here
edit: proposing pointed rewrite complete code in 1 query. code simplified using captured variable first sql query:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-left"> <?php $pdo = connect(); $sql = "select * category"; $query = $pdo->prepare($sql); $query->execute(); $row = $query->fetchall(pdo::fetch_assoc); foreach ($row $rs) { ?> <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php echo $rs['category'] ?><span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <?php $sql = "select subcategory_name, id_category subcategory id_category = '".$rs['id']."'"; $query = $pdo->prepare($sql); $query->execute(); $subcat = $query->fetchall(pdo::fetch_assoc); foreach ($subcat $sub) { ?> <li><a href="#"><?php echo $sub['subcategory_name'] ?></a></li> <?php } ?> </ul> </li> <?php } ?> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="includes/logout.php">logout</a></li> </ul> </div><!-- /.navbar-collapse --> </div>
Comments
Post a Comment