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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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