0

I have an Article controller & model. I want to have a function that adds one to an integer when a link is clicked. So I have say a "i like" button (link) it takes me to Articles/thumbsup it will then take the thumbsup field of that article and increment it by one, then save the article, and redirect to the view.

I'm not sure how to modify records in this way. I need to load the article, modify the data and then save it.

My hope is that this be handled by javascript so noone will see the url change.

Can someone help me out here?

Thanks,

Jonesy

Thanks,

Billy

1 Answer 1

1

You can do that with an AjaxRequest.

models/article.php:

function thumbsup($id = null)
  {
  $conditions = array('Article.id' => $id);
  $this->updateAll(
    array('Article.thumbsup' => 'Article.thumbsup + 1'),
    $conditions
    );
  $article = $this->find(
    'first',
    array('conditions' => $conditions, 'fields' => array('Article.thumbsup'))
    );
  return $article['Article']['thumbsup'];
  }

controllers/articles_controller.php:

function thumbsup($id = null)
  {
  $this->set('count', $this->Article->thumbsup($id));
  $this->layout = 'ajax';
  }

views/articles/thumbsup.ctp:

<?php echo $count; ?>

webroot/js/thumbsup.js:

$(document).ready(function()
  {
  $('.iLikeSomething').click(function()
    {
    $.post('http://yoursite.com/articles/incrementThumbsup/'+this.id, function(data)
      {
      $('#iLikeSomethingCount').html(data);
      });
    });
  });

Html sample for clicking:

<span class="iLikeSomething" id="insertYourArticleIdHere" style="cursor: pointer;">iLike</span>
<div id="iLikeSomethingCount">0</div>
Sign up to request clarification or add additional context in comments.

9 Comments

This will create a race condition and lead to inaccurate counts. Use DB to increment.
Well, that would be a better way indeed. I guess the only way to do that via CakePHP is to use the custom query function, anyone wanna correct me?
As it turns out - you can add conditions to the UpdateAll() function, I didn't know that. Edited the answer with proper code. Also there is something like bakery.cakephp.org/articles/ketan/2008/05/13/increment-behavior, might want to check it out. Or at least create a function in model, to keep provided code away from the controller.
thanks for replying! I'm getting a bit confused though, I'm not sure what $count is used for, I'm just getting a undefined variable error.This will also be the first time I'm doing this sort of jQuery call. I have added the jQuery library to js/ and called it in the page. I have added a link with the id 'iLikeSomething'. I've added the javascript to my view but nothing is happening when I like the link
As I said before - I wrote it in a hurry, forgot about few things. I edited the answer, actually checking the use on my app, it works now.
|

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.