Keyword

K2 AJAX and item hits

  • shenkwen
  • shenkwen's Avatar Topic Author
  • Offline
  • Junior Member
More
9 years 8 months ago #146691 by shenkwen
K2 AJAX and item hits was created by shenkwen
It is great that k2 supports ajax. Although I have been using k2 for years, I just realized it and started using ajax to improve my k2 powered websites.

But I have had a problem.

On my page there are both item list from categories and a k2 content module showing the lasted item. I have made it so that when one of the items in the category is clicked, the item content in the module gets updated by ajax.
Live page is here, aoafinc.org/index.php?option=com_k2&view=itemlist&layout=category&task=category&id=2&Itemid=150&lang=en-us It is under developed and mostly Chinese, but you get the idea.

It seems good but the problem is, the item hits won't increase. ( I had to use some javascript to fake the item hits number displayed under the title for now ) Is there anyway to solve this? I am thinking maybe I need to hardcode some PHP file so that every time the 'url+&format=json' gets displayed, item hits increased. But I really know very little about k2 core, so any direction or hints will be greatly appreciated!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 8 months ago #146695 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
$K2('.itemRatingForm a').click(function(event){
As you can see the k2.js file uses the click event. Since the markup is generated after the loading of the page you should change the code to use the on() handler.

This will make the voting function available even if the elements are not loaded.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • shenkwen
  • shenkwen's Avatar Topic Author
  • Offline
  • Junior Member
More
9 years 8 months ago #146702 by shenkwen
Replied by shenkwen on topic K2 AJAX and item hits
Thanks Sir but I'm asking about the item hits, a.k.a how many times the items have been viewed, not the item rates

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 8 months ago #146708 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
Sorry my mistake.
The hits cannot increase since the page is not requested by Joomla!.
You can create a custom script which increases the hits column in the db.
 $db = JFactory::getDBO();
 $db->setQuery(
                        'UPDATE #__k2_items' .
                        ' SET hits = hits + 1' .
                        ' WHERE id = '.(int) $articleId
                );
 $db->query();

Where $articleId would be the item's id. You can use this as a starting point.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148484 by Makis
Replied by Makis on topic K2 AJAX and item hits
Hello,

Where should I place this code?
Create a new php file?
And how can I call it?
Or somewhere else?

Thank you

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148488 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
Please do not double post.
It depends on what you want to achieve.
If you are loading items via AJAX or JSON you can look at this commit: github.com/joomlaworks/k2/issues/248

Or if you are using this is as a part of another module/ extension then you need to add this snippet to your code.

Finally if you just want to tinker with the db, you can increase the hits cell of the xx_k2_items table manually or with a mySQL script.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148490 by Makis
Replied by Makis on topic K2 AJAX and item hits
Sorry about the double post.

I just need to increase the number of hits on an article, automatically
When it is 10 hits to show 11, when 11 to show 12 etc.

I have to use a mySQL script.. automated.

thank you!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148496 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
You can use this code (also you need to define $articleid)
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (isset($_GET['view']) && strtolower($_GET['view']) == 'item') {
 $articleId = (int) $_GET['id'];
 
 $db = JFactory::getDBO();
 $db->setQuery(
                        'UPDATE #__k2_items' .
                        ' SET hits = hits + 1' .
                        ' WHERE id = '.(int) $articleId
                );
 $db->query();
}

?>

and publish it in a K2 Tools module.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148498 by Makis
Replied by Makis on topic K2 AJAX and item hits
I really appreciate your help and quick replies.
I did that and nothing happens on the Article Hits.
Can you explain in (more) details how I can achieve that?

Article has 0 hits and the same after I published the module.

Thank you in advance

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148583 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
Make sure that the option to execute PHP code is enabled, furtemore make sure that the module is not cached. You can check that in the module's advanced settings.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148590 by Makis
Replied by Makis on topic K2 AJAX and item hits
Hello,

Thank you for your reply.
Unfortunately is not working.
When you said 'Make sure that the option to execute PHP code is enabled,' where should I Find this?7
In the module or else?

Thank you

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148596 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
Yes K2 Tools (custom code functionality) has this option.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148599 by Makis
Replied by Makis on topic K2 AJAX and item hits
Hello Krikor,

I have done everything you said and unfortunately the script doesn't work.
I think that it cant find article id.
I tried to increase 1 to 100 and see if the hits change, but no luck.
I have also enabled cloudflare, is this a problem?

Thank you

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148603 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 AJAX and item hits
That could be an issue since the output is cached The user is seeing a page generated from Cloudflare and not the actual item.

This is a known Joomla! issue (hits being cached) and I do not think that there is a solution available apart from that script.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #148664 by Makis
Replied by Makis on topic K2 AJAX and item hits
Hello again,

I think that there is another solution.
I managed to make some changes to the item.php on /website/templates/template_name/html/com_k2/templates/default
Around line 324 I add the code
<?php 
// REAL HITS
$oldhits=$this->item->hits;
$newhits=$oldhits * 8.5;
?>
That multiplies the real hits by 8.5 or whatever you want.
and also changed
<?php echo JText::_('K2_READ'); ?> <b><?php echo round($newhits,0); ?></b> <?php echo JText::_('K2_TIMES'); ?>
its 2-3 lines below to display the "multiplied" hits.
In that way you can see the real hits at the administrator side of joomla and at the front the "multiplied" hits in item view.
The same can be done in category view in the same folder.
So, the database is not altered in any way, just the displayed hits.
Hope that work for others also.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum