Keyword

K2 AJAX and item hits

  • shenkwen
  • shenkwen's Avatar Topic Author
  • Offline
  • Junior Member
More
9 years 1 month 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 1 month 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 1 month 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 1 month 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
8 years 11 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
8 years 11 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
8 years 11 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
8 years 11 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
8 years 11 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
8 years 11 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.


Powered by Kunena Forum