what i am trying to do is create a list of categories that are pulled from the database, and then list underneath each of those categories the sub-categories that are relevant.
so far i have got:
<?php
// this query lets us know that we are looking for matches that equal 2, which points to the subject of vehicles.
// from this query i am able to list all categories that are relevant to vehicles.
$getCategoriesQuery = $db->query("SELECT * FROM item_cat WHERE sub_id = '2'"); //2 = vehicles
?>
<?php
while($row = $getCategoriesQuery->fetch()){
echo '<ul>'.$row['category'].'';
}
?>
which displays:
- motor vehicles
- railed vehicles
- aircraft
now, when i add the following while loop to the one above:
<?php
while($row = $getCategoriesQuery->fetch()){
echo '<ul>'.$row['category'].'';
// this has been added to try and get the sub categories
$getSubCatQuery = $db->query("SELECT * FROM item_cat, item_sub_cat
WHERE item_cat.cat_id = item_sub_cat.cat_id "); // cat_id
while($row = $getSubCatQuery->fetch()){
echo '<li><a href="vehicles.php?p='.$row['sub_category'].'" >'.$row['sub_category'].'</a></li>';
}
echo '</ul>';
}
?>
i get:
- motor vehicles
- cars
- motorbikes
- buses
- trucks
- trains
- planes
-railed vehicles
- cars
- motorbikes
- buses
- trucks
- trains
- planes
-aircraft
- cars
- motorbikes
- buses
- trucks
- trains
- planes
when what i am wanting to do is:
-motor vehicles
- cars
- motorbikes
- buses
- trucks
-railed vehicles
- trains
-aircraft
- planes
i have tried joining the queries to begin with, but no joy, so i split into 2 queries to try and see things a bit better. i have changed "$row" to "$row2" on the second while loop, but that then gives me an undefined index error for "row2['sub_category']"
i have also tried using a "foreach loop" instead of the second "while loop":
<?php
// this query lets us know that we are looking for matches that equal 2, which points to vehicles.
$getCategoriesQuery = $db->query("SELECT * FROM item_cat WHERE sub_id = '2'"); //vehicles
?>
<?php
while($row = $getCategoriesQuery->fetch()){
echo '<ul>'.$row['category'].'';
$getSubCatQuery = $db->query("SELECT * FROM item_cat, item_sub_cat
WHERE item_cat.cat_id = item_sub_cat.cat_id "); // cat_id
foreach ($getSubCatQuery->fetchAll () as $row2) {
echo '<li><a href="vehicles.php?p='.$row2['sub_category'].'" >'.$row2['sub_category'].'</a></li>';
}
echo '</ul>';
}
?>
i have spent a whole day trying to resolve this, but to no joy - just headache. could anyone please advise where i am going wrong... what am i missing? - i am using PDO also if that is not clear.
thanks in advance!
--UPDATED-- i have changed the while loops part to:
<?php
while($row = $getCategoriesQuery->fetch()){
echo '<ul>'.$row['category'].'';
$getSubCatQuery = $db->query("SELECT * FROM item_sub_cat, item_cat WHERE item_sub_cat.cat_id ='".$row['cat_id']."' ");
while($sub_row = $getSubCatQuery->fetch()){
echo '<li><a href="vehicles.php?p='.$sub_row['sub_category'].'" >'.$sub_row['sub_category'].'</a></li>';
}
echo '</ul>';
}
?>
and the output is displaying the things in the correct categories.... however it is duplicating each item by the total records i have in the database:
- motor vehicles
- cars
- motorcycles
- trucks
- buses
- cars
- motorcycles
- trucks
- buses
- cars
- motorcycles
- trucks
- buses
- cars
- motorcycles
- trucks
- buses
- cars
- motorcycles
- trucks
- buses
- railed vehicles
- trains
- trains
- trains
- trains
- trains
i have tried by adding "LIMIT 1" and "GROUP BY" to the query, but then that only displays 1 item per category.
- motor vehicles
- cars
- railed vehicles
- trains
GROUP BYand NOTlimit, this should retrieve the results.