Welcome Guest, Not a member yet? Register   Sign In
Database Update + 1
#1

[eluser]shenanigans01[/eluser]
Hello, I'm trying to build a voting function. Is there an easy way to have an update function that simply gets the number from the datbase field and adds 1 to it...

Code:
function vote(){
            $ID =  $this->uri->segment(3);
            $this->beer_model->vote('beers','id',$ID);            
            redirect(base_url().'index.php/beers/manage/');
    }

is what I have in the controller, and in the model I have the following:


Code:
function vote($table,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        
         ????????????????????????????????????

        if ($this->db->affected_rows() == '1')
        {
            return TRUE;
        }
        
        return FALSE;        
    }


the '????' is where I'm not quite sure what to-do. Any help would be greatly appreicated!
#2

[eluser]SPeed_FANat1c[/eluser]
You must have in db column "number_votes" and "total_points". In vote function you can do then something like

Code:
$this->db->query('UPDATE objects SET number_votes = number_votes + 1,
            total_points = total_points + '.$vote.' where id = '.$obj_id);

When you want to get rating, you make a calculation total_points/number_votes.
#3

[eluser]stuffradio[/eluser]
Code:
$this->db->set('field','field+1');
$this->db->where('field','val');
$this->db->update('table');




Theme © iAndrew 2016 - Forum software by © MyBB