0

How can I change my "if" statement based a variable without writing a new statement for each case? My select dropdown "timeline" will be populated with 25+ options so I want to make the if statement in the php script

HTML to set variable:

<p>Current Status: </p> <select name="timeline" id="timeline">
            <option value="completed" selected>Completed</option>
            <option value="active">Active</option>
</select>

PHP:

     $current_state = $_POST['timeline'];
     $current = strtotime("now");

while($row = mysql_fetch_array($results)){




        if($current_state == "completed"){
             $end = strtotime($row['End']);

             $my_if = "if($current > $end){";

        }

        if($current_state == "active"){

           $end = strtotime($row['End']);
           $start = strtotime($row['Start']);

           $my_if = "if($start < $current && $end > $current){";

        }
                //THIS IS WHERE THE IF STATEMENT WOULD BE USED
                echo $my_if;

                            echo '<tr>
                            <td>'. $row['ID']  .'</td>
                            <td>'. $row['Name']  .'</td>
                            <td>'. $row['LastName']  .'</td>

                        </tr>';
                }
}
0

2 Answers 2

2

You should rework your logic completely

$completed = $_POST['timeline'] == 'completed';
while($row = mysql_fetch_array($results)) {
    $end = strtotime($row['End']);
    if (!$completed)
      $start = strtotime($row['Start']);

    if (
        ($completed  && $current > $end) ||
        (!$completed && $start < $current && $end > $current)
    ) {
      // do stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Include the conditions of your "meta-if" into the if itself:

if ($current_state == "completed")
    {
    $end = strtotime($row['End']);
    }

if ($current_state == "active")
    {
    $end = strtotime($row['End']);
    $start = strtotime($row['Start']);
    }

if (($current_state == "completed" && $current > $end) || ($current_state == "active" && $start < $current && $end > $current))
    {
    echo '<tr>
    <td>'. $row['ID']  .'</td>
    <td>'. $row['Name']  .'</td>
    <td>'. $row['LastName']  .'</td>
    </tr>';
    }

7 Comments

This is actually slower due to the extra if statements.
The slowndown from a couple of extra if-statements is neglible.
Especially compared to the IO done in mysql_fetch_array() for every time these ifs are interpreted.
@Zombaya - If you adopt the habit of assuming the trade-off is negligible, across a project containing thousands of lines of code your overall performance will suffer, it is poor programming practice to work this way.
@Geoffrey: You're optimizing prematurely. Surely your PHP application is not that performance critical that you need to start worrying about a couple of if statements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.