Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

populating drop down list and using the selected command to select item

Community Beginner ,
Jul 27, 2011 Jul 27, 2011

Hi  I have a drop down list being populated by a mysql table,  the dropdown list works fine with read and update but on the edit page I would like to have the  mysql stored id to be selected in the drop down list.

I have the following code but I can not get the item to be selected?  I think it is because I have not coded the if statement correctly,

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

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

Any help would be really appreciated

TOPICS
Server side applications
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Jul 28, 2011 Jul 28, 2011

Horsemad Gilly wrote:

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

<?php  if ((isset($_POST['stand_id'])) && ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>

Translate
Guest
Jul 27, 2011 Jul 27, 2011

Without the benefit of seeing all the code it is a little tough to see if the logic is valid, but something like this should work:

                          <?php
                                $stand_set = getall_stands();                
                                while ($stands = mysql_fetch_array ($stand_set) ) {       
                                  if (isset($_POST['stand_id']) == $url_show ['stand_id']);
                            ?>
                                <select name="stand_id">
                                <option><?php echo [add your code]; ?></option>
                                <option><?php echo [add your code]; ?></option>
                                <option><?php echo [add your code]; ?></option>
                                </select>
                            <?php }?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 27, 2011 Jul 27, 2011

gary,

What is the purpose of your modified code? I'm not seeing selected in your code, which is what the OP clearly wants to achieve. Your code would repeat the entire select menu, not the options. Additionally, you are aware that isset returns true or false, so the if statement will not do anything in your code.

Gilly, try this:

<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'])) && ($_POST['stand_id']) == $url_show['stand_id'])) { ?> selected="selected"<?php } ?>><?php  echo $stands['stand_descrip']; ?></option>

<?php  }  ?>

</select>

best,

Shocker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 28, 2011 Jul 28, 2011

Hi thanks for the feed back,  I think the issue is with the if statement.

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']

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 28, 2011 Jul 28, 2011

Horsemad Gilly wrote:

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

<?php  if ((isset($_POST['stand_id'])) && ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 28, 2011 Jul 28, 2011

OOOOOOOHHHHH THANK YOU THANK YOU THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  A Major break through, this was stopping me from completing a whole load of pages,  I owe you a drink!!!!!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 28, 2011 Jul 28, 2011

There were a couple of changes so the final code that works is

<?php  if ((isset($_POST['stand_id']) || ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>

Fabulous really appreciated, but I think you might have guessed that by now.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 28, 2011 Jul 28, 2011
LATEST

You code:

<?php  if ((isset($_POST['stand_id']) || ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>

Basically says if the form is submitted (isset = true) OR if something equals something then display the selected for the option. Upon looking at my code I discovered a syntax error, but the code logic basically said if the form was submitted (isset = true) AND if something equals something. If you're using the OR argument you should be able to dump the condition of whether the form was submitted and simply check if something equals something like this:

<?php  if ($stands ['stand_id'] == $url_show ['stand_id']) { ?> selected="selected"<?php } ?>

Thanks for the drink offer... but I'm already drunk!

best,

Shocker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines