Jump to content

[SOLVED] PHP - Limit number of else if's?


grahamb314

Recommended Posts

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!

Link to comment
Share on other sites

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) ;)

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.