- Posts: 30
COMMUNITY FORUM
K2 AJAX and item hits
- shenkwen
-
Topic Author
- Offline
- Junior Member
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
-
- Offline
- Platinum Member
- Posts: 15920
$K2('.itemRatingForm a').click(function(event){
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
-
Topic Author
- Offline
- Junior Member
- Posts: 30
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
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.
- Makis
-
- Offline
- New Member
- Posts: 10
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
-
- Offline
- Platinum Member
- Posts: 15920
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.
- Makis
-
- Offline
- New Member
- Posts: 10
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
-
- Offline
- Platinum Member
- Posts: 15920
<?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.
- Makis
-
- Offline
- New Member
- Posts: 10
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
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Makis
-
- Offline
- New Member
- Posts: 10
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
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Makis
-
- Offline
- New Member
- Posts: 10
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
-
- Offline
- Platinum Member
- Posts: 15920
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.
- Makis
-
- Offline
- New Member
- Posts: 10
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;
?>
and also changed
<?php echo JText::_('K2_READ'); ?> <b><?php echo round($newhits,0); ?></b> <?php echo JText::_('K2_TIMES'); ?>
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.