Taha Kirmani
Taha Kirmani

Reputation: 1274

How to show the selected item name in the drop down list?

I have created a 'Insert_Product' page in which I have some product related fields and a drop down box which is showing 'Category' and 'Sub_category' from which the specific product relates.

When I update any specific product it opens the Product page and Fetches the data from database in the form Fields, but in the drop down list it shows the first given name. I want it to select the selected category from which the Product belongs.

Right now its showing the first item from the database.

        $select_query=  "select * from products LEFT JOIN product_description 
        ON products.product_id='".$id."' and 
        product_description.product_id='".$id."' 
        where products.product_id='".$id."'     ";

        $sub_category_id2 = $fetch_select['sub_category_id'];




        echo "<tr><td>Select Category </td><td><select name='category'>";

        $select_query=          'Select * from category';

        $select_query_run =     mysql_query($select_query);

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


        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 category_id='".$select_query_array['category_id']."'";


        $sub_query_run=         mysql_query($sub_category_query);

        while   ($sub_query_run_fetch=   mysql_fetch_array($sub_query_run) ) 
         {

            echo    'sub1 '.$sub_category_id =  
                        $sub_query_run_fetch['sub_category_id'] ;

                echo 
                "<option value=\"$sub_category_id\"";

                    if($sub_category_id==$sub_category_id2)

                echo ' selected="selected"';

                echo " >" .
                htmlspecialchars($sub_query_run_fetch['sub_category_name']) . "</option>";
                 }

                    echo "</optgroup>";
             }

enter image description here

                <tr><td>Select Category </td><td>


<select name='category'><optgroup label='Rings' >Select * from sub_categories 
                            where category_id='1'sub1 1<option value="1" >Gold</option>sub1 2<option value="2" >Silver</option>sub1 3<option value="3" >Diamond</option></optgroup><optgroup label='Bracelets' >Select * from sub_categories 
                            where category_id='2'sub1 4<option value="4" >Diamond</option>sub1 6<option value="6" >Gold</option>sub1 7<option value="7" >Silver</option></optgroup><optgroup label='Necklaces' >Select * from sub_categories 
                            where category_id='3'sub1 10<option value="10" >Diamond</option>sub1 11<option value="11" >Gold</option>sub1 12<option value="12" >Silver</option></optgroup><optgroup label='Earrings' >Select * from sub_categories 
                            where category_id='4'sub1 16<option value="16" >Diamonds</option>sub1 17<option value="17" >Gold</option>sub1 18<option value="18" >Silver</option></optgroup><optgroup label='Chains' >Select * from sub_categories 
                            where category_id='5'sub1 27<option value="27" >Diamond</option>sub1 28<option value="28" >Gold</option>sub1 29<option value="29" >Silver</option></optgroup><optgroup label='Pendants' >Select * from sub_categories 
                            where category_id='6'sub1 19<option value="19" >Diamond</option>sub1 20<option value="20" >Gold</option>sub1 21<option value="21" >Silver</option></optgroup><optgroup label='Brooches' >Select * from sub_categories 
                            where category_id='7'sub1 5<option value="5" >Silver</option>sub1 22<option value="22" >Gold</option>sub1 23<option value="23" >Diamond</option></optgroup><optgroup label='Charms' >Select * from sub_categories 
                            where category_id='8'sub1 24<option value="24" >Diamond</option>sub1 25<option value="25" >Gold</option>sub1 26<option value="26" >Silver</option></optgroup></br></div></div></br>

</select>

Upvotes: 1

Views: 1960

Answers (1)

Levi
Levi

Reputation: 2113

You need to add a space before selected

echo " selected";

without it, 'selected' is becoming part of the value. you should also quote your value for html correctness.

echo "<option value=\"$sub_category_id\"";

Update:

Looks like you're missing a query? $sub_category_id2 is being set to an undefined variable:

    $select_query=  "select * from products LEFT JOIN product_description 
    ON products.product_id='".$id."' and 
    product_description.product_id='".$id."' 
    where products.product_id='".$id."'     ";

    // MISSING QUERY HERE - $fetch_select is not defined

    $sub_category_id2 = $fetch_select['sub_category_id'];

Upvotes: 1

Related Questions