title: Codeigniter - Trying to count articles view
body: <p>Im working with codeigniter and  have a controller and 3 model functions to read, insert and update a column of table.
  i want to get count of view from db and update it per each view!
  but after the update an extra row added to table! with zero value for content_id.
  please check once!</p>
  
  <p>this is my controller:</p>
  
  <pre><code>  public function daily_report($id="")
    {
  
      $counter=$this-&gt;home_model-&gt;counter($id);
      if($counter)
      {
          $view=$this-&gt;home_model-&gt;counter($id)-&gt;row()-&gt;view;
  
          $views=$view+1;
  
  
          $this-&gt;home_model-&gt;update_counter($views,$id);
      }
      else{
  
          $views=1;
  
          $this-&gt;home_model-&gt;set_counter($views,$id);
  
      }
    }
  </code></pre>
  
  <p>This is the model functions:</p>
  
  <pre><code>  public function counter($id)
  {
      $code=$id;
      $lang=$this-&gt;session-&gt;userdata('lang');
      $data = $this-&gt;db
          -&gt;select('view')
          -&gt;from('tbl_views')
          -&gt;where("content_id",$code)
          -&gt;where("language",$lang)
          -&gt;get();
  
      if ($data-&gt;num_rows() &gt; 0) {
  
          return $data;
      }else{
          return false;
      }
  }
  public function set_counter($views,$id)
  {
      $data = array(
  
     'content_id' =&gt; $id ,
     'view' =&gt; $views,
     'language'=&gt;$this-&gt;session-&gt;userdata('lang')
      );
  
     $this-&gt;db-&gt;insert('tbl_views', $data);
  }
  
  public function update_counter($views,$id)
  {
  
  $data = array(
  
              'view' =&gt; $views,
  
                  );
      $this-&gt;db-&gt;where('content_id', $id);
      $this-&gt;db-&gt;update('tbl_views', $data);  
  }
  </code></pre>