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 ("  - ");
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
}
...