dongyan3853 2019-06-20 12:39
浏览 109

PHP使用sqldb中的数据循环遍历变量

I am making a html comparison table of total credit and debit for each month for the last one year.I am getting data from sql database table and using below code to segregate the data for each month.

Using below code, I am able to get the data and display on my html page. But I have to write separate similar code for each month. This is only a sample table, I have multiple similar code for different credit, debit category. so each time I have to duplicate it just like $credit0,$credit1....$credit11

I am new to php and this is how I come one with my limited knowledge. Is there any better way I can do it rather than writing multiple similar code for each month ?

<?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

?>

UPDATE: Using loop, I tried below method,but its not working properly

<?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>';

}


?>
  • 写回答

1条回答 默认 最新

  • dousuochu7291 2019-06-20 14:03
    关注

    Well first this:

    $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);
    

    Becomes this:

    $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);
    }
    

    And I don't really get what you're exactly trying to do with the rest of your code :|

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大