yami007 Posted July 29, 2009 Share Posted July 29, 2009 here's my code so far: <?php require_once("includes/countries.php"); $m = 0; while ($m <= 235) { echo '<option value="' . $countries[$m] . '">' . $countries[$m] . '</option>'; if ($countries[$m] == $Country) { echo '<option value="' . $countries[$m] . '" selected="selected">' . $countries[$m] . '</option>'; } $m++; } ?> there's only an array in the included file:countries.php this code of mine duplicate the name of the country not everyting, but the choosen country only. this is what i want to prevent, but i couldnt come up with anything so i need your help ^^ Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/ Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 <?php sort($countries); foreach($countries as $tempCountry) { echo "<OPTION value='" . $tempCountry . "'"; if($country == $tempCountry) { //country is a match echo " selected='selected'"; } echo ">" . $tempCountry . "</OPTION>"; } [code] the problem in your code is that you are echoing the value of the array index regardless of it matches or not, then you are echoing it a second time if it does match. Regardless, using foreach is always cleaner than trying a while loop to go through an array Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/#findComment-885493 Share on other sites More sharing options...
yami007 Posted July 29, 2009 Author Share Posted July 29, 2009 i rewrote it using your code and it worked, and it also worked when i did: <?php $m = 0; while ($m <= 235) { echo '<option value="' . $countries[$m] . '"'; if ($countries[$m] == $Country) { echo ' selected="selected"'; } echo '>' . $countries[$m] . '</option>'; $m++; } it was obvious but i couldnt come up with it, for your code i ddnt know the function that generates all the array, i searhed for it but in vain, thanks for everything ^^ Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/#findComment-885511 Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 are you talking about foreach ? in your case, what happens if $countries array changes in size in your included file. then you will have to remember somehow to change the value of your while loop else it will start chopping off countries. with a foreach loop it will automatically go through each index of the array no matter how large it is. Did it not work when you pasted my code ? Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/#findComment-885513 Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 are you talking about foreach ? in your case, what happens if $countries array changes in size in your included file. then you will have to remember somehow to change the value of your while loop else it will start chopping off countries. with a foreach loop it will automatically go through each index of the array no matter how large it is. Did it not work when you pasted my code ? This is very true. Alternatively you can also use for loop instead while like: for ($i = 0; $i<count($countries); $i++) { ... } and then theres no problems with extending the country array. Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/#findComment-885523 Share on other sites More sharing options...
yami007 Posted August 4, 2009 Author Share Posted August 4, 2009 are you talking about foreach ? in your case, what happens if $countries array changes in size in your included file. then you will have to remember somehow to change the value of your while loop else it will start chopping off countries. with a foreach loop it will automatically go through each index of the array no matter how large it is. Did it not work when you pasted my code ? man i said that it worked with your code ^^ and thanks for the advice, i think count() is better ^^ thanks again ^^ Link to comment https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/#findComment-890569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.