Jump to content

help with, ELSE IF, Issue.


Darren_Stanley

Recommended Posts

I have a working IF statement, but then when I add a 'ELSE IF' to this this 'overwrites' my IF.

I will try to explain more here.

 

For the purpose of this example,

getNewLine() returns 6. 

$noOfLines = 58.

$rotaDate = 5th Jan 2014

$line = from 1 to 58.

 

My initial IF statement gives correct output (adding 1 week to each line):-

//This function determines and inserts the custom column data.
  function customData($line){
  global $rotaDate,$noOfLines;

       if($line >= getNewLine()){
            $weeks=($line-getNewLine());
            $modifier=$weeks.' weeks';
            $rotaDate->modify($modifier);
        return $rotaDate ->format("jS F Y");
          }

1  

2  

3  

4  

5

6   05-Jan-14 

7  12-Jan-14 

8  19-Jan-14  

9  26-Jan-14  

10 02-Feb-14 

...

...

...

...

56  21-Dec-14 

57  28-Dec-14

58  04-Jan-15 

 

So far, So good..

But now, I need to continue from 4-Jan line 58, back up to line 1, 1week later (11-Jan) down to line 5 of which would be 8th Feb.  

 

i.e. all lines Less than getNewLine() // which is 6

 

To do this I have added ELSE IF:-

 /*      else if($line<getNewLine()) {
            $weeks=($line+$noOfLines-getNewLine());
            $modifier=$weeks.' weeks';
            $rotaDate->modify($modifier);
        return $rotaDate ->format("jS F Y");
     }
*/

This the outputs from line 1 to line 5 correctly, But it continues to overwrite the dates down to line 58. It needs to stop at line 6.

1   11-Jan-15  

2   18-Jan-15  

3   25-Jan-15  

4   01-Feb-15 

5   08-Feb-15 

6   15-Feb-15  

7   22-Feb-15  

8   01-Mar-15 

9   08-Mar-15  

10   15-Mar-15  

11   22-Mar-15   

...

...

...

... down to line 58...

 

Any ideas why this is happening and how I may stop this please?

Edited by Darren_Stanley
Link to comment
Share on other sites

Look at the sample below taken from PHP manual page:

 <?php

/* Incorrect Method: */
if($a > $b):
    echo $a." is greater than ".$b;
else if($a == $b): // Will not compile.
    echo "The above line causes a parse error.";
endif;


/* Correct Method: */
if($a > $b):
    echo $a." is greater than ".$b;
elseif($a == $b): // Note the combination of the words.
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>
Link to comment
Share on other sites

I have modified the IFELSE statement in order to debug to return $Line if <6.

with an un-expected outcome.

elseif($line<getNewLine()) {
            $weeks=($line+$noOfLines-getNewLine());
            $modifier=$weeks.' weeks';
            $rotaDate->modify($modifier);
        return $line;//$rotaDate ->format("jS F Y");
     }

Only the first line gets the $line number from the "if else" statement.

The remaining lines less than 6, get the modified $rotaDate, each time this function is called.

Link to comment
Share on other sites

You missed:

1. Colon at end of if statement and at end of elseif statement.

2. You missed the endif; statement at the last.

 

It sounds like you're describing the alternate syntax for if statements:

http://www.php.net/manual/en/control-structures.alternative-syntax.php

 

You can also use curly brackets:

http://www.php.net/manual/en/control-structures.elseif.php

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.