php - Nested dropdown with dynamic data -


i want create drop down list showing data 2 tables. have category table , sub_category table. have written following code, showing me items category table, , blank space after each category. want in drop down list.

required output

i have written following code, it's not giving me required output. kindly check it.

 <?php include 'connect.php';  echo "<select name='category'>";  $select_query=          'select * category'; $select_query_run =     mysql_query($select_query);    $sub_category_query="select * sub_categories id='".$select_query_array['category_id']."'";        $sub_category_query="select * sub_categories ";       $sub_query_run=         mysql_query($sub_category_query);   while ($select_query_array=   mysql_fetch_array($select_query_run) ) {       echo "<optgroup label='".$select_query_array['name']."' >".                //    $sub_category_query="select * sub_categories id='".$select_query_array['category_id']."'";                      $sub_category_query="select * sub_categories";                  $sub_query_run=         mysql_query($sub_category_query);        while   ($sub_query_run1=   mysql_fetch_array($sub_query_run) ) {           echo "<option value='".$sub_query_run1['sub_category_id'] . "' >" .                htmlspecialchars($sub_query_run1['sub_category_name']) . "</option>";      }      echo "</optgroup>";  }  echo "</br>";   $selecttag= "</br><input type='submit' value='insert product'  /></select></form>";   echo "</div></div>";   echo $selecttag;      ?> 

this work if u maintain forgien key relation between 2 tables.

echo "<select name='category'>";  $select_query=          'select * category'; $select_query_run =     mysql_query($select_query);  while ($select_query_array=   mysql_fetch_array($select_query_run) ) {       echo "<optgroup label='".$select_query_array['category_id']."' >".           htmlspecialchars($select_query_array["name"]).       $sub_category_query="select * sub_categories id='".$select_query_array['category_id']."'";       $sub_query_run=         mysql_query($sub_category_query);       while   ($sub_query_run1=   mysql_fetch_array($select_query_run) ) {           echo "<option value='".$sub_query_run1['sub_category_id'] . "' >" .                htmlspecialchars($sub_query_run1['sub_category_name']) . "</option>";      }      echo "</optgroup>";  }  echo "</br>";   $selecttag= "</br><input type='submit' value='insert product'  /></select></form>";   echo "</div></div>";   echo $selecttag; 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -