Page 1 of 1

News module - Most Popular news feature

Posted: Mon Feb 11, 2008 2:38 pm
by fritzfs
Hello guys,
I've made changes in CMSms to support Most Popular News feature. It works by counting the hits on news.

1) execute this SQL in your CMSms database:

Code: Select all

ALTER TABLE `cms_module_news` ADD `hits` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0';
2) edit file Modules/News/action.detail.php:
Just after this code:

Code: Select all

		if ($row)
		{
			$onerow = new stdClass();
add this code:

Code: Select all

			$sql = "UPDATE ".cms_db_prefix()."module_news SET hits=hits+1 WHERE news_id = ".
				$row['news_id'];
			$db->Execute($sql);
3) create UDT that will display most popular news:

Code: Select all

global $gCms;

$db = &$gCms->db;

$result = array();

$q = "SELECT news_title FROM `cms_module_news` ORDER BY hits DESC LIMIT 5";

$dbresult = $db->Execute( $q );
if( $dbresult ) {
    while ( $row = $dbresult->FetchRow() ) {
        array_push($result, $row['news_title']);
    }
}

echo implode(",", $result);

Re: News module - Most Popular news feature

Posted: Tue Mar 04, 2008 8:38 am
by Sven
Thanks for sharing, pal.
Gonna try it soon.

Re: News module - Most Popular news feature

Posted: Tue Mar 04, 2008 2:19 pm
by calguy1000
This can be done with no hacking to the news databases or php code.  You could use the news extra field, and a simple UDT that you call from your detail template to update the news_extra field for that article.

Re: News module - Most Popular news feature

Posted: Wed Mar 26, 2008 11:51 am
by kazkas
The way I think it should be:

showcounts UTD:

Code: Select all

global $gCms;
$db = &$gCms->db;
$id = $gCms->smarty->_tpl_vars['entry']->id;
$query = "UPDATE `cms_module_news` SET news_extra=IF(ISNULL(news_extra),1,1+news_extra) WHERE `news_id`=$id";
echo $query;
$rez = $db->Execute($query);
most_popular UDT:

Code: Select all

global $gCms;
$db = &$gCms->db;
$result = array();
$q = " SELECT * FROM `cms_module_news` ORDER BY ( 1 + news_extra ) DESC LIMIT 5 ";
$dbresult = $db->Execute( $q );
if( $dbresult ) {
    while ( $row = $dbresult->FetchRow() ) {
      $onerow = new stdClass();
      $onerow->title = $row['news_title'];
      $onerow->id = $row['news_id'];
      $onerow->views = $row['news_extra'];
      $result[] = $onerow;
    }
}
$gCms->smarty->assign("most_popular", $result);
add {showcounts} to your detail template, and in the template you want to add most popular news, just add:

Code: Select all

{most_popular}
<ol>
{foreach from=$most_popular item=entry}
    <li>{$entry->id}{$entry->title}, {$entry->views} views</li>
{/foreach}
</ol>