Superian Posted March 18, 2008 Share Posted March 18, 2008 I am trying to get val =\"$i\"> to count from 1-12 instead of 0-11. How would I accomplish this? <?php function months() { $month = array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $select = "<select name=\"birth_month\">\n"; $select .="<option val=\"0\">Month\n"; for ($i = 0; $i < count($month); $i++) { $select .= "<option val=\"".$i."\">".$month[$i]."\n"; } $select .= "</option></select>"; return $select; } $month = months(); echo $month; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 18, 2008 Share Posted March 18, 2008 Just change this: for ($i = 0; $i < count($month); $i++) { To: for ($i = 1; $i < count($month); $i++) { Quote Link to comment Share on other sites More sharing options...
Superian Posted March 18, 2008 Author Share Posted March 18, 2008 Just change this: for ($i = 0; $i < count($month); $i++) { To: for ($i = 1; $i < count($month); $i++) { I've tried that. I loose the month January. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 18, 2008 Share Posted March 18, 2008 You want to display i+1 in the value, yes? So do that! <?php function months() { $month = array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $select = "<select name=\"birth_month\">\n"; $select .="<option val=\"0\">Month\n"; for ($i = 0; $i < count($month); $i++) { $select .= "<option val=\"".($i+1)."\">".$month[$i]."\n"; } $select .= "</option></select>"; return $select; } $month = months(); echo $month; ?> It might be better to do $tmp = $i + 1, and then echo $tmp instead of ($i+1).... Not sure. Quote Link to comment Share on other sites More sharing options...
Superian Posted March 18, 2008 Author Share Posted March 18, 2008 You want to display i+1 in the value, yes? So do that! <?php function months() { $month = array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $select = "<select name=\"birth_month\">\n"; $select .="<option val=\"0\">Month\n"; for ($i = 0; $i < count($month); $i++) { $select .= "<option val=\"".($i+1)."\">".$month[$i]."\n"; } $select .= "</option></select>"; return $select; } $month = months(); echo $month; ?> It might be better to do $tmp = $i + 1, and then echo $tmp instead of ($i+1).... Not sure. This is the output: No January <select name="birth_month"> <option val="0">Month <option val="2">February <option val="3">March <option val="4">April <option val="5">May <option val="6">June <option val="7">July <option val="8">August <option val="9">September <option val="10">October <option val="11">November <option val="12">December </option></select> Quote Link to comment Share on other sites More sharing options...
Superian Posted March 18, 2008 Author Share Posted March 18, 2008 SOLVED 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.