Most Read/View Articles

10 posts by 3 authors in: Forums > CMS Builder
Last Post: February 13, 2012   (RSS)

Re: [aharrow] Most Read/View Articles

By Dave - December 31, 2008

Hi aharrow,

Yes, it's a two step process. First you need to track hits or views for each record. Then you can sort on that field.

Create a field called 'hits' (or whatever you want to call it) add this in your page. Then use this code. This first field is the tablename, the second field is the fieldname, and the third field is the record number

<?php incrementCounterField('news', 'hits', $newsRecord['num']); ?>

Once you have that working create a list viewer that sorts like this:

'orderBy' => 'hits+0 DESC',

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Most Read/View Articles

Can you tell me what kind of field should the "hits" field be. (checkbox, hidden......)

Also the code "[font "Verdana"]<?php incrementCounterField('news', 'hits', $newsRecord['num']); ?> " can it be placed anywhere in the body of article.[/#000000][/#ff0000]

[font "Verdana"]As it stands right now this is a sample of my code[/#000000] [/#ff0000]

[font "Verdana"]list($featuredRecords, $featuredMetaData) = getRecords(array(
'tableName' => 'featured',
'limit' => '2',
'orderBy' => 'date DESC, title',
'where' => 'archive = 0',
));
[/#ff0000]


[font "Verdana"]?>[/#ff0000]

[font "Verdana"]do i put the increment code on this (list) page or on the details page

Thanks

Re: [aharrow] Most Read/View Articles

By Dave - December 31, 2008

I'd make it a text field. And put the code on the detail page at the bottom and update the $newsRecord to be the same variable your detail viewer page is using.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Most Read/View Articles

By aharrow - January 5, 2009

Sorry for the late response Dave, got caught up in the holidays. Im giving it a shot now. Thanks for you help.

Re: [aharrow] Most Read/View Articles

By aharrow - January 5, 2009

Hi Dave,

I got the intial code working <?php incrementCounterField('press_news', 'hits', $press_newsRecord['num']); ?>[/#ff0000]

However, I have got multiple sections and really wanted to do an "includes page" with the most read articles (combination of different sections).

How would I be able to combine my results to show on one page. example Top 5 Most read articles <?php incrementCounterField('press_news', 'hits', $press_newsRecord['num']); ?> and [/#000000][/#ff0000]<?php incrementCounterField('featured', 'hits', $featuredRecord['num']); ?> [/#ff0000]

Thanks in advance.

Re: [aharrow] Most Read/View Articles

By Dave - January 6, 2009

Hi aharrow,

There's a few ways to do it. The simplest would be if you had all your articles in the same table and a field that let you specify where they should be displayed (eg: press_news, featured, etc).

Otherwise, you could show the top 5 articles from each section. To show the top five from either section you'd need to write some custom php. You'd need to load the top 5 from both and combine the arrays, then sort on hits and only show the top 5. This could leave you with all articles from one table and none from the other though. So it's possible, but a bit more complex.

Hope that helps. Showing top 5 from each table is the simplest, or merging all your articles into one table.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Most Read/View Articles

Hi,

I have got the hit counter field setup and working, which I am really pleased about. However, I am wondering if it's possible to make the field visible but not editable by users in cmsAdmin?

It seems if I make it a System Field then it just hides the field for all users except the Admin. Ideally, I'd like to make the hit counter field visible to all users but not editable, like the createdDate. The Admin should be able to edit the field though. Is that possible?

Thanks,

Greg

Re: [gversion] Most Read/View Articles

By Dave - February 13, 2012

Hi Greg,

We don't have an official way to do that, but try this undocumented method:

- Go to the "Field Editor" for your section
- Create a new "Separator" field
- Set the type to HTML with this content:
<tr>
<td>Hits</td>
<td><?php echo @$RECORD['num'] ?></td>
</tr>

- replace 'num' with the name of your hit counter field, eg: 'hits'

For some of the content in the field editor we make a variable called $RECORD available with the values of the current record so you can display them back if needed.

Hope that helps, let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Most Read/View Articles

Hi Dave,

Thanks for your help! That's a very nice workaround.

Regards,

Greg