Jump to content

[SOLVED] problem with select button & array


yami007

Recommended Posts

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  :facewall:

so i need your help ^^

Link to comment
https://forums.phpfreaks.com/topic/167887-solved-problem-with-select-button-array/
Share on other sites

<?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

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 ^^

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 ?

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.

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 ^^

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.