Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
18 posts Page 1 of 2
Hi guys!

More then 1 year ago I was facing this problem, now one customer is asking again to display on his blog's sidebar the most viewed/popular articles. At that time, it wasn't possible to do this with Couch. How about now? Any chance to be able to implement it?
Thanks.
atisz wrote: Hi guys!

More then 1 year ago I was facing this problem, now one customer is asking again to display on his blog's sidebar the most viewed/popular articles. At that time, it wasn't possible to do this with Couch. How about now? Any chance to be able to implement it?
Thanks.


You can read about it here ;)
As soon as possible!

Touch me up : abada[dot]zulma[at]gmail[dot]com
@atisz, please give me a little time.
I am busy in something right now but as soon as I get a break I'll try to create an addon for this.
GoingMarryAsap wrote: You can read about it here ;)


Yes, that's my original post :)
KK wrote: @atisz, please give me a little time.
I am busy in something right now but as soon as I get a break I'll try to create an addon for this.


Thank you KK! That would definitely save my day ;)
As you said after my first post on this specific requirement, Couch should have this feature.
@atisz

I think for a simple 'page-hit counter', like the one you want, we don't need a dedicated addon. it can be done using plain Couch code.

Here is how I made one (I'll assume we are building it for a template named 'blog.php') -
1. Define the following editable region in the template
<cms:template title='Blog' clonable='1'>
..
..
<cms:editable label='Page hits' name='page_hits' type='text' search_type='integer' />
</cms:template>

2. Place the following code in the template's page-view
Code: Select all
<cms:if k_is_page>

    <cms:no_cache />

    <cms:php>
        // identify bots
        global $CTX;
        if( isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) ){
            $CTX->set( 'is_bot', '1', 'global' );
        }       
    </cms:php>
   
    <cms:if "<cms:not is_bot />">
        <cms:db_persist
            _masterpage=k_template_name
            _page_id=k_page_id
            _mode='edit'
           
            page_hits="<cms:add page_hits '1' />"
        />
    </cms:if>
</cms:if>

The code above simply increments the hit counter by 1 every time the page is accessed (trying not to do so for bots).
Please notice that we are using cms:no_cache to prevent the page-views from being cached as, obviously, the code will not work.

And that is all that is needed.

On the frontend, we can use the 'page_hits' editable region with cms:pages tag e.g. the following will display the top 5 most viewed blog posts-
Code: Select all
<cms:pages masterpage='blog.php' orderby='page_hits' limit='5'>
   ...
</cms:pages>

Does this help?
KK wrote: @atisz
Does this help?


Of course! It works like a charm :) Thank you KK!
You are welcome :)
Thanks for sharing, I needed this one badly :)
I've tried doing this but I get ERROR! Unknown tag: "db_persist" ?
18 posts Page 1 of 2

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 3 guests