PHP Array for Form Elements

Discussion in 'PHP' started by Omzy, Mar 29, 2009.

  1. #1
    I'm creating a form and want some drop down boxes generated from an array: I want to do something like this:

    
    $attributes=array(
    	'location'=>array('manchester'=>'Manchester', 'london'=>'London'),
    	);
    
    Code (markup):
    Essentially what I want the code to do is output the following:

    
    <select name="location">
    <option value="manchester">Manchester</option>
    <option value="london">London</option>
    </select>
    
    Code (markup):

    I've got the code to match on the outer array but I now need a foreach loop which will output the values from the inner array. I want to do this using one main array ($attributes) only.
     
    Omzy, Mar 29, 2009 IP
  2. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <select name="blaha">
    <option>$array['manchester']</option>
    <option>$array['london']</option>
    </select>

    maybe something like this ?

    sorry didnt read the beloiw bit ! just noticed what u need
     
    dean5000v, Mar 29, 2009 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $location = array(1,2,3);

    echo '<select name="location">';
    foreach ($location as $value) {
    echo" <option>$value</option>";
    }
    echo '</select>';

    something like this may help. Just did it quicklyu to give you a quick idea on how to use the foreach as u wanted.
     
    dean5000v, Mar 29, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    print "<select name="location">";
    foreach($attributes as $loc) {
    print "<option>$loc</option>";
    }
    print "</select>";
     
    crivion, Mar 29, 2009 IP
  5. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You have missed out the VALUE attribute of the OPTION element...
     
    Omzy, Mar 29, 2009 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    lol, can't u add it?
    print "<option value=\"$loc\">$loc</option>";
     
    crivion, Mar 29, 2009 IP
  7. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    But that's not what I want it to do...

    <option value="manchester">Manchester</option>

    Notice the capitalization of the value attribute, I need this data to be pulled from the array. I could have a different word altogether for the value so it's not gonna be a simple case of using a string function.
     
    Omzy, Mar 29, 2009 IP
  8. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I managed to figure it out...

    
    foreach($attributes as $key1 => $value1)
    {
    	echo '<select name="'.$key1.'">';
    	
    	foreach($value1 as $key2 => $value2)
    	{
    		echo '<option value="'.$key2.'">'.$value2.'</option>';
    	}
    	
    	echo '</select>';
    }
    
    Code (markup):
     
    Omzy, Mar 29, 2009 IP