1

Trying to get an option selected in a drop down based on the reference stored in a mysql database.

This is happening during producing a table already within a while loop.

I have tried the following focusing on the

<option selected='<? if ( $row2xsecid == $row1x[item_sub_sec_id]) { echo "selected"; } ?>' value="<? echo $row1x[item_sub_sec_id]; ?>"><? echo $row1x[item_sub_sec_title]; ?></option>

I cannot seem to get the option selected based on the reference stored in the mysql.

The snippet

  <div class="input-group">
                    <span class="input-group-addon">Select Group</span>
                    <select id='add_item_groupid' class="form-control">
                    <?
                    $sql1x = 'SELECT * FROM items_sub_section_list ORDER BY item_sub_sec_id ASC';
                    $result1x = mysql_query($sql1x);

                    while ($row1x = mysql_fetch_array($result1x)) {
                    ?>
                            <?
                            $sql2x = 'SELECT * FROM items_groups WHERE item_id=$itemid';
                            $result2x = mysql_query($sql2x);
                            while ($row2x = mysql_fetch_array($result2x)) { $row2xsecid = $row2x[item_sub_sec_id]; }
                            ?>
                        <option selected='<? if ( $row2xsecid == $row1x[item_sub_sec_id]) { echo "selected"; } ?>' value="<? echo $row1x[item_sub_sec_id]; ?>"><? echo $row1x[item_sub_sec_title]; ?></option>

                    <?
                    } 
                    ?> 
                    </select>
    </div>

The pastebin of the full script - http://pastebin.com/NZrETate

1
  • 1
    are u sure ur short tag is on <? in php.ini ? Commented Mar 27, 2014 at 16:34

1 Answer 1

1

I see you are fetching array's instead of associative arrays. (line 27: mysql_fetch_array instead of: mysql_fetch_assoc)

Doing this you have to keep in mind that the index of your fields start with 0 and are numerical which means this will return Undefined index:

line40: <td><? echo $row['item_id']; ?></td>

Because you fetched an array this will also return Undefined index: $itemid = $row['item_id']; Because $itemid is undefined the query at line 49 will also fail:

 $secid = mysql_query(" SELECT * FROM item_groups WHERE item_id='$itemid' ");

I also can't see where item_sub_sec_id is declared anywhere? (In PHP if this is a variable it must have a $ in front: $item_sub_sec_id)

Are you able to see the errors PHP is throwing you? If not I suggest showing all errors like so:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Once you have these issues sorted we can look at the problem, it will also be easier for me to give you a solution if we know what the DB structure looks like.

Sign up to request clarification or add additional context in comments.

Comments

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.