Welcome Guest, Not a member yet? Register   Sign In
Oh simple error, how do i fix thee?
#1

[eluser]Tom Taila[/eluser]
Ok so ive run into a confusing problem which i cant seem to solve, im basically creating a "vote up" function whereby a user can click a vote up button and this will therefore add the score of the comment which they just voted up (if that makes sense), its kind of like the "like" button on facebook, for example if 50 people on facebook liked something it would say "50 people like this" where as in my case it would say "score = 50".

The problem im having is that when i click on vote up it is incremented by one but once i click on vote up all the scores of all the comments on my page become the incremented value of what i just clicked vote up for. anyway, i cant seem to figure out why i hope you guys can help, heres my code :

Code:
//part of my comment controller
    function vote_up() {
        $this->load->model('comment_model');
        $this->comment_model->vote_up();
        redirect('comment');
    }

and
Code:
//part of my comment_model model
function vote_up() { //MUST RETRIEVE THE CORRECT ROW AND THEN GET THE COMMENT SCORE, STORE IT AS A VARIABLE, THEN ADD ONE TO IT, THEN UPDATE THE DATABASE WITH ITS NEW SCORE
        $query = $this->db->where('id', $this->uri->segment(3));
        $comment_row = $this->db->get('comments');
        $result = $comment_row->row();
        $data = array(
            'score' => $result->score + 1
        );
        
        $this->db->update('comments', $data);
        
    }

and this is my view
Code:
<?php if(isset($all_comments)){
    foreach($all_comments as $row){
        echo "<h2>" . $row->user . "</h2>";
        echo "<p>" . $row->content . "</p>";
        echo "score=" . $row->score . "<br/>";
        echo "<a href=" . base_url() . ">id . ">vote up</a><br/>";
        echo "<a href=" . base_url() . ">id . ">Delete</a>";
        }
    }
    else {
        echo "<h3> No records </h3>";
        
    }
?&gt;

really hope you guys can help this is bugging me so much and i bet its gonna be a simple fix :!
#2

[eluser]LuckyFella73[/eluser]
It's not tested but try the following lines:
Code:
function vote_up()
{
    $id = $this->uri->segment(3);
    $this->db->where('id', $id);

    $comment_row = $this->db->get('comments');
    $result = $comment_row->row();
    $data = array(
        'score' => $result->score + 1
        );
    
    $this->db->where('id', $id);
    $this->db->update('comments', $data);
}

EDIT:
You could even shorten that a little bit:
Code:
function vote_up()
{
    $id = $this->uri->segment(3);
    
    $this->db->set('score', 'score+1',FALSE); // check if "FALSE" is needed here ..
    $this->db->where('id', $id);
    $this->db->update('comments'); // EDIT no 2 > replaced a variable with your table name
}
#3

[eluser]mattpointblank[/eluser]
LuckyFella73 is right - your code has two database calls, the $this->db->get() and $this->db->update(). You didn't give the second call a WHERE clause so it updated all of the rows.

Also, he's right about shortening it - no need to get the current score then increment it, MySQL can do that in one query. Cool eh?
#4

[eluser]Tom Taila[/eluser]
thank you both so much, i thought i had isolated that row by including the first 'where' statement at the begining of that section of code but i guess i thought wrong, the problems fixed. Cheers guys Smile




Theme © iAndrew 2016 - Forum software by © MyBB