2016-11-08 59 views
-1

如何检查值是否不会插入到数据库中,但是如果不是零将插入到数据库中?检查值是否为零不会插入数据库,但如果不为零将插入到数据库中

这里是我的代码片段,以使事情更清晰的:

$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); 
    } 

我只需要保存价值,是不是在数据库为零或空

+1

'if($ x!= 0)....' – nogad

+0

您的代码没有使它更清晰,但更糟糕。你似乎插入到redis而不是mysql – e4c5

+0

this($ this-> redis_search_value_model-> insert($ dataRedis);)只是一个mysql函数不是redis –

回答

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); 
     } 
    }