PHP / MySQLi虽然在值的同时首先重复

时间:2013-01-29 08:00:18

标签: php mysqli while-loop

代码:

$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之类的值。 感谢。

3 个答案:

答案 0 :(得分:0)

在您的while循环之前,您需要重置$revs,否则您只需将更多内容添加到其中已有的值。

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

答案 1 :(得分:0)

$ revs = array();

之后

foreach(收入为$收入) {

答案 2 :(得分:0)

更改您的

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

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

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

OR

之前初始化数组
    $revs      = aray();
    while($rev = $queryi->fetch_array())
    {
        $revs[$rev_day][] = $rev;
    }