0

I'm trying to implement an up/downvote system for our web application. Currently the user can 'post' a question on our site. When the user goes to the browse section of our site, they are shown a list of all the questions currently posed on the website.

When the user clicks the up or downvote link, the database should be updated via AJAX (i.e. I don't want to redirect the user, or have the page refresh).

How do I retrieve the question associated with each up/downvote link, and then send that data to the controller?

site/browse:

<?php
    /*
     * @var $model Question
     */

    //loop through all the given questions
    foreach($model as $q)
    {        
        //add the upvote and downvote (text for now)
        echo CHtml::ajaxLink('up', array('question/upvote'));
        echo ("&nbsp - &nbsp;");
        echo CHtml::ajaxLink('down', array('question/downvote'));
        echo '<br />';

        echo CHtml::link($q['name'], array('question/browse/'.$q['_id']));
        echo "<br />Description: " . $q["description"] . "<br />";
        echo "Owner: " . $q["user_id"] . "<br />";

        $timeStamp = new Timestamp($q['timestamp']);

        echo $timeStamp->getDifferenceString() . "<br /><br />";
     }
?>

question controller:

...
 public function upvote()
        {
            //should retrieve question_id and then update database accordingly
            //e.g. UPDATE Question SET value = value + 1 WHERE Question.id = id
        }
...

1 Answer 1

1

You can pass the id of the question getting the upvote in the URL of the ajax request, or as POST data in the ajax request. I would recommend using POST data through the jQuery AJAX call, but here I'll show how to do it with the code you have supplied.

<?php
    /*
     * @var $model Question
     */

    //loop through all the given questions
    foreach($model as $q)
    {        
        //add the upvote and downvote (text for now)
        echo CHtml::ajaxLink('up', array('question/upvote',array('id'=>$q['_id'])));
        echo ("&nbsp - &nbsp;");
        echo CHtml::ajaxLink('down', array('question/downvote',array('id'=>$q['_id'])));
        echo '<br />';

        echo CHtml::link($q['name'], array('question/browse/'.$q['_id']));
        echo "<br />Description: " . $q["description"] . "<br />";
        echo "Owner: " . $q["user_id"] . "<br />";

        $timeStamp = new Timestamp($q['timestamp']);

        echo $timeStamp->getDifferenceString() . "<br /><br />";
     }
?>

Once you have the ID, then in your question controller you would modify the upvote() method as shown.

question controller:

...
 public function upvote($id)
        {
            //you have the question id, now you can update database accordingly
            //e.g. UPDATE Question SET value = value + 1 WHERE Question.id = id
        }
...

This will get you started. You should look at using jQuery to generate the upvote/downvote requests and handle the responses - and I don't mean the jQuery automatically generated by the ajaxLink() method. You can also look at making the url look nicer with a route to handle upvote and downvote requests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.