tqla Posted May 27, 2009 Share Posted May 27, 2009 Hello. I want to make this static listbox dynamic. <select name="checkInMonthYear" id="checkInMonthYear" class="check"> <option value="May 2009">May 2009</option> <option value="Jun 2009">Jun 2009</option> <option value="Jul 2009">Jul 2009</option> <option value="Aug 2009">Aug 2009</option> <option value="Sep 2009">Sep 2009</option> <option value="Oct 2009">Oct 2009</option> <option value="Nov 2009">Nov 2009</option> <option value="Dec 2009">Dec 2009</option> <option value="Jan 2010">Jan 2010</option> <option value="Feb 2010">Feb 2010</option> <option value="Mar 2010">Mar 2010</option> <option value="Apr 2010">Apr 2010</option> </select> It needs to check the current month and year and then list the current "month/year" plus the next consecutive 11 "month/year" options. Can someone point me in the right direction? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/ Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Have you tried coding it at all? Generally, we give out help to those that at least attempt to find out the answer themselves... Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843241 Share on other sites More sharing options...
tqla Posted May 27, 2009 Author Share Posted May 27, 2009 My actual question was "Can someone point me in the right direction?". Link, PHP can/cannot do this, JS better for this, etc. . Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843257 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 PHP. A quick look in the manual would tell you that date('M') returns a three letter representation of the current month. And to get the next 11 months, you can use date('M', strtotime("+ $i months")) in a loop, where $i is increased by one on each iteration. Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843267 Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Sorry about that... Personally, I would just use PHP... I constantly test what people post about on here and already figured this one out: http://anomymage.com/tests/do/phpfreaks/ Might be the long way to do it, but it works: <?php $month = date("F o", mktime(0, 0, 0, date("m"))); $month1 = date("F o", mktime(0, 0, 0, date("m")+1)); $month2 = date("F o", mktime(0, 0, 0, date("m")+2)); $month3 = date("F o", mktime(0, 0, 0, date("m")+3)); $month4 = date("F o", mktime(0, 0, 0, date("m")+4)); $month5 = date("F o", mktime(0, 0, 0, date("m")+5)); $month6 = date("F o", mktime(0, 0, 0, date("m")+6)); $month7 = date("F o", mktime(0, 0, 0, date("m")+7)); $month8 = date("F o", mktime(0, 0, 0, date("m")+); $month9 = date("F o", mktime(0, 0, 0, date("m")+9)); $month10 = date("F o", mktime(0, 0, 0, date("m")+10)); $month11 = date("F o", mktime(0, 0, 0, date("m")+11)); ?> <select name="checkInMonthYear" id="checkInMonthYear" class="check"> <option value="<?php echo $month; ?>"/><?php echo $month; ?></option> <option value="<?php echo $month1; ?>"/><?php echo $month1; ?></option> <option value="<?php echo $month2; ?>"/><?php echo $month2; ?></option> <option value="<?php echo $month3; ?>"/><?php echo $month3; ?></option> <option value="<?php echo $month4; ?>"/><?php echo $month4; ?></option> <option value="<?php echo $month5; ?>"/><?php echo $month5; ?></option> <option value="<?php echo $month6; ?>"/><?php echo $month6; ?></option> <option value="<?php echo $month7; ?>"/><?php echo $month7; ?></option> <option value="<?php echo $month8; ?>"/><?php echo $month8; ?></option> <option value="<?php echo $month9; ?>"/><?php echo $month9; ?></option> <option value="<?php echo $month10; ?>"/><?php echo $month10; ?></option> <option value="<?php echo $month11; ?>"/><?php echo $month11; ?></option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843270 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 Might be the long way to do it Yep! Loopy goodness: <?php echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">'; for ($i = 0; $i < 12; $i++) { $date_str = date('M Y', strtotime("+ $i months")); echo "\n<option value=\"$date_str\">$date_str</option>"; } echo "\n</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843284 Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Might be the long way to do it Yep! Loopy goodness: <?php echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">'; for ($i = 0; $i < 12; $i++) { $date_str = date('M Y', strtotime("+ $i months")); echo "\n<option value=\"$date_str\">$date_str</option>"; } echo "\n</select>"; ?> Fantastic... must update my snippets! Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843288 Share on other sites More sharing options...
tqla Posted May 27, 2009 Author Share Posted May 27, 2009 Hi jschmitt, I didn't mean to make my first post seem to ask for someone to do it for me and after your post I tried to reword it but the modify link was gone. I promise I am not a leach. I'm studying your first post and it's... perfect. So just so I fully understand it and will be able to do this myself the next time (in other words, learn from you) I will explain it back. $month = date("F o", mktime(0, 0, 0, date("m"))); You created a variable called month that is equal to the current date displayed as F (month) and o (year). Within date() you used mktime to create a current unix timestamp and as your 4th argument (month) you used the current month. Genius! Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843300 Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Hi jschmitt, I didn't mean to make my first post seem to ask for someone to do it for me and after your post I tried to reword it but the modify link was gone. I promise I am not a leach. I'm studying your first post and it's... perfect. So just so I fully understand it and will be able to do this myself the next time (in other words, learn from you) I will explain it back. $month = date("F o", mktime(0, 0, 0, date("m"))); You created a variable called month that is equal to the current date displayed as F (month) and o (year). Within date() you used mktime to create a current unix timestamp and as your 4th argument (month) you used the current month. Genius! I didn't think you were being a leech, I was just wondering what you had attempted really. I guess my first post came out wrong. Thanks, but thebadbad actually made one better then mine. Mine will work just fine, but his is a simple loop... But your re-iteration is definitely correct, and I appreciate the genius statement, but it is far from it. Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843304 Share on other sites More sharing options...
tqla Posted May 27, 2009 Author Share Posted May 27, 2009 wow, loopy goodness indeed. Thanks thebadbad and jsschmitt. I'm understand this but it was at first admittedly beyond my current capabilities. You created a for loop that will run as long as i is less than 12. The variable $date_str is equal to the date Month and Year and you use the strtotime() function to add the value of "i" to months. Awesome. <?php echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">'; for ($i = 0; $i < 12; $i++) { $date_str = date('M Y', strtotime("+ $i months")); echo "\n<option value=\"$date_str\">$date_str</option>"; } echo "\n</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843324 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 You're an easy leaner! Good on ya Quote Link to comment https://forums.phpfreaks.com/topic/159885-solved-dynamic-listbox-that-checks-current-monthyear/#findComment-843337 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.