0

I've been looking for hours trying to find a solution. It has to be a simple one, yet, I can't find it. All works well without the if-statement. Each row gets its button. But whenever I put the if-statement back in, the buttons dissappear. What am I doing wrong? Am I overlooking the obvious?

    foreach($result as $row) {

            echo "<tr>
                    <td>".date('d F Y', strtotime($row['DATE']))."</td>
                    <td>".date('H:i  ', strtotime($row['TIME']))."</td>
                    <td>".$row['NAME']."</td>
                    <td>";
                    if ($row['availability'] == 0) { echo "FULL"; } 
                    else if ($row['availability'] != 0) { echo $row['availability']; };
                    "</td>
                    <td> <form method='post' action='res2.php'>
                    <input type='submit' name='action' value='Next'>
                    <input type='hidden' name='ID' value='".$row['ID']."'>
                    </form> </td>
                    </tr>";
        }
4
  • 1
    You've finished the echo before the if but never restart it, so everything after is just a string, not attached to anything nor output. Commented Jan 7, 2021 at 16:29
  • 2
    In other words, you need another echo after the if/else block. Commented Jan 7, 2021 at 16:30
  • 1
    You could also use the ternary operator instead of an if/else statement. Commented Jan 7, 2021 at 16:31
  • 1
    General advice: when the else if condition is the opposite of the if condition, you should just use else. Commented Jan 7, 2021 at 16:32

1 Answer 1

1

You can use the conditional operator to concatenate different strings depending on the availability.

foreach($result as $row) {
    echo "<tr>
              <td>".date('d F Y', strtotime($row['DATE']))."</td>
              <td>".date('H:i  ', strtotime($row['TIME']))."</td>
              <td>".$row['NAME']."</td>
              <td>" . ($row['availability'] == 0 ? "FULL" : $row['availability']) . "</td>
              <td> <form method='post' action='res2.php'>
                   <input type='submit' name='action' value='Next'>
                   <input type='hidden' name='ID' value='".$row['ID']."'>
                   </form> 
              </td>
          </tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

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.