1

代码:

$query = $db->query("SELECT day FROM impressions WHERE pub_id = '$pub_id' AND pub_tag = '$pub_tag' AND month = '$month' AND valid = '1' GROUP BY day ASC");
$stat_rev = $query->num_rows;
if($stat_rev > 0) {
    while($revenue = $query->fetch_array())
    {
        $revenues[] = $revenue;
    }
    foreach($revenues as $revenue)
    {
        echo $rev_day = $revenue['day'].':';

        $queryi = $db->query("SELECT revenue FROM impressions WHERE pub_id = '$pub_id' AND pub_tag = '$pub_tag' AND month = '$month' AND day = '$rev_day' AND valid = '1'");
        while($rev = $queryi->fetch_array())
        {
            $revs[] = $rev;
        }
        foreach($revs as $rev)
        {
            //$total_rev = $total_rev + $rev['revenue'];
            echo $rev['revenue'].',';
        }
        $queryi->close();

        echo ' - ';

        //echo '[\''.$revenue['day'].'\', '.$total_rev.'],';
        $total_rev = 0;

    }
}

输出:

28:0.001,0.001,0.006, - 29:0.001,0.001,0.006,0.006,0.001, -

数据库:

28: 0.001
28: 0.001
28: 0.006
29: 0.006
29: 0.001

在第一次循环运行期间,当天是 28,循环显示 0.001、0.001、0.006。现在在第 29 天第二次循环运行时,循环显示 0.001、0.001、0.006、0.006、0.001。第二个循环显示我尝试过但无法修复的第一个循环值我想显示诸如 28 之类的值:0.001、0.001、0.006 和 29:0.006、0.001。谢谢。

4

2 回答 2

0

在您的 while 循环之前,您需要 reset $revs,否则您只是将更多内容附加到已经存在的值中。

$revs = array( );在 foreach 中添加类似的东西应该可以解决问题。

于 2013-01-29T08:02:56.913 回答
0

改变你的

    while($rev = $queryi->fetch_array())
    {
        $revs[] = $rev;
    }

    while($rev = $queryi->fetch_array())
    {
        $revs[$rev_day][] = $rev;
    }

这将为您提供一个结构化数组,您可以稍后处理。

或者

在while之前初始化数组

    $revs      = aray();
    while($rev = $queryi->fetch_array())
    {
        $revs[$rev_day][] = $rev;
    }
于 2013-01-29T08:41:48.910 回答