2017-07-14 4 views
1

私は通知があるかどうかをチェックしたいが、私がそれらをチェックしようとすると偽を返そうとするが、私がページをリフレッシュするとjsonの通知が表示される。しかし、私は私のIDの通知でdinamically私の配列を送信するが、私はそれを行うことはできますか?使用後のないまたはあなたがする必要がどのような私のID通知を受けても私の問題を解決する方法はありますか?

コントローラ通知

public function notification(){ 
      $id = array(1,2,3,4); 
      $output = array_rand($id, 1); 
      $output_data = array('id' => $id[$output]); 
      $notifications = $this->notification->getNotifications($output_data); 
      if ($notifications) { 
       $data['product_id'] = $notifications->product_id; 
       $data['user_id'] = $notifications->user_id; 
       $data['message'] = $notifications->message; 
       $data['timestamp'] = $notifications->timestamp; 
       $data['update_count_notification'] = $this->notification->getCountNotification(); 
       $data['success'] = true; 
      }else{ 
       $data['success'] = false; 
      } 

      $this->json($data); 
     } 

モデル通知

public function getNotifications($data){ 
     $this->db->select('*'); 
     $this->db->from('storelte_notifications'); 
     $this->db->where('id',$data['id']); 
     $query = $this->db->get(); 
     return $query->row(); 
    } 

答えて

0

はいくつかに追加することによって、あなたのコードが何をしているか確認する方法を学ぶある渡してもらいますデバッグ。

あなたのコントローラー:

public function notification() { 
    $id = array(1, 2, 3, 4); 
    $output = array_rand($id, 1); 
    $output_data = array('id' => $id[$output]); 
    $notifications = $this->notification->getNotifications($output_data); 

    // These two lines added for DEBUG ONLY 
    var_dump($output_data); 
    var_dump($notifications); 

    if ($notifications) { 
     $data['product_id'] = $notifications->product_id; 
     $data['user_id'] = $notifications->user_id; 
     $data['message'] = $notifications->message; 
     $data['timestamp'] = $notifications->timestamp; 

     $data['update_count_notification'] = $this->notification->getCountNotification(); 

     $data['success'] = TRUE; 

    } else { 
     $data['success'] = FALSE; 
    } 

    $this->json($data); 
} 

だから、ラインif ($notifications)は、TRUEまたはFALSEに評価することがあります。それは?

そして、あなたのモデルは、まさにこれを行うことができますあなたに起こっている参照し

...

public function getNotifications($data){ 
    $this->db->select('*'); 
    $this->db->from('storelte_notifications'); 
    $this->db->where('id',$data['id']); 
    $query = $this->db->get(); 

    // This line added for DEBUG 
    echo $this->db->last_query(); 

    return $query->row(); 
} 

ここでの考え方は、実際にあなたのコードが、それはあなたが意図したとおりに動作している場合だ確実にするためにやっていることを検査することですなぜそれを表示するべきではありません。

これを試してみてください。

関連する問題