代码之家  ›  专栏  ›  技术社区  ›  techDigi

有人能解释一下下面的代码吗?

  •  -5
  • techDigi  · 技术社区  · 10 年前

    现在我的问题是:

    1) 订单的用途。php和他为什么创建?以及如何使用Object存储$order的Refence?

    2) 请描述代码(我知道注释已经存在,但我需要帮助) 现在是Main.php:

    <?php
    include("Order.php");
    include("connect.php");
    $query="SELECT * FROM `orders`";
    $filter_Result=mysqli_query($con,$query);
    $newOrders=Array();
    $items = array();     
    
    while($row=mysqli_fetch_array($filter_Result))
    {
    $order;
    $orderId= $row['id']; //fetch row id
    echo "hello".$orderId;
    if(in_array($orderId,$newOrders,true)){
     //we already created an array object for this id..use it
     $order=<get order object from $newOrders for which id is $orderId>
    }
    else{
    $order=new Order($row['id'], $row['tableId'], $row['createdDate']);
    //$newOrders.AddToArray($order);
    array_push($newOrders,$order);
    }
    $item=new Item($row['ProductId'], $row['ProductName'], $row['Quantity']);
    
    $Order.AddItem($item);
    
    }
    foreach($order as $newOrders)
    {
    //create box
    
    
    }
    include("Modal.php");
    ?>
    

    现在订购.php:

    <?php
     class Order {
      /* Member variables */
      var $orderId;
      var $orderTime;
      var $tableNumber;
      var $items = array();   
    
      function __Order($orderId, $orderTime, $tableNumber)
      {
          $this->$orderId = $orderId;
          $this->$orderTime = $orderTime;
          $this->$tableNumber = $tableNumber;
      }   
      function AddItem($itemId, $itemName, $quantity, $personalization)   
      {
           $item = new Item($itemId, $itemName, $quantity, $personalization);
          $items[] = $item;
      }
    }
    
    class Item {
        var $productId;
        var $productName;
        var $quantity;
        var $personalization;
    
        function __Item($productId, $productName, $quantity, $personalization)
        {
            $this->$productId = $productId;
            $this->$productName = $productName;
            $this->$quantity = $quantity;
            $this->$personalization = $personalization;
        }
    }
    

    ?>

    1 回复  |  直到 10 年前
        1
  •  0
  •   Matt    10 年前

    他创建了订单。php将类元素保持在主代码之外。这是更干净的代码,更容易维护。

    以及如何使用Object存储$order的Refence? 您已将此存储在 $newOrders ?

    为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");
    ?>
    

    订单.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;
        }
    }
    ?>
    

    要回答您的问题:

    1. 对象数组只是指创建对象的新实例,然后将对象保存在数组中。你已经在用你的 $新订单 大堆您已经创建了一个新的 $order (对象),然后将其保存在数组中 $新订单 使用: array_push($newOrders,$order);

    2. 不确定你在这里要什么?这个代码是从哪里来的?这是某种教程吗?