skip to Main Content

I’m pretty new to PHP, working on a school project. I’m trying to connect this if statement together. How do I make it possible to send multiple messages to admin-add-order.php? I want the code to go through all the conditions first before sending the message and exiting. What’s the correct way of combining these together? At the moment it can only show one message at a time.

doAddOrder.php

  $boolean=false;
  $msg="";

    if ($origin == $destination){
      $msg+=1;
      $boolean=true;
    }
  
    if ($stuffing_date > $dep_date){
      $msg+=2;
      $boolean=true;
    }

    if ($dep_date > $arr_date){
      $msg+=3;
      $boolean=true;
    }

    if ($boolean=true){
      header("location:admin-add-order.php?msg=$msg");
      exit;
    }

admin-add-order.php

 <?php if (isset($_GET["msg"])){
        if ($_GET["msg"] == 1) echo "Origin and destination must be different";
        if ($_GET["msg"] == 2) echo "Departure date cannot be earlier than stuffing date";
        if ($_GET["msg"] == 3) echo "Arrival date cannot be earlier than departure date";
 } ?>

I tried exiting after each if, but that didn’t work. I’m not sure how to echo multiple messages to admin-add-order.php. I keep only getting one msg no matter how I tried to add to $msg.

This is my first time asking here. Would appreciate all your help!

2

Answers


  1. You can use bitwise operator

    doAddOrder.php

    $msg = 0;
    
    if ($origin == $destination){
      $msg |= 2;
    }
    
    if ($stuffing_date > $dep_date){
      $msg |= 4;
    }
    
    if ($dep_date > $arr_date){
      $msg |= 8;
    }
    
    if ($msg > 0){
      header("location:admin-add-order.php?msg=$msg");
      exit;
    }
    

    admin-add-order.php

    if (isset($_GET["msg"]) && is_numeric($_GET["msg"])){
        if ($_GET["msg"] & 2) echo "Origin and destination must be different";
        if ($_GET["msg"] & 4) echo "Departure date cannot be earlier than stuffing date";
        if ($_GET["msg"] & 8) echo "Arrival date cannot be earlier than departure date";
    }
    
    Login or Signup to reply.
  2. This an other way to do it using letters instead of numbers which is easy to handle :

    $msg = '';
    if ($origin == $destination){
      $msg.='A';
    }
    
    if ($stuffing_date > $dep_date){
      $msg.='B';
    }
    
    if ($dep_date > $arr_date){
      $msg.='C';
    }
    

    admin-add-order.php

    <?php 
    if (isset($_GET["msg"])){
            $msg = $_GET["msg"]
            if (str_contains($msg, 'A')) echo "Origin and destination must be different";
            if (str_contains($msg, 'B')) echo "Departure date cannot be earlier than stuffing date";
            if (str_contains($msg, 'C')) echo "Arrival date cannot be earlier than departure date";
     } 
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search