在许多情况下,将多行压缩为一行
Condense multiple rows into one single, for many instances
我有一个 table
叫 Orders
,另一个叫 orderProducts
。
orderProducts
包含一个 CustomerID
字段,listCustomerOrders()
然后在 DB
中查找匹配的行。返回的输出如下:
array() {
[0]=>
object(Basket)#3 (11) {
["ProductName"]=>
string(17) "Green Pesto Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[1]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Cheese Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[2]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Salt Beef Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[3]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Veggie Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[4]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Chicken Bagel"
["completeDate"]=>
string(19) "2015-03-25 20:30:00"
}
[5]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Cheese Salad"
["completeDate"]=>
string(19) "2015-03-25 20:30:00"
}
}
并显示为;
------1st order, date:02-25-15 11:00-------
Green Pesto Bagel
Cheese Bagel
Salt Beef Bagel
Veggie Bagel
------2nd order, date:03-25-15 11:00-------
Chicken Bagel
Cheese Salad
我想知道如何处理每个 orderDate
输出一个结果,同时迭代每个 row
以获得任何其他产品并将它们添加到数组中。
我可以使用某种计数器来遍历每个代码块,并在满足条件时继续下一个代码块吗?
函数到此,干杯!
function myAccount(){
require_once('classes/basket.php');
// pull the customers order
$CustomerID = $_SESSION['CustomerID'];
$Limit = 20;
$results = Basket::listCustomersOrders($CustomerID, $Limit);
$orders['orders'] = $results['results'];
include('templates/myAccount.php');
}
public static function listCustomersOrders($CustomerID, $Limit){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT Orders.OrderID, Orders.payPalRef, Orders.SubTotal, Orders.completeDate,
OrderProducts.Quantity, OrderProducts.ProductName, OrderStatus.Status
FROM Orders
LEFT OUTER JOIN OrderProducts
ON Orders.OrderID = OrderProducts.OrderID
LEFT OUTER JOIN OrderStatus
ON Orders.OrderStatusID = OrderStatus.OrderStatusID
WHERE CustomerID = :CustomerID AND (Status = 'Cash on Delivery' or Status = 'PayPal') ORDER BY CompleteDate DESC LIMIT :Limit";
$st = $conn->prepare( $sql );
$st->bindValue( ":CustomerID", $CustomerID, PDO::PARAM_INT );
$st->bindValue( ":Limit", $Limit, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$basket = new Basket( $row );
$list[] = $basket;
}
//var_dump($list);
$conn = null;
return ( array ( "results" => $list ) );
}
$lastdate = null;
$ordercnt = 1;
foreach ($orders[´orders´] as $row) {
if ($lastdate != $row[´completeDate´]) {
echo '--Order #'.$ordercnt++.' Date '.$row[´completeDate´].'--<br />';
$lastdate = $row[´completeDate´]
}
echo $row[´ProductName´].'<br />';
}
感谢 Peters inout,我创建了一个脚本来完成这项工作。
// Init the counters and vars
$lastdate = null;
$ordercnt = 1;
$output = "";
while ($row = $st->fetch()) {
// if CompleteDate exists proceed
if ($lastdate != $row['completeDate']) {
if($lastdate != null){
$output .="
<form action=\"reorder\" method=\"post\">
<button value=\"".$row['OrderID']."\" name=\"OrderID\" class=\"green-button\" style=\"width: 110px;float: right;margin-top: -40px;\">Order</button>
</form>
</ul><hr>";
}
$output .= '<ul class="previousOrderList"><li>Order #'.$ordercnt++.', Date: '.$row['completeDate'].'-- Total '.$row['SubTotal'].'</li>';
$output .= '<li>'.$row['Status'].' -- Total: '.$row['SubTotal'].'</li>';
$lastdate = $row['completeDate'];
}
$output .='<li>'.$row['ProductName']." x ".$row['Quantity'].'</li>';
}
if ($output){$output .= "</ul>";} // if there is results, add the end of line here
$conn = null;
return $output;
}
我有一个 table
叫 Orders
,另一个叫 orderProducts
。
orderProducts
包含一个 CustomerID
字段,listCustomerOrders()
然后在 DB
中查找匹配的行。返回的输出如下:
array() {
[0]=>
object(Basket)#3 (11) {
["ProductName"]=>
string(17) "Green Pesto Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[1]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Cheese Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[2]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Salt Beef Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[3]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Veggie Bagel"
["completeDate"]=>
string(19) "2015-02-25 11:00:00"
}
[4]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Chicken Bagel"
["completeDate"]=>
string(19) "2015-03-25 20:30:00"
}
[5]=>
object(Basket)#4 (11) {
["ProductName"]=>
string(12) "Cheese Salad"
["completeDate"]=>
string(19) "2015-03-25 20:30:00"
}
}
并显示为;
------1st order, date:02-25-15 11:00-------
Green Pesto Bagel
Cheese Bagel
Salt Beef Bagel
Veggie Bagel
------2nd order, date:03-25-15 11:00-------
Chicken Bagel
Cheese Salad
我想知道如何处理每个 orderDate
输出一个结果,同时迭代每个 row
以获得任何其他产品并将它们添加到数组中。
我可以使用某种计数器来遍历每个代码块,并在满足条件时继续下一个代码块吗?
函数到此,干杯!
function myAccount(){
require_once('classes/basket.php');
// pull the customers order
$CustomerID = $_SESSION['CustomerID'];
$Limit = 20;
$results = Basket::listCustomersOrders($CustomerID, $Limit);
$orders['orders'] = $results['results'];
include('templates/myAccount.php');
}
public static function listCustomersOrders($CustomerID, $Limit){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT Orders.OrderID, Orders.payPalRef, Orders.SubTotal, Orders.completeDate,
OrderProducts.Quantity, OrderProducts.ProductName, OrderStatus.Status
FROM Orders
LEFT OUTER JOIN OrderProducts
ON Orders.OrderID = OrderProducts.OrderID
LEFT OUTER JOIN OrderStatus
ON Orders.OrderStatusID = OrderStatus.OrderStatusID
WHERE CustomerID = :CustomerID AND (Status = 'Cash on Delivery' or Status = 'PayPal') ORDER BY CompleteDate DESC LIMIT :Limit";
$st = $conn->prepare( $sql );
$st->bindValue( ":CustomerID", $CustomerID, PDO::PARAM_INT );
$st->bindValue( ":Limit", $Limit, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$basket = new Basket( $row );
$list[] = $basket;
}
//var_dump($list);
$conn = null;
return ( array ( "results" => $list ) );
}
$lastdate = null;
$ordercnt = 1;
foreach ($orders[´orders´] as $row) {
if ($lastdate != $row[´completeDate´]) {
echo '--Order #'.$ordercnt++.' Date '.$row[´completeDate´].'--<br />';
$lastdate = $row[´completeDate´]
}
echo $row[´ProductName´].'<br />';
}
感谢 Peters inout,我创建了一个脚本来完成这项工作。
// Init the counters and vars
$lastdate = null;
$ordercnt = 1;
$output = "";
while ($row = $st->fetch()) {
// if CompleteDate exists proceed
if ($lastdate != $row['completeDate']) {
if($lastdate != null){
$output .="
<form action=\"reorder\" method=\"post\">
<button value=\"".$row['OrderID']."\" name=\"OrderID\" class=\"green-button\" style=\"width: 110px;float: right;margin-top: -40px;\">Order</button>
</form>
</ul><hr>";
}
$output .= '<ul class="previousOrderList"><li>Order #'.$ordercnt++.', Date: '.$row['completeDate'].'-- Total '.$row['SubTotal'].'</li>';
$output .= '<li>'.$row['Status'].' -- Total: '.$row['SubTotal'].'</li>';
$lastdate = $row['completeDate'];
}
$output .='<li>'.$row['ProductName']." x ".$row['Quantity'].'</li>';
}
if ($output){$output .= "</ul>";} // if there is results, add the end of line here
$conn = null;
return $output;
}