2

I have a list of areas (1000+) and i was wondering if there was a way i can do this easier with code instead of repeating each value.

<select>
<option value="apple" <?php if ($user_data["$area"] == apple){echo 'selected';} ?>>Apple
</option> 

<option value="lemon" <?php if ($user_data["$area"] == lemon){echo 'selected';} ?>>Lemon
</option> 

<option value="orange" <?php if ($user_data["$area"] == orange){echo 'selected';} ?>>Orange
</option> 

<option value="banana" <?php if ($user_data["$area"] == banana){echo 'selected';} ?>>Banana
</option>
</select>

I.E. have the same piece of php code for each option instead of having to type in the name of the value each time

<?php if ($user_data["$area"] == option VALUE){echo 'selected';} ?>

Could you provide some code or ideas for what to look in tutorials, i have no idea how to start. Thank you!

2
  • 2
    perhaps a for loop iterating over an array of compiled elements you would like to render? Try compiling all of the option values into a 1 dimensional array and use a for loop to write each to the screen. Commented Nov 10, 2013 at 0:03
  • The examples below (with the exception of mine) will render out the same exact content you are writing by hand now. Commented Nov 10, 2013 at 0:22

4 Answers 4

5
//pseudo
$arr = array("apple", "lemon", "orange", ...);
foreach($arr as $value) {
    echo '<option value="'.$value;
    if($user_data[$area] === $value) {
        echo 'selected';
    }
    //echo {the end of your option field syntax}
}
Sign up to request clarification or add additional context in comments.

Comments

4

All the solutions look good... Here's one more way though:

<select>
<?php
  $areas = array('apple', 'lemon', 'orange', 'banana');
  $areas_count = count($areas);
  for ($i = 0; $i < $areas_count; $i++) {
    echo '<option value="' . $areas[$i] . '"';
    echo ($user_data[$area] == $areas[$i]) ? ' selected' : '';
    echo '>' . ucwords($areas[$i]) . '</option>';
  }
?>
</select>

Comments

3

Use an array:

$areas = array(
    'apple' => 'Apple',
    'lemon' => 'Lemon',
    'orange' => 'Orange',
    'banana' => 'Banana'
);

Then use that array to print the select:

<select>
<?php foreach ($areas as $value => $text): ?>
    <option value="<?php echo $value; ?>" <?php if ($user_data[$area] == $value) {echo 'selected';} ?>><?php echo $text; ?>
    </option> 
<?php endforeach; ?>
</select>

I am using an associative array because I am assuming that you want to be able to customize the areas' text label, and that they will not only be a capitaized version of the value used to match the user data.

4 Comments

This is a good option, but I think its better not to wrap $area in quotes. $user_data[$area] is just fine, maybe even better than $user_data["$area"]. Using variables in double quotes is best used only to improve readability when adding multiple variables to a string, there's really no point for it to be used in this case.
yes it is, I just copied the code from the question... this comment should be directed at the OP ;)
@SirDarius Ah, I didn't even notice the OP had $area wrapped in quotes. Sorry about that. :P
my select name is "fruit", so when i do echo $_POST['fruit'], I have for example 'apple', but how can I have 'Apple' ? Thanks
0
foreach ($areas as $key => $val)
{
    $select.= '<option '; // opening the option tag
    foreach ($selected_from_db as $keyselected => $valselected)
    {
      $val_fetch_from_array = $valselected == $val['campaignid'] ? 'selected' : '';
      $select.= $val_fetch_from_array; // codes for selecting multiple values
    }
    $select.= ' value = "' . $val['campaignid'] . '">#' . $val['campaignid'] . '-' . $val['campaignname'] . '</option>'; // closing the option tag
}

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.