Jump to content

using the selected comand within php drop down list with data from mysql dbase


barniegilly

Recommended Posts

hi  I am working on a form which has a drop down list being populated by a mysql database.

 

Aim is to have the item held in the database to be selected on the dropdown list.  I have the following code but

 

 

<select name="stand_id">

<?php

  $stand_set = getall_stands();  

  while ($stands = mysql_fetch_array ($stand_set) ) {

        ?>

<option value=" <?php echo $stands ['stand_id'];

        ?>"

          <?php

                  if (isset($_POST['stand_id']) == $url_show ['stand_id'])  echo 'selected=\"selected\"';

          ?>

            >

          <?php  echo $stands ['stand_descrip']; ?></option>

  <?php  }  ?> </select>

 

Whilst this updates the database the drop down list defaults to the first list item instead of the item in the database,  any help would be very much appreciated.

 

Link to comment
Share on other sites

not quite sure what you mean but echo 'selected=\"selected\"'; is wrong.

Try this

echo '<select name="stand_id">';
$stand_set = getall_stands();
while ($stands = mysql_fetch_array($stand_set)){
echo '<option value="'.$stands['stand_id'].'"';
if (isset($_POST['stand_id']) == $url_show ['stand_id'])  echo ' selected';
echo '>'.$stands['stand_descrip'].'</option>';
}
echo '</select>';

Link to comment
Share on other sites

Hi

 

Thank you for that, I have amended the code but the drop down lists still defaults to the first item on the list coming from the database.

 

The attached image shows that whilst the $stand_id is 7 the drop down list has nothing selected and shows the first item in the drop down list where it should be A07 selected.

 

[attachment deleted by admin]

Link to comment
Share on other sites

Opps. Sorry.

change

if (isset($_POST['stand_id']) == $url_show ['stand_id'])  echo ' selected';

to

if (isset($_POST['stand_id']) && $_POST['stand_id'] == $url_show ['stand_id'])  echo ' selected';

 

Keep in mind it's expecting that posted value to compare. you can change that line to any other variable if you're getting the selected item from somewhere else.

Link to comment
Share on other sites

Hi  sorry but still not working,  I think the issue is with what it is comparing. 

 

The value  $url_show ['stand_id'] is the individual value stored in the table.  The drop down list value is also coming from the database and if the form has not been submitted it will not be showing a $_POST.  So I need it to be comparing against the ID coming from the list containing the drop down items, this is being pulled from the database with

 

$stand_set = getall_stands();

while ($stands = mysql_fetch_array($stand_set))

 

so the argument  would be $stands ['stand_id'] being compared with $url_show ['stand_id'] and then selecting the $stands ['stand_id']

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.