Jump to content

Populating Drop Down List


gobbles

Recommended Posts

Hey All,

Im just trying to cut down on thousands of lines of code at the moment and hopefully someone could help me.

I have a drop down box and the "selected" value needs to be selected by a dynamic value that gets entered into the database, this is currently how im doing it:

[code]<?php
                if($modify_row[21] == "0") {
                  print "<option value=\" \"></option>\n";
                  print "<option value=\"1\">Active</option>\n";
                  print "<option value=\"0\" selected>In-Active</option>\n";
                } elseif ($modify_row[21] == "1") {
                  print "<option value=\" \"></option>\n";
                  print "<option value=\"1\" selected>Active</option>\n";
                  print "<option value=\"0\">In-Active</option>\n";
                }
?> [/code]

At the moment i have over 100 "options" and i think it would be a major waist of code doing it this way, can someone help me out please.

Its fairly straight forward, basically the option that is automatically selected needs to be the one coming from the database $modify_row[21].

Thanks All
Link to comment
Share on other sites

You might consider making a function so you don't have the same thing posted over and over. Here is a simpler version. Waist is around your tummy, waste is a verb more appropriate.
[code]
for($i=1; $i<=100; $i++){
  printStuff($modify_row[$i]);
}

function printStuff($value){
    print '<option value=" "></option>
    <option value="1"';
    if(!$value){
        print ' selected ';
    }
    print '>Active</option>
    <option value="0"';
    if($value){
        print ' selected ';
    }
    print '>In-Active</option>';
}
[/code]
Link to comment
Share on other sites

try this

[code]
<?php

if($modify_row[21] == "0") { $selected = "selected";}


                  print "<option value=\" \" $selected></option>\n";
                  print "<option value=\"1\" $selected>Active</option>\n";
                  print "<option value=\"0\" $selected>In-Active</option>\n";
[/code]
Link to comment
Share on other sites

Erm, you could make an array of the options, and use foreach, or call the function several times.
[code]
$options = array('Phone', 'Fax');
foreach($options AS $o){
    printStuff($_POST[$o]);
}
[/code]

Or just
[code]printStuff($_POST['Phone']);
printStuff($_POST['Fax']);[/code]

Without seeing the code that's all I can suggest. You'll obviously have to modify it to suit you.
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.