News module - Most Popular news feature

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
fritzfs
New Member
New Member
Posts: 8
Joined: Thu Feb 07, 2008 3:39 pm

News module - Most Popular news feature

Post 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);
Sven

Re: News module - Most Popular news feature

Post by Sven »

Thanks for sharing, pal.
Gonna try it soon.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: News module - Most Popular news feature

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
kazkas

Re: News module - Most Popular news feature

Post 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>
Post Reply

Return to “Tips and Tricks”