希望有人可以帮助我正在尝试建立一个每日销售表,并显示一个表格,每个销售人员可以每周查看他们的每日结果。我确定它并不过分困难,但我一直在摸索这个问题,我是 php 新手,MySQL 一直在寻找我的方式,虽然没问题,但这让我
我试图达到的结果看起来像这样。
-------------------------------------------------
|Name | sun | mon | tue | wed | thu | fri | sat |
-------------------------------------------------
|pete | 0 | 0 | 0 | 200 | 200 | 0 | 0 |
-------------------------------------------------
|mike | 0 | 0 | 0 | 0 | 100 | 0 | 100 |
-------------------------------------------------
我使用的表的一个例子是......
------------------------------------------------
| order_number | order_value | user_name | Date |
-------------------------------------------------
| 1 | 100 | pete | 10/7 |
-------------------------------------------------
| 2 | 200 | mike | 13/7 |
-------------------------------------------------
| 3 | 100 | pete | 10/7 |
-------------------------------------------------
| 4 | 100 | mike | 11/7 |
-------------------------------------------------
| 5 | 200 | peter | 11/7 |
-------------------------------------------------
我目前使用的代码如下 不确定我是否最好通过 php 代码或使用更好的 SQL 查询来处理这些数据。
$sql = mysqli_query($mysqli," SELECT SUM(order_value) , DAYOFWEEK(Date) , `user_name`
FROM `my_table`
WHERE WEEK(Date) = $thisweek -1 AND YEAR(Date) = $currentYear
GROUP BY user_name , DAYOFWEEK(Date)");
echo "
<table border ='1'>
<tr><td>Name</td><td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thu</td><td>fri</td><td>sat</td></tr>";
while($result=mysqli_fetch_array($sql))
{
$i = $result['DAYOFWEEK(Date)'];
$v = $result['SUM(order_value)'];
$n = $result['user_name'];
$d = $i;
echo "<td>" . $n . "</td>";
for($d=1; $d<=7; $d++ )
{
if($v >0 && $d == $i)
{
echo "<td>" . $v . "</td>";
}
else
{
echo "<td>0</td>";
}
}
echo"</tr>";
}
echo "</table>";
从这段代码中,我得到的结果很接近,但我希望它变平,所以每个用户只在表格中出现一次,
-------------------------------------------------
|Name | sun | mon | tue | wed | thu | fri | sat |
-------------------------------------------------
|pete | 0 | 0 | 0 | 200 | 0 | 0 | 0 |
-------------------------------------------------
|pete | 0 | 0 | 0 | 0 | 200 | 0 | 0 |
-------------------------------------------------
|mike | 0 | 0 | 0 | 0 | 100 | 0 | 0 |
-------------------------------------------------
|mike | 0 | 0 | 0 | 0 | 0 | 0 | 100 |
-------------------------------------------------
任何帮助或建议将不胜感激。