CodeIgniter Forums

Full Version: [Solved] MY Custom Fourm Vote: Stop forum votes going below 0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On my forum I am learning to make I have made a reputation vote model.

I would like to know. When forum_user_reputation get's to 0 from been voted down $this->input->post('vote_down') to it then it should stop there.

Currently if the question gets down voted still the forum_user_reputation will go into negative -2 but I need to make it stop at 0

How could I make suitable changes to my function update_forum_question()

PHP Code:
public function update_forum_question() {
        
$current_votes $this->get_question_reputation();

        if (
$this->input->post('vote_up') == TRUE) {

            
$sum_total $current_votes $this->input->post('vote_up');
            
            
$data = array(
                
'forum_question_reputation' => $sum_total    
            
);

            
$this->db->where('forum_question_id''1');
            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_questions'$data);

            
$forum_user_reputation $this->get_forum_user_reputation();

            
$forum_reputation $forum_user_reputation  5;

            
$data = array(
                
'forum_reputation' => $forum_reputation    
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_user'$data);

        } elseif (
$this->input->post('vote_down') == TRUE) {

            
$sum_total $current_votes $this->input->post('vote_down');

            
$data = array(
                
'forum_question_reputation' => $sum_total
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->where('forum_question_id''1');
            
$this->db->update('forum_questions'$data);

            
$form_votes $current_votes 2;

            
$data = array(
                
'forum_reputation' => (int)$form_votes    
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_user'$data);

        }
    } 

Thanks for you advice
(12-27-2015, 03:25 AM)wolfgang1983 Wrote: [ -> ]On my forum I am learning to make I have made a reputation vote model.

I would like to know. When forum_user_reputation get's to 0 from been voted down $this->input->post('vote_down') to it then it should stop there.

Currently if the question gets down voted still the forum_user_reputation will go into negative -2 but I need to make it stop at 0

How could I make suitable changes to my function update_forum_question()

PHP Code:
public function update_forum_question() {
        
$current_votes $this->get_question_reputation();

        if (
$this->input->post('vote_up') == TRUE) {

            
$sum_total $current_votes $this->input->post('vote_up');
            
            
$data = array(
                
'forum_question_reputation' => $sum_total    
            
);

            
$this->db->where('forum_question_id''1');
            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_questions'$data);

            
$forum_user_reputation $this->get_forum_user_reputation();

            
$forum_reputation $forum_user_reputation  5;

            
$data = array(
                
'forum_reputation' => $forum_reputation    
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_user'$data);

        } elseif (
$this->input->post('vote_down') == TRUE) {

            
$sum_total $current_votes $this->input->post('vote_down');

            
$data = array(
                
'forum_question_reputation' => $sum_total
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->where('forum_question_id''1');
            
$this->db->update('forum_questions'$data);

            
$form_votes $current_votes 2;

            
$data = array(
                
'forum_reputation' => (int)$form_votes    
            
);

            
$this->db->where('forum_user_id''1');
            
$this->db->update('forum_user'$data);

        }
    } 

Thanks for you advice

Try this:

PHP Code:
public function update_forum_question()
{
    
$current_votes $this->get_question_reputation();

    if (
$this->input->post('vote_up') == TRUE)
    {
        
$sum_total $current_votes $this->input->post('vote_up');

        
$data = array(
            
'forum_question_reputation' => $sum_total
        
);

        
$this->db->where('forum_question_id''1');
        
$this->db->where('forum_user_id''1');
        
$this->db->update('forum_questions'$data);

        
$forum_user_reputation $this->get_forum_user_reputation();

        
$forum_reputation $forum_user_reputation  5;

        
$data = array(
            
'forum_reputation' => $forum_reputation
        
);

        
$this->db->where('forum_user_id''1');
        
$this->db->update('forum_user'$data);

    }
    elseif (
$this->input->post('vote_down') == TRUE)
    {

        
$sum_total $current_votes $this->input->post('vote_down');

        
// check to see if vote_down is less than 0 or equals 0
        
if ($sum_total <= 0)
        {
            
$sum_total 0;
        }

        
$data = array(
            
'forum_question_reputation' => $sum_total
        
);

        
$this->db->where('forum_user_id''1');
        
$this->db->where('forum_question_id''1');
        
$this->db->update('forum_questions'$data);

        
$form_votes $current_votes 2;

        
$data = array(
            
'forum_reputation' => (int)$form_votes
        
);

        
$this->db->where('forum_user_id''1');
        
$this->db->update('forum_user'$data);

    }


Not tested.
Thank you for that advice

Where it updates the user votes I added it just above it and works fine

PHP Code:
public function update_forum_question() {

$current_votes $this->get_question_reputation();


if (
$this->input->post('vote_up') == TRUE) {

$sum_total $current_votes $this->input->post('vote_up');

$data = array(
 
  'forum_question_reputation' => $sum_total
);

$this->db->where('forum_question_id''1');
$this->db->where('forum_user_id''1');
$this->db->update('forum_questions'$data);

$forum_user_reputation $this->get_forum_user_reputation();

$forum_reputation $forum_user_reputation  5;

$data = array(
 
  'forum_reputation' => $forum_reputation
);

$this->db->where('forum_user_id''1');
$this->db->update('forum_user'$data);

}

elseif (
$this->input->post('vote_down') == TRUE)

{

$sum_total $current_votes $this->input->post('vote_down');

$data = array(
 
  'forum_question_reputation' => $sum_total
);

$this->db->where('forum_user_id''1');
$this->db->where('forum_question_id''1');
$this->db->update('forum_questions'$data);

$forum_votes $current_votes 2;

// check to see if vote_down is less than 0 or equals 0
if ($forum_votes <= 0) {
 
  $forum_votes 0;
}

$data = array(
 
 'forum_reputation' => (int)$forum_votes
);

$this->db->where('forum_user_id''1');
$this->db->update('forum_user'$data);

}