RE: [PHP] looping inside of a loop
| From: | Rodney Davis | Date: | Wed, 11 Oct 2000 01:23:30 +0000 |
| Subject: | RE: [PHP] looping inside of a loop | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19457@lists.php.net to get a copy of this message | ||
It worked wonderfully. Except for the demarkating "selected"
in the appropiate field. I tried the following:
function writePrefsOptions () {
$prefs = array( 'something' => 'something_in_japanese',
'something_else' =>
'something_else_in_japanese');
while (list($option, $value) = each($prefs)) {
$selected = $option == $form[prefecture] ? 'selected' : '';
echo "<option value=\"$option\"";
$selected = $option == $form[prefecture] ? 'selected' : '';
echo "$selected>$value</option>\n";
}
}
$form[prefecture] is the name of the select field.
I am not too sure why this wouldn't work
If you have any ideas please let me know.
thanks,
rodney
Rodney Davis writes:
> I am trying to dynamically generate an option list with the
> following script. easy enough but now I need the option
> value and the displayed value to be different. The option
> value must be in english and the displayed value must
> be in Japanese. How do I get $values[0] below into
> a loop inside of the original foreach loop so that
> "something" and "somethinginjapanese" match?
>
> function writePrefsOptions () {
> $prefs = array("something", "somethingelse");
> $values = array("somethinginjapanese", "somethingelseinjapanese");
> foreach ($prefs as $value) {
> print "<OPTION VALUE=\"".($value). "\"";
> print (($prefs == ($value))?"selected":"");
> print ">$values[0]</option>\n";
> }
> }
>
> i tried another foreach inside of the foreach loop and it didn't work
> as anticipated. What else could i do?
>
> Thanks for your help in advance,
>
> rodney
If feasible, you could have the English words as the keys of the
array, and the Japanese as the values. Something like this (not tested
but should be close):
$prefs = array(
'something' => 'something_in_japanese',
'something_else' => 'something_else_in_japanese'
);
while (list($option, $value) = each($prefs)) {
$selected = $option == $selected ? 'selected' : '';
echo "<option value=\"$option\"$selected>$value</option>\n";
}
That method automatically ensures that the option->value pairs are
always synced up, so long as that one array doesn't get
hosed. Maintaining two arrays means having to keep them in sync
manually--depending on the application, this may or may not be an
issue.
If you can't do that and the arrays are always guaranteed to be in
sync, you could just loop through one, keeping an integer counter as
you go, and use that integer counter to index into the corresponding
element of the other array.
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net