现在我的问题是:
1)Order.php的用途以及他创建的原因是什么?以及如何使用Object存储$ 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;
}
}
&GT?;
答案 0 :(得分:0)
他创建了Order.php,以便将类元素保留在主代码之外。这是更简洁的代码,更易于维护。
以及如何使用Object存储$ order的参考?您已将其存储在$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");
?>
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;
}
}
?>
回答你的问题:
对象数组只表示您正在创建对象的新实例,然后将对象保存在数组中。您已使用$newOrders
数组执行此操作。您已创建新的$order
(对象),然后使用以下内容将其保存在数组$newOrders
中:array_push($newOrders,$order);
不确定你在这里要求什么?这段代码来自哪里?这是某种教程吗?