[Solved] Can Someone Explain me the below Code?


He created the Order.php to keep the class elements external from the main code. This is cleaner code and easier to maintain.

and how can i store the Refrence of $order with the Object? You are already storing this in $newOrders?

Added comments to each line for main.php

<?php
// these includes are just bringing in code from external files. Nothing much really to say here.
include("Order.php");
include("connect.php");

//$query is just setting up the SQL query to the database for selecting all columns from a table called orders.
$query="SELECT * FROM `orders`";

//$filter_Results is just saving the query in a variable, this is used later on to fetch the data in a while loop
$filter_Result=mysqli_query($con,$query);

//$newOrders and $items = array() is just pre-defining these variables as arrays.
$newOrders=Array();
$items = array();     

//As said before, we need to use a while loop to extract the data fetched from sql where `mysqli_fetch_array` is the method of retrieving the data.
while($row=mysqli_fetch_array($filter_Result))
{
    $order; //Not sure what the next line is doing.. `$order;` doesn't do anything..
    $orderId= $row['id']; //Saving the column name `id` as a variable $orderID
    echo "hello".$orderId; //echo out the hello# where # is the orderID

    //This is checking if the orderID retrieved from sql has already been placed inside the $newOrders array returning true or false.
    if(in_array($orderId,$newOrders,true)){

        //Some more code needs to be added here, I'm guessing you need to add in something to find the object relating to the `orderID` that already exists in `$newOrders`
        $order=<get order object from $newOrders for which id is $orderId>
    }

    // If $orderID not in $newOrders
    else{
        // Create a new instance of Order class called $order
        $order=new Order($row['id'], $row['tableId'], $row['createdDate']);

        //Add this order to the array $newOrders
        array_push($newOrders,$order);
    }
    // Create a new instance of the Item class called $item takinging in the columns ProductId, ProductName, Quantity
    $item=new Item($row['ProductId'], $row['ProductName'], $row['Quantity']);

    // Using a method from orders called AddItem (this can be found in Order.php under the order class
    $Order.AddItem($item);
}

// Looping through each order inside $newOrders (although this seems wrong, should be foreach($newOrders as $order)
foreach($order as $newOrders)
{
//create box
}

/ Finally including some more code inside the Modal.php file
include("Modal.php");
?>

Order.php

<?php

// Class called Order
 class Order {

  // properties of the class.
  var $orderId;
  var $orderTime;
  var $tableNumber;
  var $items = array();   

  // Function inside the method which fills the properties upon creating a new instance the class `$order=new Order($row['id'], $row['tableId'], $row['createdDate']);` 
  function __Order($orderId, $orderTime, $tableNumber)
  {
      // Using the parameters passed to the function to fill the properties of the class
      $this->$orderId = $orderId;
      $this->$orderTime = $orderTime;
      $this->$tableNumber = $tableNumber;
  }   

  //Function called AddItem which takes parameters and fills an items array however this should be using $this->
  function AddItem($itemId, $itemName, $quantity, $personalization)   
  {
       $item = new Item($itemId, $itemName, $quantity, $personalization);
      $items[] = $item;
  }
}

// New class called Item
class Item {

    // properties of the class.
    var $productId;
    var $productName;
    var $quantity;
    var $personalization;

    // Same as above: Function inside the method which fills the properties upon creating a new instance the class
    function __Item($productId, $productName, $quantity, $personalization)
    {
        $this->$productId = $productId;
        $this->$productName = $productName;
        $this->$quantity = $quantity;
        $this->$personalization = $personalization;
    }
}
?>

To answer your question:

  1. An array of objects just means you are creating a new instance of the object and then saving the object in an array. You are already doing this with your $newOrders array. You have created a new $order (the object) and then saved it in the array $newOrders using: array_push($newOrders,$order);

  2. Not really sure what you are asking for here? Where has this code come from? Is this a tutorial of some sort?

4

solved Can Someone Explain me the below Code?