coder1 Posted October 5, 2008 Share Posted October 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 6, 2008 Share Posted October 6, 2008 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> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Why are they in as arrays in the first place though? Quote Link to comment Share on other sites More sharing options...
coder1 Posted October 6, 2008 Author Share Posted October 6, 2008 I don't know, I found an example on the web, how should they be? Thanks. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
coder1 Posted October 6, 2008 Author Share Posted October 6, 2008 hey thanks for the reply again, I wanted the string month ie 'March' in the text display of the dropdownlist, in the the value, I want 03. Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 6, 2008 Share Posted October 6, 2008 deleted [0] out of the following line to accommodate. <option <?php echo isset($_POST['selMonth']) && $_POST['selMonth'] == $key?'selected="selected" ':""; ?>value="<?php echo $key; ?>"><?php echo $value; ?></option> Quote Link to comment Share on other sites More sharing options...
coder1 Posted October 6, 2008 Author Share Posted October 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 6, 2008 Share Posted October 6, 2008 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> Quote Link to comment Share on other sites More sharing options...
coder1 Posted October 6, 2008 Author Share Posted October 6, 2008 R0bb0b, thanks for your help. of course it works, i just missed a semi colon at the end of my array. Many many thanks for you help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.