Jump to content

2 dimensional array + dropdownlist


coder1

Recommended Posts

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 in the dropdownlist as selected.

 

any help would be much apperciated.

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/127167-2-dimensional-array-dropdownlist/
Share on other sites

This should do it:

<?php
$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'));
?>

<select name="selMonth">
<?php
foreach($month as $key=>$value)
{
?>
	<option <?php echo isset($_POST['selMonth']) && $_POST['selMonth'] == $key?'selected="selected" ':""; ?>value="<?php echo $key; ?>"><?php echo $value[0]; ?></option>
<?php
}
?>
</select>

I don't know, I found an example on the web, how should they be?

 

Thanks.

 

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

print_r($months);

 

Like that.

Here's my array.

 

 

$month = array("January"=>1,"February"=>2,"March"=>3,"April"=>4,"May"=>5"June"=>6,"July"=>7,"August"=>8,"September"=>9,"October"=>10,"November"=>11,"December"=>12 )

 

here the dropdownlist. I did what u posted.

 

 

<select name="completemonth" id="completemonth" >
	  <option value="0" SELECTED>Month</option>
		<?php foreach($month as $key=>$value){ ?>
	    <option <?php echo isset($_POST['completemonth']) && $_POST['completemonth'] == $key?'selected="selected" ':""; ?>value="<?php echo $key; ?>"><?php echo $value[0]; ?></option>
      <?php }?>
	</select>

 

I m getting a blank page.

I tested this and it works fine:

<?php
$month = array("January"=>1,
			"February"=>2,
			"March"=>3,
			"April"=>4,
			"May"=>5,
			"June"=>6,
			"July"=>7,
			"August"=>8,
			"September"=>9,
			"October"=>10,
			"November"=>11,
			"December"=>12 );
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="selMonth">
<?php
foreach($month as $key=>$value)
{
?>
	<option <?php echo isset($_POST['selMonth']) && $_POST['selMonth'] == $value?'selected="selected" ':""; ?>value="<?php echo $value; ?>"><?php echo $key; ?></option>
<?php
}
?>
</select><br />
<input type="submit" name="btnSubmit" value="Submit" />
</form>

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.