0
votes

I am having some problem in retrieving sub categories from mysql database.I want to display the sub-categeories for the parent categories.I am able to get only the last sub category of a main category. The first sub-categories are not displaying **. In my table **i have category_id and category_parent_id.where category_parent_id will be '0' for parent category. .Thanks in advance

 <ul class="betterList">
 <?php 
        $con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error());
               mysql_select_db("DB",$con);
               $result=mysql_query("select * from table ")or die("No table available with this name"."<br/><br/>".mysql_error());
             while($row=mysql_fetch_array($result))
               {
                $parent_id=$row['category_parent_id'];
                $category_id=$row['category_id'];
                if($parent_id==0)

                {
?>
                <li>
                <?php echo $row['category_id'].$row['name_en-GB'];

                $result1=mysql_query("select * from table where category_parent_id=".$category_id)or die("No data available with this name"."<br/><br/>".mysql_error());
                 echo  $num_row    = mysql_num_rows($result1);

                    if($num_row>0) {
                        for($i=0;$i<$num_row;$i++)
                        {
                             while($row1=mysql_fetch_array($result1))
                             {

                  ?>
                                     <ul style="margin:0px;padding:0;">
                                        <li><?php echo $row1['name_en-GB']?></li>
                                     </ul>
                                     <?php
                             }
                        }
                    }

                ?>


                </li>

                <?php } ?>



    <?php }?> 
 </ul>

when i remove <li> tag which is at the end and keep it after at the end of in while i could display all the sub-catgeories but the css is not applying for that. Some thing is going wrong there but i couldn't figuer it out

5
What is the result for $num_row?Nes
It is giving the number of sub catgeories for that main catgeory.user2083041
Yes i'm asking the no of rows countNes
First you start with minimum so please add a break keyword in first while. I mean the categories whileNes
What this shows in browser echo $num_row = mysql_num_rows($result1);Nes

5 Answers

1
votes

Remove below and try again:

for($i=0;$i<$num_row;$i++)
{
0
votes

Wow ! o_O You're using old mysql_* functions ...

You wrote : for($i=0;$i<$num_row;$i++)

And After : while($row1=mysql_fetch_array($result1))

Both of these instructions are looping on each rows you got with this query.

Remove all of that:

echo  $num_row    = mysql_num_rows($result1);

if($num_row>0) {
    for($i=0;$i<$num_row;$i++) {

Cause this is useless.

The only important thing to loop on your results is while($row1=mysql_fetch_array($result1))

You can also replace mysql_fetch_array() by mysql_fetch_assoc() that is lighter.

Your code will be optimizable but this should solve your problem.

0
votes

Instead of doing nested loops, get everything with a join:

SELECT t1.category_id parent, t1.`name_en-GB` parent_name,
       t2.category_id child, t2.`name_en-GB` child_name
FROM table t1
JOIN table t2 ON t2.parent_category_id = t1.category_id
WHERE t1.parent_category_id = 0

Then your loop would be:

$last_parent = null;
$first_cat = true;
while ($row = mysql_fetch_assoc($result)) {
    if ($row['parent'] != $last_parent) {
        $last_parent = $row['parent'];
        if (!$first_cat) { // Close out the last UL and LI
            echo '</ul></li>';
        } else {
            $first_cat = false;
        }
        echo '<li>' . $row['parent'] . $row['parent_name'];
        echo '<ul style="margin:0px;padding:0;">';
    }
    echo '<li>' . $row['child_name'] . </li>
}
if (!$first_cat) {
    echo '</ul></li>';
}

You had too many nested loops in your code: you had both a for and while loop that were both trying to loop over the rows of the inner query. Also, you were putting each child into its own <ul>, which is probably not what you wanted.

0
votes

Just try whether this solutions work for u, if it works adjust your code accordingly

        $result = mysql_query("select * from table WHERE category_parent_id = 0 ");

        while($row=mysql_fetch_array($result)) {

            $parent_id = $row['category_parent_id'];
            $query = mysql_query("select * from table where category_parent_id = {$parent_id}");

            while($sub_cats=mysql_fetch_array($query)) {
                echo '<pre>'.print_r($sub_cats).'</pre>';
            }

        } 
0
votes

just by adding internal <ul> before while loop i could get subcategories.

        <?php 
          echo "<ul>";
          while($row1=mysql_fetch_array($result1))
             {
         ?>
               <li><?php echo $row1['name_en-GB']?></li>
                <?php
             }
                echo " </ul>";