尝试通过一个通过 MySQL 查询提供的数组生成一个表,我们提取一个查询中所需的所有数据并将其存储到一个数组中。
数组片段(它目前有超过 40 个子数组,但这是一个示例)
Array
(
[0] => Array
(
[Date] => October-2013
[TotalPaid] => 100.00
[OrderStatus] => 1
)
[1] => Array
(
[Date] => October-2013
[TotalPaid] => 150.00
[OrderStatus] => 2
)
我们想要的结果是生成一个看起来像的表
Date | Processing | Complete | Deleted
October 2013 £150 £100 £0
September 2013 £400 £200 £50
等等可以追溯到(x)年。
我已经设法让表格显示正确的行,但是我们遇到了当月没有任何完整交易的情况的问题,因此上述所需的输出最终像
Date | Processing | Complete | Deleted
October 2013 £150 £100 £0
September 2013 £400 £50
代码是
$MasterSQL = "SELECT DATE_Format(DTStamp, ('%M-%Y')) AS 'Date', SUM(TotalPaid) AS 'TotalPaid', OrderStatus FROM bs_orders GROUP BY OrderStatus, EXTRACT(YEAR_MONTH FROM DTStamp) ORDER BY OrderStatus ASC, DTStamp DESC";
$MasterData= $db->getResults($MasterSQL);
$Table = '';
$Table .= '<h2>Page Title</h2>';
$Table .= '<p>Some Text</p>';
$Table .= '<table id="Report">';
$Table .= '<tr>';
$Table .= '<th>Pending</th>';
$Table .= '<th>Complete</th>';
$Table .= '<th>Problem</th>';
$Table .= '<th>WP Complete</th>';
$Table .= '<th>Deleted</th>';
$Table .= '</tr>';
$DataArray = array();
while($row = mysql_fetch_assoc($MasterData)){
$DataArray[] = $row;
}
$DateArr = array();
foreach($DataArray as $Date){
if(!in_array($Date['Date'], $DateArr))
{
$Table .= '<tr>';
$Table .= '<td>'.$Date['Date'].'</td>';
foreach($DataArray as $data){
if($data['Date'] == $Date['Date']){
$DateArr[] = $Date['Date'];
if($data['OrderStatus'] == 1){
$Table .= '<td>£'.$data['TotalPaid'].'</td>';
}
else
if($data['OrderStatus'] == 4){
$Table .= '<td>£'.$data['TotalPaid'].'</td>';
}
else
if($data['OrderStatus'] == 3){
$Table .= '<td>£'.$data['TotalPaid'].'</td>';
}
else
if($data['OrderStatus'] == 2){
$Table .= '<td>£'.$data['TotalPaid'].'</td>';
}
else
if($data['OrderStatus'] == 6){
$Table .= '<td>£'.$data['TotalPaid'].'</td>';
}
} // if($data['Date'] == $Date['Date'])
} // foreach($DataArray as $data)
$Table .= '</tr>';
} // if(!in_array($Date['Date'], $DateArr))
} // foreach($DataArray as $Date)
echo '<pre>';
print_r($DataArray);
echo '</pre>';
$Table .= '<tr>';
$Table .= '<th> </th>';
$Table .= '<th>Pending</th>';
$Table .= '<th>Complete</th>';
$Table .= '<th>Problem</th>';
$Table .= '<th>WP Complete</th>';
$Table .= '<th>Deleted</th>';
$Table .= '</tr>';
$Table .= '</table>';
echo $Table;
这可能只是我忽略的一个简单修复,我尝试了许多 if else 语句的方法,但似乎没有任何效果。