0

I am in a big trouble. I have been using ajax to fetch k2 item and display it on my page. On my page there are both k2 category list and article area, when one of the items in the category is clicked, I prevent default link and use ajax to fetch k2 item content(by adding '&format=json' to the url) to display in the article area. I just realized this way the item won't be considered viewed, thus item.hits will remain 0. I have been spending too much time on the layout to change it, so I think my best bet is to find out how k2 item calculate how many times it has been viewed, and maybe there is some trick to force it.

1 Answer 1

2

In K2, articles are represented by K2ModelItem (com_k2/models/item.php, line 892), where you can find:

function hit($id)
{

    $row = JTable::getInstance('K2Item', 'Table');
    $row->hit($id);
}

If you are fetching items directly from tables, then it is not consistent with K2 model. It would be better to follow the general MVC pattern and access them via K2ModelItem... or just increment hits calling in the same way than model works.

3
  • Thank you very much Sir for the hint. I have read the item.php file. I guess this function is to increment item's hits property, is it right? I am thinking if I am to keep my ajax layout, I will have to write some PHP code to manually call this function. To do that, I need to know more about how k2 functions: Originally, when is this function called? Is it when the users click relative link or when some contents get displayed? Thank you very much again
    – shenkwen
    Commented Aug 17, 2015 at 14:09
  • Hits are added when item is shown
    – Anibal
    Commented Aug 17, 2015 at 15:26
  • What exactly can be considered "shown". Apparently, by fecthing the item content and display them is not considered "shown". By the way, to better demonstrate the problem, the live page is here:aoafinc.org/…. It is underdeveloped and mostly in Chinese, but you get the idea. And I have posted a new SO question here:stackoverflow.com/questions/32054153/…. You can move there if you want.
    – shenkwen
    Commented Aug 17, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.