【发布时间】:2014-11-22 15:41:03
【问题描述】:
PHP MYSQL - 如何仅计算每天的价格总和?如何判断新的一天何时从 0 重新开始计数。有没有办法说,现在是第 26 天,让我们从 0 重新开始计数,以便单独保存信息。在我的 SQL 表中,我有两列 Price 和 Lastvisit,25 日的最终结果应该是 289,而 26 日的总结果应该是 44+90+10 所以 144。如果可以,请提供帮助。谢谢。
- 价格
- 20
- 50
- 65
- 55
- 50
- 15
- 34
- 44
- 90
10
上次访问
- 2014-09-25
- 2014-09-25
- 2014-09-25
- 2014-09-25
- 2014-09-25
- 2014-09-25
- 2014-09-25
- 2014-09-26
- 2014-09-26
-
2014-09-26
<?php $date_start = date("Y/m/d"); //Todays Date $end_date = date ("Y/m/d", strtotime("last month", strtotime($date_start))); //30 days prior while (strtotime($date_start) >= strtotime($end_date)) { //run through each of the 30 days $date_start = date ("Y/m/d", strtotime("-1 day", strtotime($date_start))); //remove 1 day at a time. //echo $date_start . "<br>"; $lastthirtydays .= substr($date_start,8)."|"; //explode each day to plug it in later $linegraphq = "SELECT History.Price,History.Lastvisit FROM History WHERE Lastvisit='$date_start'"; //sql $linegraphqresult = mysql_query($linegraphq); ////query while($row = mysql_fetch_array($linegraphqresult)){ //get all results from query $dayofprice .= substr($row['Lastvisit'],8); //explode each DAY # only. //echo "COST: " . $row['Price'] . "DATE" . $row['Lastvisit'] . "<br>"; //this is just the info im working with. $finalcost += $row['Price']; //this is summing all of the 30 days pricing $pricetoday .= $row['Price'] . "|" . $row['Lastvisit']. "|"; //now you can tell im getting lost... } } echo $dayofprice; //TEST echo $pricetoday; //TEST $explodedmonth = explode('|',$lastthirtydays); $explodedprice = explode('|',$pricetoday); //echo $explodedprice[1]; //echo $finalcost; ?>
【问题讨论】:
-
为什么我们要处理表格 RBAR 中的行(逐行处理)?
-
呃,我知道。这段代码是我沮丧的产物。感谢 John,新的完整代码总共 10 行。感谢您的光临。