kingnutter Posted April 21, 2009 Share Posted April 21, 2009 Hi there, The following form is part of a page to enter information about periodical events. The frequency of said events can be yearly, monthly or daily. The frequency is defined by $freq which eventually will be POSTed from a preceding page. For a yearly event, however I do not want the "month" or "day" options to appear. The IF conditions below do not seem to be working. I suspect it may be the use of OR, or perhaps I need some ELSEs too. Any pointers appreciated. Cheers. Gary <?php // This script makes three pull-down menus // for an HTML form: months, days, years. // Make the months array: $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Make the days and years arrays and set frequency $days = range (1, 31); $years = range (2001, 2009); $freq = "yearly"; if ($freq = "daily") { // Make the days pull-down menu: echo '<select name="day">'; foreach ($days as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // Make the months pull-down menu: if ($freq = "monthly" or "yearly") { echo '<select name="month">'; foreach ($months as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // make the year pull-down menu: if ($freq = "daily" or "monthly" or "yearly") { echo '<select name="year">'; foreach ($years as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/ Share on other sites More sharing options...
Maq Posted April 21, 2009 Share Posted April 21, 2009 You need to compare the days which would be a double equal (==) the single equals is for assigning. You also have to compare $freq to each day... i.e. if ($freq == "daily" OR $freq == "monthly" OR $freq == "yearly") Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815610 Share on other sites More sharing options...
kingnutter Posted April 21, 2009 Author Share Posted April 21, 2009 Thank you. I shall give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815614 Share on other sites More sharing options...
kingnutter Posted April 21, 2009 Author Share Posted April 21, 2009 I've implemented the above suggestion to the code below, however it is still resulting in a dropdown box of day, month and year whatever I set the $freq to. I have tried putting the $freq values in both single and double quotes. Can anyone suggest where I might be going wrong? <?php // This script makes three pull-down menus // for an HTML form: months, days, years. // Make the months array: $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Make the days and years arrays and set frequency $days = range (1, 31); $years = range (2001, 2009); $freq = "monthly"; if ($freq == "daily") { // Make the days pull-down menu: echo '<select name="day">'; foreach ($days as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // Make the months pull-down menu: if ($freq == "monthly") OR ($freq == "daily") { echo '<select name="month">'; foreach ($months as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // make the year pull-down menu: if ($freq == "daily") OR ($freq == "monthly" OR ($freq == "yearly") { echo '<select name="year">'; foreach ($years as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815816 Share on other sites More sharing options...
rondog Posted April 21, 2009 Share Posted April 21, 2009 if ($freq == "monthly") OR ($freq == "daily") should be if (($freq == "monthly") OR ($freq == "daily")) if ($freq == "daily") OR ($freq == "monthly" OR ($freq == "yearly") should be if (($freq == "daily") OR ($freq == "monthly") OR ($freq == "yearly")) and I would use || rather than 'or' Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815820 Share on other sites More sharing options...
kingnutter Posted April 21, 2009 Author Share Posted April 21, 2009 Thanks for your help, both of you. My mistake, both the above solutions work so sorry for doubting you Mac. And cheers for an eloquent solution rondog. Gary Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815841 Share on other sites More sharing options...
Maq Posted April 21, 2009 Share Posted April 21, 2009 My mistake, both the above solutions work so sorry for doubting you Mac. Np Like rondog suggested, you should probably use || rather than OR, learn more here: Logical Operators. Quote Link to comment https://forums.phpfreaks.com/topic/155066-solved-if-statement-to-define-date-select-dropdown-boxes/#findComment-815846 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.