php - PHP循环使用来自sqldb的数据的变量
问题描述
我正在制作过去一年每个月的总贷方和借方的 html 比较表。我从 sql 数据库表中获取数据并使用下面的代码来分离每个月的数据。
使用下面的代码,我可以获取数据并显示在我的 html 页面上。但是我必须为每个月编写单独的类似代码。这只是一个示例表,对于不同的贷记、借记类别,我有多个类似的代码。所以每次我都必须复制它,就像$credit0,$credit1....$credit11
我是 php 新手,这就是我知识有限的方式。有没有比每个月编写多个类似代码更好的方法?
<?php
//setting initial variable values are 0
$credit0 =$credit1 =$credit2 = 0;
$debit0 =$debit1 =$debit2 = 0;
//Getting 12 months details in Y-M format and assigning to different variable
$month0 = date("Y-M");
$month1 = date("Y-M", strtotime("-1 months"));
$month2 = date("Y-M", strtotime("-2 months"));
$month3 = date("Y-M", strtotime("-3 months"));
$month4 = date("Y-M", strtotime("-4 months"));
$month5 = date("Y-M", strtotime("-5 months"));
$month6 = date("Y-M", strtotime("-6 months"));
$month7 = date("Y-M", strtotime("-7 months"));
$month8 = date("Y-M", strtotime("-8 months"));
$month9 = date("Y-M", strtotime("-9 months"));
$month10 = date("Y-M", strtotime("-10 months"));
$month11 = date("Y-M", strtotime("-11 months"));
//Get the date details from sqldb and assgning to variable in Y-M format
$month_check= date('Y-M', strtotime($data['date']));
//each loop with the data from sqldb
foreach ($datas as $data){
if ($month_check == $month0) {
if ($data['entry_type'] == 'Credit') { $credit0 = $credit0 + $data['amount']; }
if ($data['entry_type'] == 'Debit' ) { $debit0 = $debit0 + $data['amount']; }
}
if ($month_check == $month1) {
if ($data['entry_type'] == 'Credit') { $credit1 = $credit1 + $data['amount']; }
if ($data['entry_type'] == 'Debit' ) { $debit1 = $debit1 + $data['amount']; }
}
if ($month_check == $month2) {
if ($data['entry_type'] == 'Credit') { $credit2 = $credit2 + $data['amount']; }
if ($data['entry_type'] == 'Debit' ) { $debit2 = $debit2 + $data['amount']; }
}
.
.
.
.
.
same format upto $month11
}
echo '<table">
<thead>
<tr>
<th>Month</th>
<th>Total Credit</th>
<th>Total Debit</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$month0.'</td>
<td>'.$credit0.'</td>
<td>'.$debit0.'</td>
</tr>
<tr>
<td>'.$month1.'</td>
<td>'.$credit1.'</td>
<td>'.$debit1.'</td>
</tr>
<tr>
<td>'.$month2.'</td>
<td>'.$credit2.'</td>
<td>'.$debit2.'</td>
</tr>
.
.
.
.
upto $month11
</tbody>
</table>';
//I have multiple similar tables for different categories
?>
更新: 使用循环,我尝试了下面的方法,但它不能正常工作
<?php
$credit0 =$credit1 =$credit2 = 0;
$debit0 =$debit1 =$debit2 = 0;
$month0 = date("Y-M");
$month1 = date("Y-M", strtotime("-1 months"));
$month2 = date("Y-M", strtotime("-2 months"));
$month3 = date("Y-M", strtotime("-3 months"));
$month4 = date("Y-M", strtotime("-4 months"));
$month5 = date("Y-M", strtotime("-5 months"));
$month6 = date("Y-M", strtotime("-6 months"));
$month7 = date("Y-M", strtotime("-7 months"));
$month8 = date("Y-M", strtotime("-8 months"));
$month9 = date("Y-M", strtotime("-9 months"));
$month10 = date("Y-M", strtotime("-10 months"));
$month11 = date("Y-M", strtotime("-11 months"));
$months = array($month0,$month1,$month2,$month3,$month4,$month5,$month6,$month7,$month8,$month9,$month10,$month11);
$nums = array(0,1,2,3,4,5,6,7,8,9,10,11);
foreach (array_combine($months, $nums) as $month => $num) {
$month_check= date('Y-M', strtotime($data['date']));
foreach ($datas as $data){
if ($month_check == $month) {
if ($data['entry_type'] == 'Credit') { $credit.$num = $credit.$num + $data['amount']; }
if ($data['entry_type'] == 'Debit' ) { $debit.$num = $debit.$num + $data['amount']; }
}
}
echo '<table">
<thead>
<tr>
<th>Month</th>
<th>Total Credit</th>
<th>Total Debit</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$month.$num.'</td>
<td>'.$credit.$num.'</td>
<td>'.$debit.$num.'</td>
</tr>
</tbody>
</table>';
}
?>
解决方案
那么首先这个:
$month0 = date("Y-M");
$month1 = date("Y-M", strtotime("-1 months"));
$month2 = date("Y-M", strtotime("-2 months"));
$month3 = date("Y-M", strtotime("-3 months"));
$month4 = date("Y-M", strtotime("-4 months"));
$month5 = date("Y-M", strtotime("-5 months"));
$month6 = date("Y-M", strtotime("-6 months"));
$month7 = date("Y-M", strtotime("-7 months"));
$month8 = date("Y-M", strtotime("-8 months"));
$month9 = date("Y-M", strtotime("-9 months"));
$month10 = date("Y-M", strtotime("-10 months"));
$month11 = date("Y-M", strtotime("-11 months"));
$months = array($month0,$month1,$month2,$month3,$month4,$month5,$month6,$month7,$month8,$month9,$month10,$month11);
$nums = array(0,1,2,3,4,5,6,7,8,9,10,11);
变成这样:
$months = array();
$nums = array();
for($i=0; $i<12; $i++) {
$month = date("Y-M", strtotime("-".$i." months"));
array_push($months, $month);
array_push($nums, $i);
}
而且我真的不明白你到底想用你的其余代码做什么:|
推荐阅读
- google-sheets - 在 Google 电子表格上将全名转换为“姓、名”格式
- c++ - 如何在 UML 中显示地图?C++
- javascript - 多次执行axios请求只得到一个输出
- dataframe - 在 Julia 中转换 DataFrame 的多列的正确方法是什么?
- r - Plotly/Vistime - 重叠标签
- python-3.x - 混淆矩阵 - 当真/预测列都有值时的 TP
- api - 如果我的 API 受 OAuth 保护,我是否需要 Web 应用程序防火墙?
- python - 关于DataCamp关于p值的假设检验问题的问题
- single-sign-on - Keycloak 跨远程站点的高可用性
- javascript - 如何根据循环的索引动态设置key