My site's articles use a page view counter that makes it possible to list top 5 most viewed articles. After enabling cache for all pages it no longer counts views. Is there any way to fix this?
Page view counter and cache
Started by mangopo, Aug 09 2012 06:43 PM
4 replies to this topic
#4
Posted 10 August 2012 - 04:45 AM
Well, as suggested: I would put this code in a page with a non cached template:
And in your pages / other templates you place something like this (jQuery assumed):
"mycounter" would be your counter page with the counter template.
[totally untested]
<?php $id = $sanitizer->text($input->get->id); $whichPage = $pages->get($id); $whichPage->views = $whichPage->views+1; $whichPage->saveField($whichPage, 'views');
And in your pages / other templates you place something like this (jQuery assumed):
<script>
$.get("/mycounter/", {id: <?= $page->id ?> } );
</script>
"mycounter" would be your counter page with the counter template.
[totally untested]
#5
Posted 10 August 2012 - 07:13 AM
I like MadeMyDay's solution. A couple other possibilities are:
1. Use MarkupCache rather than template caching. That way you can cache just the parts of the output that you want to, and leave the things like your counter uncached.
2. Create an autoload module. Add a ready() function to it and have that function increment the counter of $this->page (using the same method you are already doing).
1. Use MarkupCache rather than template caching. That way you can cache just the parts of the output that you want to, and leave the things like your counter uncached.
2. Create an autoload module. Add a ready() function to it and have that function increment the counter of $this->page (using the same method you are already doing).
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












