0
votes

I want to create a list which contains categories and on hover the category i need to display sub categories.I am able to display the parent categories in list.but couldn't understand how to get sub categories for that. In my table i have category_id, parent_id column and some other columns. If parent_id is '0' it is main category and for subcategories it contains there category_id.So now i need to display sub-categories for main categories.I could not understand how to proceed.Can any one give advice.

<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 order by `name_en-GB`")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['name_en-GB'];?></li>
               <?php }
                    ?>


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

   <?php

           }?>   
</ul>
2

2 Answers

0
votes

used this code

 <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 where parent_id=0 order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error());
  while($row=mysql_fetch_array($result))
  {
    $category_id=$row['category_id'];
                ?>
                <li><?php echo $row['name_en-GB']; ?></li>

                <?php 
                $result1=mysql_query("select * from table where category_id=".$category_id." order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error());
                $num_row    = mysql_num_rows($result1);
                if($num_row>0) {
                    ?>
                     <ul id="internal" style=" margin:0px; padding:0;">
                    <?php                   
                }
                while($row1=mysql_fetch_array($result1))
                {?>
                    <li><?php echo $row1['name_en-GB']; ?></li>
                <?php
                }
                if($num_row>0) {
                    ?>
                     </ul>
                    <?php                   
                }
  }
                    ?>
0
votes

You miss some codes use good Editors. Check my comment lines

<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 order by `name_en-GB`")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['name_en-GB'];?> // you miss this close tag

                </li>
                <?php // you miss this Open tag
                }
                    ?>


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

   <?php

           }?>   
</ul>