[SOLVED] News - Sort by Most Viewed
[SOLVED] News - Sort by Most Viewed
Hi everyone,
A client needs to sort news articles by most viewed. I found this article http://forum.cmsmadesimple.org/viewtopi ... ed#p213345 but it's a bit old and haven't tested yet. Is it possible to modify this to work with the news module or is there a better and easiest way to achieve this?
Thanks in advance for any help on this.
A client needs to sort news articles by most viewed. I found this article http://forum.cmsmadesimple.org/viewtopi ... ed#p213345 but it's a bit old and haven't tested yet. Is it possible to modify this to work with the news module or is there a better and easiest way to achieve this?
Thanks in advance for any help on this.
Last edited by mr.bacan on Tue Mar 18, 2014 7:35 pm, edited 1 time in total.
Re: News - Sort by Most Viewed
I can't think of an easier way. Open phpMyAdmin, or other mySql browser, and compare the CGBlog tables with the news tables. You should be able to change table names and field names to match.
Re: News - Sort by Most Viewed
Thanks for this solution, but my database knowledge is very limited, so I'll probably mess things upWishbone wrote:I can't think of an easier way. Open phpMyAdmin, or other mySql browser, and compare the CGBlog tables with the news tables. You should be able to change table names and field names to match.

Calguy mentioned Hitcounter as another option, and it actually looks like an easier one, but I don't get how to assign values to the "extra" field in news to sort by using that field info.
Re: News - Sort by Most Viewed
Hello everyone, I've been trying different methods to accomplish this task without any luck. Mainly because I've no programming skills
Hitcounter seemed to be a great and easy solution to this, but I have no idea how to set it up to work with the News Module and then get it to sort the way I need it.
Could anyone please give me a hand.
Thanks in advance.

