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.
I think you should not use
do whileto retrieve the data from the database.You should rather usewhileloop because,do whileruns the loops once before checking the condition.So at the first loop, you will not get any data in theecho $row_getCategory['category'];variable. So please try using the following code :