Forum Moderators: coopster

Message Too Old, No Replies

2 dimensional array

popluating dropdown list with 2 dimensional array.

         

coder1

11:47 pm on Oct 5, 2008 (gmt 0)

10+ Year Member



I have 2 dimensional array:

$month =
array(01=>array('January'),02=>array('February'),03=>array('March'), 04=>array('April'),05=>array('May'),06=>array('June'),07=>array('July'), 08=>array('August'),09=>array('September'),10=>array('October'), 11=>array('November'),12=>array('December'));

1)I want to loop throught the array and populate a dropdownlist in a html form with the above array. I want the string version of the month to be a diplay text, and number to be in the value. like this

<select>
<option value="01">January</option>
</select>

2)when the form is submitted, I want to echo out the selected value.

any help would be much apperciated.

thanks.

[edited by: eelixduppy at 12:04 am (utc) on Oct. 6, 2008]
[edit reason] side-scroll [/edit]

cameraman

12:02 am on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



foreach($month as $num => $ary)
echo "<option value=\"$num\">{$ary[0]}</option>\n";

You'll need to give the select a name, then if you're using POST method on the form it will be $_POST['whatevername'] or if GET, $_GET['whatevername'].

Out of curiosity, why do you have the month names as single-element arrays?

grallis

12:18 am on Oct 6, 2008 (gmt 0)

10+ Year Member



hi coder1 -

I recommend reforming your 2-dimensional array to either this format: array("January"=>1, "February"=>2), or using a regular array like such: array("January","February","March")


<select name="months">
<?
// for associative array
foreach($months as $k=>$v){
?><option value="<?=$v?>">$k</option><?
}

// for regular array
$monNum = count($months);
for($i=0; $i<$monNum; $i++){
?><option value="<?=$i?>"><?=$months[$i]?></option><?
}
?>
</select>

Now to echo out the string value of the month after using the $_POST array would be:


// validate POST variables
// associative array method using the date function
echo date("F", mktime(0,0,0,$_POST['month'],1,2000));
// normal array method
echo $months[$_POST['month']];

Hope this helps ...

coder1

12:39 am on Oct 6, 2008 (gmt 0)

10+ Year Member



thank you guys for you reply.

grallis- thanks, I tried your idea, its working fine.I meant how do i echo it out on the dropdownlist, what the user selected. Like if the form did not get validated, returned with errors with u r previously selected option.

thanks again for info on the array.

grallis

1:09 am on Oct 6, 2008 (gmt 0)

10+ Year Member



Here's the solution using the regular array:


<select name="months">
<?
$monNum = count($months);
for($i=0; $i<$monNum; $i++){
if($i == $_POST['months']) $selected = "selected='selected'";
else $selected = "";
?><option value="<?=$i?>" $selected><?=$months[$i]?></option><?
}
?>
</select>

Something to that effect would work. May need to be tweaked a little.

Also note: depending on your PHP error and warning levels, you may need to validate if the $_POST['months'] even exists before going into that if statement, or it may throw a warning at you.