2016-11-08 75 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); 
     } 
    }