Hitcounter seemed to be a great and easy solution to this, but I have no idea how to set it up to work with the News Module and then get it to sort the way I need it.
Could anyone please give me a hand.
Thanks in advance.
Re: News - Sort by Most Viewed
From the hitcounter help I can see how it counts the hits. But I don't think it will help you sorting the news items. At least not efficiently.
The other post you refer to seems to do what you want but it's rather old.
NOT TESTED, MAKE BACKUP BEFORE USE, NO WARRANTY, USE AT YOUR OWN RISC, ETC...
The other post you refer to seems to do what you want but it's rather old.
NOT TESTED, MAKE BACKUP BEFORE USE, NO WARRANTY, USE AT YOUR OWN RISC, ETC...
Code: Select all
edit: removed
Last edited by velden on Mon Mar 17, 2014 9:24 am, edited 2 times in total.
Re: News - Sort by Most Viewed
Hi all,
I also need a lot of this function. I tried this UDT (I also corrected a mistake cgblog_id to news_id), but it did not work. Any suggestions?
Thanks
I also need a lot of this function. I tried this UDT (I also corrected a mistake cgblog_id to news_id), but it did not work. Any suggestions?
Thanks
velden wrote:From the hitcounter help I can see how it counts the hits. But I don't think it will help you sorting the news items. At least not efficiently.
The other post you refer to seems to do what you want but it's rather old.
NOT TESTED, MAKE BACKUP BEFORE USE, NO WARRANTY, USE AT YOUR OWN RISC, ETC...
Code: Select all
$query ="UPDATE ".cms_db_prefix()."module_news SET news_extra=? WHERE cgblog_id=?"; $db->Execute($query,array($row, $id)); return;
Re: News - Sort by Most Viewed
This should work.
MAKE BACKUP BEFORE USE, NO WARRANTY, USE AT YOUR OWN RISC, ETC...
Note the zero padding because it seemed that the sorting was lexicographically (e.g. 2 > 100)
I saved this UDT named 'NewsHit'.
At top of News DETAIL template I add:
Call News module:
MAKE BACKUP BEFORE USE, NO WARRANTY, USE AT YOUR OWN RISC, ETC...
Code: Select all
// Simple news entry views counter
$cms = cmsms();
$db =& $cms->GetDb();
// Check if News installed
if(!$cms->GetModuleInstance('News')) {
return "News module not installed!";
}
// Check if parameter id given
$id = '';
if(isset($params['id'])) {
$id = $params['id'];
} else {
return "Error: no id given";
}
// Try to update given field in given entry.
$query = "SELECT news_extra FROM ".cms_db_prefix()."module_news WHERE news_id =?";
$row = $db->GetOne($query, array($id));
$row = (int)$row;
// If field dosen't consist numbers, convert it to zero.
if(!is_numeric($row)) {
$row == 0;
}
// Add one to results and update db.
$row++;
// zero padding row var so sorting will work as expected
$row = sprintf('%08d', $row);
$query ="UPDATE ".cms_db_prefix()."module_news SET news_extra=? WHERE news_id=?";
$db->Execute($query,array($row, $id));
return;
I saved this UDT named 'NewsHit'.
At top of News DETAIL template I add:
Code: Select all
{NewsHit id=$entry->id}
Code: Select all
{news sortby='news_extra'}
Last edited by velden on Mon Mar 17, 2014 2:49 pm, edited 1 time in total.
Re: News - Sort by Most Viewed
Hi velden,
I tried with the previous UDT, not only did not work, but it has created a problem in the News module.
Practically in the backend, inside the articles, has vanished the "Summary" and for previous articles inserted "Summary" and "Content" are united in "Content".
I had made a backup with the "Mysql Dump" module but unfortunately it is corrupt and I could not use it.
Thankfully the news that were previously inserted, appear correctly in FrontEnd, but I could not do the corrections.
How could I bring back the Summary in News? Is possible to do it with a UDT?
Thank you
P.s: I also tried this new UDT on another installation, but when I push the botton "Run UDT", under "Output:" appears: "Error: no id given".
I tried with the previous UDT, not only did not work, but it has created a problem in the News module.
Practically in the backend, inside the articles, has vanished the "Summary" and for previous articles inserted "Summary" and "Content" are united in "Content".
I had made a backup with the "Mysql Dump" module but unfortunately it is corrupt and I could not use it.
Thankfully the news that were previously inserted, appear correctly in FrontEnd, but I could not do the corrections.
How could I bring back the Summary in News? Is possible to do it with a UDT?
Thank you
P.s: I also tried this new UDT on another installation, but when I push the botton "Run UDT", under "Output:" appears: "Error: no id given".
Re: News - Sort by Most Viewed
I doubt that. If I understand the problem of the initial code it did nothing but checking for the existing of the News module.farhadprm wrote:Hi velden,
I tried with the previous UDT, not only did not work, but it has created a problem in the News module.
Because that failed (old method) it did nothing more than that.
Something else must be going on.
Re: News - Sort by Most Viewed
Thank you for your reply.
Before trying your UDT, I tried with the two other UDT which i found in this link:
http://forum.cmsmadesimple.org/viewtopi ... ed#p213345
where I changed "Cgblog" in "News".
Could the problem due to these two? How can I solve my problem?
Thank you so much
Before trying your UDT, I tried with the two other UDT which i found in this link:
http://forum.cmsmadesimple.org/viewtopi ... ed#p213345
where I changed "Cgblog" in "News".
Could the problem due to these two? How can I solve my problem?
Thank you so much
Re: News - Sort by Most Viewed
I'm sorry, I really don't know as I don't know what went wrong.
Re: News - Sort by Most Viewed
@velden
Thanks so much for that UDT, it seems to be working great on adding an incremental value to the extra field each time a news is read. What I can't figure out is why the sort='news_extra' option breaks the template. It happens only when using that option, and no matter what I do it won't work.
Here's an example of a news page with sort='news_extra', and sort='news_date'. http://iscreatividad.com/testsite/
It doesn't show the list of news when sorting by the extra field, but it does when using any other sorting method. I'm using CMSMS 1.11.10 if it helps
Thanks again for your help on this.
Thanks so much for that UDT, it seems to be working great on adding an incremental value to the extra field each time a news is read. What I can't figure out is why the sort='news_extra' option breaks the template. It happens only when using that option, and no matter what I do it won't work.
Here's an example of a news page with sort='news_extra', and sort='news_date'. http://iscreatividad.com/testsite/
It doesn't show the list of news when sorting by the extra field, but it does when using any other sorting method. I'm using CMSMS 1.11.10 if it helps

Thanks again for your help on this.
Re: News - Sort by Most Viewed
Confirmed after upgrade to 1.11.10:
http://dev.cmsmadesimple.org/bug/view/9813
http://dev.cmsmadesimple.org/bug/view/9813
Re: News - Sort by Most Viewed
Time casually asked - what's the "better" choice for coding?
or
Code: Select all
// Check if News installed
if(!$cms->GetModuleInstance('News')) {
return "News module not installed!";
}
Code: Select all
// Check if News installed
if(!CMSModule::GetModuleInstance('News')) {
return "News module not installed!";
}
Re: News - Sort by Most Viewed
I solved the problem by making a backup of the news tables in Mysql, uninstalling the module, import the tables and reinstall the News module. I'm going to try again UDT (This time on a development site!).
Edit 1: UDT does not work for me. I get this error in output: Error: no id Given. I have 1.11.10 with 2.14.2 version of News module.
Edit 2: I upgrade the News module to 2.14.3 (svn version) and now I can confirm it works.

Edit 1: UDT does not work for me. I get this error in output: Error: no id Given. I have 1.11.10 with 2.14.2 version of News module.
Edit 2: I upgrade the News module to 2.14.3 (svn version) and now I can confirm it works.
velden wrote:I'm sorry, I really don't know as I don't know what went wrong.