Welcome Guest, Not a member yet? Register   Sign In
how create of update field ??
#1

[eluser]Asinox[/eluser]
Hi, i want to do this:

When the user go to some links for read the article, i want to update the field 'views' in my table 'tbl_views'...

my function for read articles
Code:
function leer_articulo($id){
               $this->db->select('*');
            $this->db->where('article_id',$id);
            $this->db->where('articles.pub =','1');            
            $this->db->from('articles');
            /*$this->db->join('comments','comments.article_id = articles.id');*/
            $this->db->join('categories', 'categories.categories_id = articles.category_id');            
            $query = $this->db->get();
            return $query->row_array();
            
            $this->db->select('*');
            $this->db->where('article_id',$id);
            $this->db->from('lecturas');
            $query2 = $this->db->get();
            return $query2->row_array();
            
            if($query2->num_rows() == 0){
                $data = array('article_id'=>$id,'lecturas'=>1);
                $this->db->insert('lecturas',$data);
            }else{
                $suma = $row['lecturas'];
                $this->db->where('article_id',$id);
                $this->db->update('lecturas',$suma+1);
            }
            
       }
#2

[eluser]Michael Wales[/eluser]
I took the opportunity to clean your code up quite a bit... not even sure how what you had works to be honest - nothing should be executed after your first return...

Also, I'm not sure why you have a whole new table for views - why not just create a views field within the articles table and increase it by one? Here's how I would handle this:

Code:
function leer_articulo ($id){
  $this->db->join('comments', 'comments.categories_id = articles.category_id');
  $query = $this->db->get_where('articles', array('article_id' => $id, 'articles.pub' => '1'));
  if ($query->num_rows() > 0) {
    // We have a valid article - increase the views
    $this->db->update('articles', array('views' => 'views + 1'), array('article_id' => $id));
    return $query->result_array();
  }
  // No article by that ID
  return FALSE;
#3

[eluser]Asinox[/eluser]
Thanks Michael Smile im sorry but im new with CI

Smile

Thanks Smile
#4

[eluser]Asinox[/eluser]
Im sorry but this is not working
Code:
$this->db->update('views', array('views' => 'views+1'), array('article_id' => $id));

if the lecturas field hav 1 and i execute the code ...the field go back to 0

Sad

Thanks
#5

[eluser]Michael Wales[/eluser]
Bah - I always forget about the quoting boolean.

Add the following code to your constructor, this will show you the queries CI generates:
Code:
$this->output->enable_profiler(TRUE);

If you look, the query will be wrong. Change the code I gave you to:
Code:
$this->db->set('views', 'views + 1', FALSE);
$this->db->where(array('article_id' => $id));
$query = $this->db->update('views');

By using the set() method and passing the 3rd parameter (FALSE) we can turn off CI's auto-escaping.
#6

[eluser]Asinox[/eluser]
Super!,

Thanks Michale Smile




Theme © iAndrew 2016 - Forum software by © MyBB