0

I want to create a drop down list which is showing data from two tables. I have a CATEGORY table and a SUB_CATEGORY table. I have written the following code, but its only showing me items from CATEGORY table, and a blank space after each category. I want something like this in the drop down list.

Required Output

I have written the following code, but it's not giving me the required output. Kindly check it.

 <?php
include 'connect.php';

echo "<select name='category'>";

$select_query=          'Select * from category';
$select_query_run =     mysql_query($select_query);

  $sub_category_query="Select * from sub_categories where id='".$select_query_array['category_id']."'";

      $sub_category_query="Select * from 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 * from sub_categories where id='".$select_query_array['category_id']."'";

                    $sub_category_query="Select * from 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;





?>
2
  • did u maintain foreign key relation Commented Jul 22, 2013 at 9:56
  • Assuming you have a link key column between the 2 tables your code is almost fine just need to move the sub query into the while loop and put your sub output inside its own while Commented Jul 22, 2013 at 10:02

1 Answer 1

1

This will work only if u maintain forgien key relation between two tables.

echo "<select name='category'>";

$select_query=          'Select * from 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 * from sub_categories where 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;
Sign up to request clarification or add additional context in comments.

2 Comments

Parse error: syntax error, unexpected '}' in F:\xampp\htdocs\CMS\drop_down_list_test2.php on line 65
I think its because of the missing semi colon at the end of </option> line, but now after rectifying error, its showing me empty drop down list in the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.