如何检查值是否为零不会插入数据库但如果不为零则会插入数据库?
这是我的代码片段,使事情更清晰:
$SearchData = $this->redis->hmget($searchCohortKey, $DateRange);
$NaturalClickData = $this->redis->hmget($naturalClickCohortKey, $DateRange);
$AdClickData = $this->redis->hmget($adClickCohortKey, $DateRange);
$uninstallData = $this->redis->hmget($uninstallKey, $DateRange);
$count = count($DateRange);
for ($i = 0; $i < $count; $i++) {
$dataRedis = array(
'app_id' => $appId,
'searches' => empty($SearchData[$i]) ? 0 : $SearchData[$i],
'clicks' => empty($NaturalClickData[$i]) ? 0 : $NaturalClickData[$i],
'adclicks' => empty($AdClickData[$i]) ? 0 :$AdClickData[$i],
'uninstalls' => empty($uninstallData[$i]) ? 0 : $uninstallData[$i],
'date' => $DateRange[$i],
'key' => $searchCohortKey
);
$this->redis_search_value_model->insert($dataRedis);
}
我只需要在数据库中保存非零或空值
答案 0 :(得分:0)
您的问题有点不清楚,但我认为您希望实现的目标,您可以根据需要修改条件。
$SearchData = $this->redis->hmget($searchCohortKey, $DateRange);
$NaturalClickData = $this->redis->hmget($naturalClickCohortKey, $DateRange);
$AdClickData = $this->redis->hmget($adClickCohortKey, $DateRange);
$uninstallData = $this->redis->hmget($uninstallKey, $DateRange);
$count = count($DateRange);
for ($i = 0; $i < $count; $i++) {
$dataRedis = array(
'app_id' => $appId,
'searches' => empty($SearchData[$i]) ? 0 : $SearchData[$i],
'clicks' => empty($NaturalClickData[$i]) ? 0 : $NaturalClickData[$i],
'adclicks' => empty($AdClickData[$i]) ? 0 :$AdClickData[$i],
'uninstalls' => empty($uninstallData[$i]) ? 0 : $uninstallData[$i],
'date' => $DateRange[$i],
'key' => $searchCohortKey
);
/*Here's the code to check if the values are not zero,
it won't insert the data if even of the 'searches' Or 'clicks' or adclicks
have a value of 0*/
if($dataRedis['searches'] != 0 && $dataRedis['clicks'] != 0 && $dataRedis['adclicks'] != 0)
{
$this->redis_search_value_model->insert($dataRedis);
}
}