Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Dynamic combobox

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Postby azteck » Fri Feb 28, 2003 11:40 pm

Hello.I have a sample.It uses arrays but the ideea stays the same.All you have to do is to modify it

<select name="country" size="1" id="country">
<?
for ($i=0;$i<count($country_array);$i++)
{
if ($i==0)
{
?>
<option selected><?echo $country_array[$i]?></option>
<?
}
else
{
?>
<option><?echo $country_array[$i]?></option>
<?

}
}
?>
</select>

where
$country_array=array("France","Romania"); ans so on.
I hope you will understand this and then crezte your own.If not I'll try to send you the code for mysql :)
azteck
New php-forum User
New php-forum User
 
Posts: 12
Joined: Sun Dec 15, 2002 3:54 pm

Postby Redcircle » Sat Mar 01, 2003 1:26 am

correction (you forgot to give the option's values.) also I would set a variable named $size before the loop. Doing this makes execution a lil faster. Updated code below

Code: Select all
<select name="country" size="1" id="country">
<?
$size = sizeof($country_array);
for ($i=0; $i<$size;$i++)
{
if ($i==0)
{
?>
<option value = "<?echo $country_array[$i]?>" selected><?echo $country_array[$i]?></option>
<?
}
else
{
?>
<option value = "<?echo $country_array[$i]?>"><?echo $country_array[$i]?></option>
<?

}
}
?>
</select>
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby azteck » Sat Mar 01, 2003 2:53 am

Yes, you are right :)
Thank you .
azteck
New php-forum User
New php-forum User
 
Posts: 12
Joined: Sun Dec 15, 2002 3:54 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.