【问题标题】:Can Someone Explain me the below Code?有人可以解释一下下面的代码吗?
【发布时间】:2016-09-28 05:11:46
【问题描述】:

现在我的问题是:

1)Order.php有什么用,他为什么要创建?以及如何将 $order 的引用与对象一起存储?

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

现在 Order.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;
    }
}

?>

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    他创建了 Order.php 以将类元素保留在主代码之外。这是更简洁的代码,更易于维护。

    我怎样才能将 $order 的引用与对象一起存储?您已经将其存储在 $newOrders 中?

    在 main.php 的每一行添加 cmets

    <?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. 对象数组只是意味着您正在创建对象的新实例,然后将对象保存在数组中。您已经在使用 $newOrders 数组执行此操作。您创建了一个新的$order(对象),然后将其保存在数组$newOrders 中,使用:array_push($newOrders,$order);

    2. 不确定您在此处要求什么?这段代码是从哪里来的?这是某种教程吗?

    【讨论】:

    • 先生,我有一个问题,你能描述一下下面两行的含义吗?我该怎么做:1)//声明“订单”对象的数组 - 你创建的那个php。 2) //您可能想在 php 中使用 对数组类型 - 如果它可以用作哈希表。
    • @DevelopmentTechlab 在我的答案底部添加了答案;但是不确定您在 2) 中要问什么
    • 如果我想在 main .php 中创建 'Order' 对象的数组 ........ $Order = array(); $订单=新订单();所以是对还是错
    • @Development Techlab 你已经有了一个对象数组,为什么还要再做一个?但否则$Order[] = new Order(); 就足够了
    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多