Reputation: 95
I keep getting a wierd list after running this:
<?php
$query_Category = "SELECT * FROM blog_categories ORDER BY category ASC";
$getCategory = mysql_query($query_Category) or die(mysql_error());
?>
<div id="sheader" style="">Categories</div>
<div class="sbody" style="color:#000 !important;">
<?php
do {
?>
<div><?php echo $row_getCategory['category'];?></div>
<?php
$cat = $row_getCategory['cat_id'];
$query_Subcategory = "SELECT * FROM blog_subcategories WHERE primcat_id = '$cat' ORDER BY subcategory ASC";
$getSubCategory = mysql_query($query_Subcategory) or die(mysql_error());
$row_getSubCategory = mysql_fetch_assoc($getSubCategory);
$str = $row_getSubCategory['subcategory']; $subcategory = explode(',', $str);
foreach ($subcategory as $arraysubcat)
{
echo '<div>' . $arraysubcat . '</div>';
}
} while ($row_getCategory = mysql_fetch_assoc($getCategory));
?>
</div>
<?php mysql_free_result($getCategory); ?>
I have a categories table with id & category and a sub categories table with a id, subcategory, & primary category id. I run it and it displays the foreach first randomly.
Upvotes: 0
Views: 13170
Reputation: 19979
There are times where it's ok to have a foreach
within a while
, but this isn't one of them. First off you are doing a do ... while
loop which means regardless if a row is found or not, you will step through all the code, so if no row exists in database, you will probably get undefined index
errors. Secondly you are executing the subcategory query for every iteration of the outer while loop, which means that if your query returns 100 rows, you are issuing 100 queries, not good.
Your best bet is to create a join between the two tables and step through the rows in one loop, preferably a while loop (vs a do ... while
loop).
Something like this:
// Made some assumptions with the query, as no schema was posted
// But should give you a starting point
$sql =
'SELECT bc.*, GROUP_CONCAT(bsc.subcategory) AS sub_categories ' .
'FROM blog_categories AS BC INNER JOIN blog_subcategories AS bsc ON bsc.primcat_id = bc.id ' .
'GROUP BY bc.id ' .
'ORDER BY bc.category ASC';
Now you only have a single query which retrieved all categories and their associated sub-categories, now you can step through them with a single while loop:
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
<div>
<?php echo $row['category']; ?>
</div>
// To get at subcategories you can do this
$subCategories = array_map('trim', explode(',', $row['sub_categories']));
// And sort them ASC
sort($subCategories);
foreach ($subCategories as $subCategory) {
echo '<div>' . $subCategory . '</div>';
}
}
Upvotes: 2
Reputation: 37065
You don't need to do a separate subquery for each category. Just do a join. Try this:
$db = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$get_categories = $db -> query("SELECT category, subcategory
FROM blog_categories, blog_subcategories
WHERE primcat_id = cat_id
ORDER BY category ASC, subcategory ASC");
echo "<h1>Categories</h1>";
echo "<ul>";
while($row = $get_categories -> fetch_assoc()) {
echo "<li>" . $row['category'];
if($row['subcategory']) {
echo "<ul>";
foreach(explode(",",$row['subcategory']) as $subcategory ) {
echo "<li>$subcategory</li>";
}
echo "</ul>";
}
echo "</li>";
}
echo "</ul>";
Upvotes: 2
Reputation: 1934
My head really hurts from your code, but i believe your problem is here:
$cat = $row_getCategory['cat_id'];
$row_getCategory is fetched at the end of your code, so this code just can not work.
I believe you want to do something like this
while ($row_getCategory = mysql_fetch_assoc($getCategory))
{
$cat = $row_getCategory['cat_id'];
$query_Subcategory = "SELECT * FROM blog_subcategories WHERE primcat_id = '$cat' ORDER BY subcategory ASC";
$getSubCategory = mysql_query($query_Subcategory) or die(mysql_error());
$row_getSubCategory = mysql_fetch_assoc($getSubCategory);
$str = $row_getSubCategory['subcategory'];
$subcategory = explode(',', $str);
foreach ($subcategory as $arraysubcat) {
echo '<div>' . $arraysubcat . '</div>';
}
}
}
Upvotes: 2
Reputation: 1268
I think you should not use do while
to retrieve the data from the database.You should rather use while
loop because, do while
runs the loops once before checking the condition.So at the first loop, you will not get any data in the echo $row_getCategory['category'];
variable. So please try using the following code :
<?php
while ($row_getCategory = mysql_fetch_assoc($getCategory)){
?>
<div>
<?php
echo $row_getCategory['category'];
?>
</div>
<?php
$cat = $row_getCategory['cat_id'];
$query_Subcategory = "SELECT * FROM blog_subcategories WHERE primcat_id = '$cat' ORDER BY subcategory ASC";
$getSubCategory = mysql_query($query_Subcategory) or die(mysql_error());
$row_getSubCategory = mysql_fetch_assoc($getSubCategory);
$str = $row_getSubCategory['subcategory'];
$subcategory = explode(',', $str);
foreach ($subcategory as $arraysubcat) {
echo '<div>' . $arraysubcat . '</div>';
}
?>
<?php
}
?>
Upvotes: 1