grahamb314 Posted September 21, 2008 Share Posted September 21, 2008 Hi all, I have the following code which only works for monday, tuesday and wednesday! - The for the other days, the code doesnt work - Is there a limit on the number of else ifs you can have or have I done something silly? if ($_POST["Day"] == 'Monday' || $_POST['Day'] == 'monday') {$_POST["Day2"] = 0;} else if ($_POST["Day"] == 'Tuesday' || $_POST['Day'] == 'tuesday') {$_POST["Day2"] = 1;} else if ($_POST["Day"] == 'Wednesday' || $_POST['Day'] == 'wednesday') {$_POST["Day2"] = 2;} else if ($_POST["Day"] == 'Thursday' || $_POST['Day'] == 'thursday') {$_POST["Day2"] = 3;} else if ($_POST["Day"] == 'Friday' || $_POST['Day'] == 'friday') {$_POST["Day2"] = 4;} else if ($_POST["Day"] == 'Saturday' || $_POST['Day'] == 'saturday') {$_POST["Day2"] = 5;} else if ($_POST["Day"] == 'Sunday' || $_POST['Day'] == 'sunday') {$_POST["Day2"] = 6;} else if ($_POST["Day"] == '' || $_POST['Day'] == ' ' || $_POST['Day'] == ' ') { $errorstatus=$errorstatus + 1; echo "<br><Strong>ERROR </Strong>"; echo $errorstatus; echo ":"; echo "<b>No Day entered</b>"; } Thanks! Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 php has an elseif statement - use it (or a switch statement) instead of the c-style else if. Also, it would be easier to read if you used strtolower($_POST["Day"]) == 'monday' instead of $_POST["Day"] == 'Monday' || $_POST['Day'] == 'monday' if you do not care about case. EDIT: that's not to say this code couldn't be made more efficient (it can) Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 21, 2008 Share Posted September 21, 2008 No there's no limit to the number of else if's Your may be better of using: if(isset($_POST['Day']) && trim($_POST['Day']) != '') { switch(strtolower($_POST['Day'])) { case 'monday': $_POST['Day2'] = 0; break; case 'tuesday': $_POST['Day2'] = 1; break; case 'wednesday': $_POST['Day2'] = 2; break; case 'thursday': $_POST['Day2'] = 3; break; case 'friday': $_POST['Day2'] = 4; break; case 'saturday': $_POST['Day2'] = 5; break; case 'sunday': $_POST['Day2'] = 6; break; } } else { $errorstatus = $errorstatus + 1; echo "<br><Strong>ERROR </Strong>"; echo $errorstatus . ':'; echo "<b>No Day entered</b>"; } Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 Thanks, My problem wasn't a problem it was a problem in my error checking! Whats this I hear about elseif and not else if? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 it functions the same as else if... else if is just using if/else to nest other if/elses.. if bool do something else if bool do something else if bool etc. elseif is actually a statement that continues the same if statement. Not that it matters, since as wildteen pointed out you should favor switches over multiple elseif statements. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 Interesting, Whats the advantage in using switches over multiple else ifs? (seems to be the same amount of code to write) Quote Link to comment Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 <?php $days = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday'); if (in_array(strtolower($_POST['day']), $days)) { $_POST['day2'] = array_keys($days,$_POST['day']); } else { $errorstatus = $errorstatus + 1; echo "<br><Strong>ERROR </Strong>"; echo $errorstatus . ':'; echo "<b>No Day entered</b>"; } ?> I win. edit: modified to add strtolower forgot about that. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 <?php $days = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday'); if (in_array($_POST['day'], $days)) { $_POST['day2'] = array_keys($days,$_POST['day']); } else { $errorstatus = $errorstatus + 1; echo "<br><Strong>ERROR </Strong>"; echo $errorstatus . ':'; echo "<b>No Day entered</b>"; } ?> I win. Even better, thanks! Quote Link to comment Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 I edited it just now, forgot about strtolower. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 Hah, I actually wrote <?php $days = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ); $day = trim(strtolower($_POST['day'])); $key = array_keys($days, $day); if(!empty($key)) { $_POST['day2'] = $key[0]; } else { $errorstatus += 1; echo "<br><Strong>ERROR </Strong>$errorstatus:<b>No Day entered</b>"; } ?> in case he asked how to make it more efficient (as I posted you could) but he didn't... it's pretty similar to yours though. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 21, 2008 Share Posted September 21, 2008 Beat this: $day = "monday"; $dayNumber = date("N",strtotime($day)); Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 interesting, what does that do? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 Beat this: $day = "monday"; $dayNumber = date("N",strtotime($day)); Hahaha pwned. Except that he wants monday as 0 not 1, and so on. Not really sure why he is even needing it to be trimmed or strtolowered. Ideally an input like that should be from a dropdown or radio button, not a textfield. I hope you know Mr. OP, lots of people can't spell the days of the week correctly, so you're either going to have to a) write lots of conditions to look for mispellings, trying to guess what the user meant b) keep handing the user your existing error message until they get it right c) use a dropdown or radio button where the values are already coded Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 It returns 1-7 based upon the day. You'd need to subtract 1, check to see if strtotime() returned an error if the day wasn't of the week (which is a possibility in this context) etc... it's a little limited in its ability to be used, which is why I, and probably purple, didn't use that method. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 It returns 1-7 based upon the day. You'd need to subtract 1, check to see if strtotime() returned an error if the day wasn't of the week (which is a possibility in this context) etc... it's a little limited in its ability to be used, which is why, and probably purple, didn't use that method. Nah I'll admit I didn't think of it. However, add in all the error checking and it's barely better than what we had. Requires php5 tho. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 I'm always afraid of sticking in what a user posts into strtotime()... they can put in much more than monday-sunday (yesterday, 5/21/87, 1/1/1 etc) that would work without error. I'm also incredibly paranoid and if a vulnerability is found in the strtotime() parser it could be easily exploited. It's a clever method though, and I love php's strtotime() function Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 21, 2008 Share Posted September 21, 2008 It returns 1-7 based upon the day. You'd need to subtract 1, check to see if strtotime() returned an error if the day wasn't of the week (which is a possibility in this context) etc... it's a little limited in its ability to be used, which is why I didn't use that method. Ok, ok... this checking for proper input will beat it down here... And it'll work for English only I suppose. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 yes, but after proper checking it would likely be easier to read than mine and violet's implementations. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 yes, but after proper checking it would likely be easier to read than mine and violet's implementations. whoa wait I didn't say all that. I had to go look up in the manual what "N" was. That's a deal killer. I just conceded it was shorter Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 21, 2008 Share Posted September 21, 2008 I didn't say you thought it was easier to read just that I did . Mainly because I knew there was a setting in date() to get the day of the week and I figured that since he used N it was the directive (I didn't know what it was, I just assumed). I suppose if you have no idea what the script is supposed to do it may make it a little difficult to read if you're not a date() expert (I don't know ANYONE that has all of them memorized)... Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 Beat this: $day = "monday"; $dayNumber = date("N",strtotime($day)); Hahaha pwned. Except that he wants monday as 0 not 1, and so on. Not really sure why he is even needing it to be trimmed or strtolowered. Ideally an input like that should be from a dropdown or radio button, not a textfield. I hope you know Mr. OP, lots of people can't spell the days of the week correctly, so you're either going to have to a) write lots of conditions to look for mispellings, trying to guess what the user meant b) keep handing the user your existing error message until they get it right c) use a dropdown or radio button where the values are already coded HAHA Thanks! -I've been making loads of conditions!!!! - Silly me I should have thought a drop down box! - Ah well Live and Learn Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted September 21, 2008 Author Share Posted September 21, 2008 Thanks for everyones input, Although its been a good discussion - I better close it (We're already off topic!) Thanks again, 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.