运行时间在PHP动态绑定变量

Run time dynamically bind variable in PHP

我需要在 运行 时绑定变量的值。在这里,我描述了我所需要的标准。

我有一个 table : test_record

ID    user_id    details    record_time
1        1         xyz      2017-08-05 09:00:30
2        2         abc      2017-08-05 09:05:32
3        1         pqr      2017-08-05 09:06:25
4        1         lmn      2017-08-05 09:08:56
5        2         def      2017-08-05 09:08:59 
6        1         xyz      2017-08-06 09:00:30
7        2         abc      2017-08-06 09:05:32
8        2         pqr      2017-08-06 09:06:25
9        2         lmn      2017-08-06 09:08:56
10       2         def      2017-08-06 09:08:59

现在我想按日期然后按用户获取总打字组。

所以我使用这个查询:

SELECT count(ID) as total_rec ,user_id,DATE(record_time) as date_rec 
FROM test_record GROUP BY DATE(record_time),user_id 
ORDER BY DATE(record_time),user_id

我得到的结果是:

total_rec    user_id    date_rec
 3              1       2017-08-05
 2              2       2017-08-05
 1              1       2017-08-06
 2              4       2017-08-06

在 php 中,我将它放在数组中,然后循环遍历它以在表格中显示它:

<table>
<tr>
<th>Total test records</th>
<th>User ID</th>
<th>Date</th>
</tr>
<?php
$rec_date = ""
foreach($records as $key=>$value )
{
    //I have done this for grouping so that each date can highlighted with new row.
    if($rec_date != $value['date_rec'])
    {
        $rec_date = $value['date_rec'];
        echo "<tr><td colspan='3'>$rec_date</td></tr>";
    }
    echo "<tr>";
    echo "<td>".$value['total_rec']."</td>";
    echo "<td>".$value['user_id']."</td>";
    echo "<td>".$value['date_rec']."</td>";
    echo "</tr>";
}
?>
</table>

一切正常。现在我想要的是我需要 total_rec 日期的总和,即所有用户的总数。我想显示我在 if 条件下分开的日期。 我知道我可以像

这样循环获取它
$total_dt = 0;
foreach($records as $key=>$value )
{
    if($rec_date != $value['date_rec'])
    {
        $rec_date = $value['date_rec'];
        echo "<tr><td colspan='3'>$rec_date</td></tr>"; // I want to display total here with date
    }
    $total_dt + = $value['total_rec'];
    .
    .
    .
}

但我想要 在 if 条件 if($rec_date != $value['date_rec']) 中,即 在显示特定日期的所有数据之前 。我知道我终于可以得到它了,但我想在

之前显示

您可以从 SQL

SELECT * FROM 
    (
    SELECT count(ID) as total_rec ,user_id,DATE(record_time) as date_rec,'user_date' as mode 
    FROM test_record GROUP BY DATE(record_time),user_id 


    UNION 


    SELECT count(ID) as total_rec ,0 as user_id,DATE(record_time) as date_rec,'date_only' as mode 
    FROM test_record GROUP BY DATE(record_time)
    )tmp
    ORDER BY date_rec ,user_id DESC

基于mode你可以做一些你认为的事情

我还有 PHP 5.3.2,所以我没有 array_column 可用,但确实找到了一个可以在 PHP site 上模拟它的工作函数 - 所以这样的测试似乎产生所需的输出。

假设记录集是这样的

$records=array(
    array('total_rec'=>3,'user_id'=>1,'date_rec'=>'2017-08-05'),
    array('total_rec'=>2,'user_id'=>2,'date_rec'=>'2017-08-05'),
    array('total_rec'=>1,'user_id'=>1,'date_rec'=>'2017-08-06'),
    array('total_rec'=>2,'user_id'=>4,'date_rec'=>'2017-08-06')
);

然后

echo array_sum( array_column( $records, 'total_rec' ) ); //8

1st : 您可以循环数组并像这样按日期制作总数组

$new_array=array();
foreach($array as $key=>$val){

    $new_array[$val['date_rec']]=isset($new_array[$val['date_rec']])? $new_array[$val['date_rec']]+$val['total_rec'] : $val['total_rec'];
}

2nd : 像这样从数组中回显特定日期的总数。

echo "<tr><td colspan='3'>$rec_date</td><td>".$new_array[$value['date_rec']]."</td></tr>";

更新 1 : 您可以使用 array_walk()

$new_array=array();
array_walk($ss,function($v) use(&$new_array){  $new_array[$v['date_rec']] = isset($new_array[$v['date_rec']])? $new_array[$v['date_rec']]+$v['total_rec'] : $v['total_rec'];  });

print_r($new_array);