我有一个显示值的表格:
日期列显示今天的日期,列2,3,4显示每个杂志的订阅数量。每天数字都会更改,mysql查询会从数据库中提取数字并将其显示在表格中。
我想显示一行显示连续两天之间的差异,即上/下时对特定杂志的订阅量:Todays value - yesterdays value
。
我正在运行的查询位于foreach
循环中。我如何计算连续值?
我的代码是:
foreach($values as $v_date){
$date = $v_date->DATE;
$count = $wpdb->get_results($wpdb->prepare(
" SELECT DISTINCT `date`,
(select subscriber_count FROM wp_email_signup where LIST_NAME = 'B1' AND date = '$date') as 'B1',
(select subscriber_count FROM wp_email_signup where LIST_NAME = '2D' AND date = '$date' ) as '2D',
(select subscriber_count FROM wp_email_signup where LIST_NAME = 'F90' AND date = '$date' ) as 'F90'
FROM wp_email_signup WHERE date = '$date' "));
echo '<tr>';
echo '<td>' . $date . '</td>';
foreach($count as $counts){
$val1 = $counts->B1;
$val2 = $counts->2D;
$val3 = $counts->F90;
echo '<td>' . $val1 . '</td>';
echo '<td>' . $val2 . '</td>';
echo '<td>' . $val3 . '</td>';
echo '</tr>';
echo '<tr class="row_diff">';
echo '<td></td>';
echo '<td>' . DIFFERENCE BETWEEN 2 CONSECUTIVE VALUES . '</td>';
echo '<td>' . DIFFERENCE BETWEEN 2 CONSECUTIVE VALUES . '</td>';
echo '<td>' . DIFFERENCE BETWEEN 2 CONSECUTIVE VALUES. '</td>';
echo '</tr>';
}
}
答案 0 :(得分:0)
这样的事情可行。
// set some values to zero so that our subtraction does not produce errors
$prev1 = 0;
$prev2 = 0;
$prev3 = 0;
foreach($count as $counts)
{
$val1 = $counts->B1;
$val2 = $counts->2D;
$val3 = $counts->F90;
echo '<td>' . $val1 . '</td>';
echo '<td>' . $val2 . '</td>';
echo '<td>' . $val3 . '</td>';
echo '</tr>';
echo '<tr class="row_diff">';
echo '<td></td>';
echo '<td>' . ($val1 - $prev1) . '</td>';
echo '<td>' . ($val2 - $prev2) . '</td>';
echo '<td>' . ($val3 - $prev3) . '</td>';
echo '</tr>';
// Set the current loop of values to be subtracted for the next loop
$prev1 = $val1;
$prev2 = $val2;
$prev3 = $val3;
}