3

I am simply trying to increment the amount of page views per visit with CodeIgniter's active record. For some reason, the following code is incrementing twice. So it adds 2 page views per visit. What is strange is that this is used on another website that shares the same table and the same method code and it works properly on the other website. The views field is a simple int(11). I am only calling this method once in the controller, I thought maybe I had a duplicate but I do not.

function increment_video_view($video_pk) {
    $this->db->where('video_pk', $video_pk);
    $this->db->set('views', 'views+1', FALSE);
    $this->db->update('videos');
}

Any ideas or help would be great! Thanks!

Chris
  • 764
  • 1
  • 10
  • 24
  • My code was fine, I had an out of date Model on the live server. The newest version of the Model locally was correct which is the code you see above. Thanks for the help below! – Chris Apr 11 '12 at 14:08
  • Try enabling the profiler and see what is being executed. – kba Apr 11 '12 at 14:09
  • I spoke to soon, this is very strange. I just noticed that the page views update correctly in FireFox, but they update twice in Chrome. How does that even make sense? I must be doing something weird here... – Chris Apr 11 '12 at 14:15
  • I ran the profiler and the page is running only one update query, and it is for the increment. There are no duplicates. Could this be a corrupt database problem? Nevermind, corrupt DB would not make sense either since the other browsers are fine. I am completely confused here. – Chris Apr 11 '12 at 14:26
  • 1
    Looks like this may be an issue with Chrome itself: http://code.google.com/p/chromium/issues/detail?id=64810 I'd still love any ideas or solutions to solve this if anyone has any? Thanks! – Chris Apr 11 '12 at 14:42
  • http://stackoverflow.com/questions/2009092/page-loads-twice-in-google-chrome maybe this thread also helps? It's ASP but still relevant I think. – qwertzman Apr 11 '12 at 22:11

3 Answers3

2

Try putting an echo (or log) statement inside the function to see if it actually gets called twice. Let us know if it only echo's once.

function increment_video_view($video_pk) {
    echo "We in increment_video_view";
    $this->db->where('video_pk', $video_pk);
    $this->db->set('views', 'views+1', FALSE);
    $this->db->update('videos');
}
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • It only showed once, but I think I found the problem. I must have had an old bug in the code from weeks ago and never updated the new model file that I had in my project. The views are incrementing one at a time now. Stupid mistake, I thought the model was up to date. Thank you! – Chris Apr 11 '12 at 14:07
  • I will mark you as the correct answer, since it did indirectly solve my problem and the echo was a smart and simple idea I should have tried before. Thank you! – Chris Apr 11 '12 at 14:09
  • FireFox updates page views once, Safari updates page views once, Chrome updates page views twice. Your echo only shows once on all three browsers. Not sure what is up with Chrome... – Chris Apr 11 '12 at 14:21
0

Are you sure that the controller is executed only once?

Chalda Pnuzig
  • 406
  • 4
  • 16
  • I did a project wide search and the increment_video_view() method is only called once ever. If you are asking if the controller method itself is called twice, I searched and it is also only called once. The controller method that calls the video page and the increment method is called videos(), it is only called once. Thank you... – Chris Apr 11 '12 at 13:52
0

This happens when you have a 404 link (image/css/favicon) in your HTML, or if you have a missing favicon.ico

Chrome looks for a favicon.ico even if you don't insert it in your HTML, and when it finds it missing, I think it calls another request.

WebNovice
  • 2,230
  • 3
  • 24
  • 40