/    Sign up×
Community /Pin to ProfileBookmark

How can I simplify this PHP code

[code=html]
<select name=”country”>
<option [code=php]<?php if ($val == ”) echo ‘selected’ ?>[/code]

value=””>Country…</option>
<option

[code=php]<?php if ($val == ‘Afganistan’) echo ‘selected’ ?>[/code]

value=”Afganistan”>Afghanistan</option>
<option

[code=php]<?php if ($val == ‘Albania’) echo ‘selected’ ?>[/code]

value=”Albania”>Albania</option>
<option

[code=php]<?php if ($val == ‘Algeria’) echo ‘selected’ ?>[/code]

value=”Algeria”>Algeria</option>
<option

[code=php]<?php if ($val == ‘American Samoa’) echo ‘selected’ ?>[/code]

value=”American Samoa”>American Samoa</option>
<option

[code=php]<?php if ($val == ‘Andorra’) echo ‘selected’ ?>[/code]

value=”Andorra”>Andorra</option>
<option

[code=php]<?php if ($val == ‘Angola’) echo ‘selected’ ?>[/code]

value=”Angola”>Angola</option>
<option

[code=php]<?php if ($val == ‘Anguilla’) echo ‘selected’ ?>[/code]

value=”Anguilla”>Anguilla</option>
….
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@rootDec 29.2015 — Looks like your post is in a bit of distress!

You wrap the forum tag around the block of code and not the individual elements...
[code=php]<select name="country">
<option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
<option <?php if ($val == 'Afganistan') echo 'selected' ?> value="Afganistan">Afghanistan</option>
<option <?php if ($val == 'Albania') echo 'selected' ?> value="Albania">Albania</option>
<option <?php if ($val == 'Algeria') echo 'selected' ?> value="Algeria">Algeria</option>
<option <?php if ($val == 'American Samoa') echo 'selected' ?> value="American Samoa">American Samoa</option>
<option <?php if ($val == 'Andorra') echo 'selected' ?> value="Andorra">Andorra</option>
<option <?php if ($val == 'Angola') echo 'selected' ?> value="Angola">Angola</option>
<option <?php if ($val == 'Anguilla') echo 'selected' ?> value="Anguilla">Anguilla</option>
[/code]
[code=php][/code]
Copy linkTweet thisAlerts:
@rootDec 29.2015 — Not the best way of doing it because I have no idea how you have selected the "selected" option in your script. [code=php]<select name="country">
<?php
$opts = array( ""=>"",
"afganistan"=>"Afganistan",
"albania"=>"Albania",
"algeria"=>"Algeria",
"american_samoa"=>"American Samoa"
);
$selected_option = "albania";
$out = array();
foreach($opts as $o=>$opt){
$selected = ( $selected_option == $o ) ? " selected" : "";
$out[] = sprintf("<option value='%s'%s>%s</option>",$o,$selected,$opt[$o]);
}
echo $out;
?>
</select>[/code]


If possible, turn the thing in to an array of elements to work with and output the string, this way you will either get an empty select element if something is wrong but the script runs rather than broken HTML and a PHP parser throwing lots of errors.
Copy linkTweet thisAlerts:
@tubc2015authorDec 29.2015 — ok, 'cause I'm li'l bit stressed to my code.
Copy linkTweet thisAlerts:
@rootDec 29.2015 — Spotted an error

[code=php]echo $out;[/code]

needs to be

[code=php]echo implode( PHP_EOL, $out);[/code]
Copy linkTweet thisAlerts:
@rootDec 29.2015 — ok, 'cause I'm li'l bit stressed to my code.[/QUOTE]

Will be AFK for a while, should be plenty of people to jump in with their ideas on how to do this.
Copy linkTweet thisAlerts:
@NogDogDec 29.2015 — I'd probably skip building the output array, and just do something like:
[code=php]
<?php
// might want this array defined in a separate include file or even a database query,
// rather than typing it all out in this file:
$options = array(
'Afghanistan',
'Albania',
'Algeria',
'American Samoa',
'Andora',
'Angola',
// . . .
'Zambia'
);
?>
<select name="country">
<option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
<?php foreach($options as $option)
{
printf(
" <option %svalue='%s'>%s</option>n",
$val == $option ? 'selected ' : '',
$option,
$option
);
}
?>
</select>
[/code]
Copy linkTweet thisAlerts:
@rootDec 29.2015 — I chose $opts = array( ""=&gt;"",
"afganistan"=&gt;"Afganistan",
"albania"=&gt;"Albania",
mainly to allow for a value of any description as well as an option of any value, for example...
[code=php]$opts = array( ""=>"",
"0a3b9f"=>"America, including United Kingdom, Iran, Libya, Lebanon, Kuwait, Iraq, Somalia, Bosnia, Saudi Arabia, Afghanistan, Sudan, Kosovo, Yemen, Pakistan and soon Syria",
"0000b5"=>"Albania",
[/code]
Copy linkTweet thisAlerts:
@tubc2015authorDec 30.2015 — NogDog, it works!

So, I will only add for example, [code=php]$val = "Albania";[/code]
[code=php] to the PHP code below.


<?php
// might want this array defined in a separate include file or even a database query,
// rather than typing it all out in this file:
$options = array(
'Afghanistan',
'Albania',
'Algeria',
'American Samoa',
'Andora',
'Angola',
// . . .
'Zambia'
);

$val = "Albania";

?>
<select name="country">
<option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
<?php foreach($options as $option)
{
printf(
" <option %svalue='%s'>%s</option>n",
$val == $option ? 'selected ' : '',
$option,
$option
);
}
?>
</select>[/code]
×

Success!

Help @tubc2015 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...