php数组在使用while时使用foreach显示错误的结果

时间:2018-05-25 03:45:50

标签: php mysqli

我运行以下代码:

$getytclicks = mysqli_query($mysqli, "SELECT * FROM links WHERE type='youtube'");
$clicks=array();
    while($r = mysqli_fetch_array($getytclicks))
    {
        if($r['month'] == $month && $r['year'] == date("Y"))
        {
            foreach($days as $day)
            {
                if($r['day'] . " " . $month == $day && $r['year'] == date("Y"))
                {
                    $clicks[$day] .= mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM links WHERE day='".$r['day']."' AND month='".$r['month']."' AND year='".$r['year']."'"));
                } else {
                    $clicks[$day] .= 0;
                }
            }
        }
    }
var_dump($clicks);

这就是var_dump显示的内容:

array (size=31)
  '01 Май' => string '0000000000' (length=10)
  '02 Май' => string '0000000000' (length=10)
  '03 Май' => string '0000000000' (length=10)
  '04 Май' => string '0000000000' (length=10)
  '05 Май' => string '0000000000' (length=10)
  '06 Май' => string '0000000000' (length=10)
  '07 Май' => string '0000000000' (length=10)
  '08 Май' => string '0000000000' (length=10)
  '09 Май' => string '0000000000' (length=10)
  '10 Май' => string '0000000000' (length=10)
  '11 Май' => string '0000000000' (length=10)
  '12 Май' => string '0000000000' (length=10)
  '13 Май' => string '0000000000' (length=10)
  '14 Май' => string '0000000000' (length=10)
  '15 Май' => string '0000000000' (length=10)
  '16 Май' => string '0000000000' (length=10)
  '17 Май' => string '0000000000' (length=10)
  '18 Май' => string '0000000000' (length=10)
  '19 Май' => string '0000000000' (length=10)
  '20 Май' => string '1000000000' (length=10)
  '21 Май' => string '0000000000' (length=10)
  '22 Май' => string '0333000000' (length=10)
  '23 Май' => string '0000100000' (length=10)
  '24 Май' => string '0000044440' (length=10)
  '25 Май' => string '0000000001' (length=10)
  '26 Май' => string '0000000000' (length=10)
  '27 Май' => string '0000000000' (length=10)
  '28 Май' => string '0000000000' (length=10)
  '29 Май' => string '0000000000' (length=10)
  '30 Май' => string '0000000000' (length=10)
  '31 Май' => string '0000000000' (length=10)

为什么它会显示太多零?

它只需要为零。此外,如果值为1341,我只需要显示它们,而不是零。

我尝试从$clicks['day'] .=更改为$clicks['day'] =,但之后只显示最后一个结果,125th May

2 个答案:

答案 0 :(得分:0)

您将{0}追加为$clicks[$day] .= 0;。我认为你试试$ click [$ day] = 0;每次只是分配它不附加。

$getytclicks = mysqli_query($mysqli, "SELECT * FROM links WHERE type='youtube'");
$clicks=array();
    while($r = mysqli_fetch_array($getytclicks))
    {
        if($r['month'] == $month && $r['year'] == date("Y"))
        {
            foreach($days as $day)
            {
                if($r['day'] . " " . $month == $day && $r['year'] == date("Y"))
                {
                    $clicks[$day] = mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM links WHERE day='".$r['day']."' AND month='".$r['month']."' AND year='".$r['year']."'"));
                } else {
                    $clicks[$day] = 0;
                }
            }
        }
    }
var_dump($clicks);

答案 1 :(得分:0)

您正在连接数据,从而导致这些超额值。

改变这个:

select a.*
from demo a
left join demo b on a.id_konsul = b.id_konsul
                and a.staflow = b.staflow 
                and a.created_at < b.created_at
where a.id_konsul = 4 
and b.id_konsul is null

对此:

if($r['day'] . " " . $month == $day && $r['year'] == date("Y"))
{
     $clicks[$day] .= mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM links WHERE day='".$r['day']."' AND month='".$r['month']."' AND year='".$r['year']."'"));
} else {
    $clicks[$day] .= 0;
}