Forum for discussing general topics related to Couch.
6 posts Page 1 of 1
Hi,
Is it possible to count the number of times an entry with the same k_page_title appears?
My site allows users to submit recommendations (e.g. hotels, eateries etc) via a databound form on the front end and sometimes the same recommendation is submitted by different users.
As it is right now, such submissions are automatically changed the k_page_name, adding a number at the end, which is fine. But I'd like to have the multiple recommendations published although showing the number of times it was recommended.

Any ideas are greatly welcome.
Hey, it is very easy.
I have a template with pages titled as: 1, 2, 3, 4, 5, 5.

Please study following sample. It will show how to count pages with same title. It will be helpful on each 'page-view'.
Note, how we enumerate *all* pages of the template and therefore pages with same title re-appear.

2017-10-31-001.png
2017-10-31-001.png (5.5 KiB) Viewed 1511 times


Code for it:

Code: Select all
<h2>Pages:</h2>
<ul>

    <cms:pages show_future_entries='0' skip_custom_fields='0' show_unpublished='0'  limit='1000000'>
        <cms:set this_page_title = k_page_title scope='global' />
        <cms:set count_of_equals = '0' scope='global' />
        <cms:set count_of_equals = "<cms:pages masterpage=k_template_name page_title=this_page_title count_only='1' />" scope='global' />
        <li>
            <a href="<cms:show k_page_link />">
                <cms:show k_page_title /></a> (<a href="<cms:admin_link />">admin link</a>). <i>Pages with same title: </i><cms:show count_of_equals />
        </li>
    </cms:pages>
</ul>


Now, if at some point you wish to display a list of pages with unique titles and a count of titles, then study the following sample.

2017-10-31-002.png
2017-10-31-002.png (2.65 KiB) Viewed 1511 times


Code: Select all
<cms:capture into='sql' >

    SELECT p.page_title AS `k_page_title`, p.page_name AS `k_page_name`, COUNT(*) AS `count_of_equals`
    FROM couch_pages p
    WHERE p.template_id='<cms:show k_template_id />' AND p.publish_date < '<cms:date format='Y-m-d H:i:s' />' AND NOT p.publish_date = '0000-00-00 00:00:00' AND p.parent_id=0
    GROUP BY p.page_title

</cms:capture>
<ul>
    <cms:query sql=sql limit='1000000'>
        <li>
            <a href="<cms:link masterpage=k_template_name page=k_page_name />">
                <cms:show k_page_title /></a>. <i>Pages with same title: </i><cms:show count_of_equals />
        </li>
    </cms:query>
</ul>


@KK: I don't remember if aggregate_by could be used here for k_page_title, so if this is possible - kindly add to the topic to remind us the exact syntax.
@trendoman, the 'aggregate_by' parameter works only for 'relation' custom fields.
I think your second example above (cms:query) is just right for OP's use-case.
willanga wrote: Is it possible to count the number of times an entry with the same k_page_title appears?

The code for the count that can be placed on any page-view (where k_is_page is active):
Code: Select all
<cms:pages masterpage=k_template_name page_title=k_page_title count_only='1' />
Thanks trendoman and KK. I'll try it out and let you know how it goes.
Hi,
It has worked perfectly... thanks!
One more thing, is it possible to show only one of the duplicates with the number of times it has been recommended next to it e.g. Taxify (3) instead of showing the title three times?
6 posts Page 1 of 1