most read articles : start counting today !

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
erwanpia
Posts: 39
Joined: Wed 21. Apr 2004, 10:28
Location: Breizh - Europa
Contact:

most read articles : start counting today !

Post by erwanpia »

Hello,

I give you here the code to count articles read, the code for displaying most read articles will come when I figure out the best way to integrate it in phpwcms templates :

in include/inc_front/content.article.inc.php, line 232, just before

Code: Select all

	}
				mysql_free_result($cresult);
add the following :

Code: Select all

if ($count_str_sql=="")
{$count_str_sql ="insert into "  . DB_PREPEND . "article_read_count values(now() , " .$content["article_id"] . "," . $content["cat_id"]  . ")";
$count_sql = mysql_query( count_str_sql,$db);
if (!$count_sql) echo "sql error : " . count_sql . " - " . $count_str_sql;
}
I put the

Code: Select all

if ($count_str_sql=="")

because the thing is executed twice and you don't want to double the log


you have to create the following mysql table with your DB_PREPEND :

Code: Select all

CREATE TABLE `phpwcms_article_read_count` (
  `date_read` datetime NOT NULL default '0000-00-00 00:00:00',
  `article_id` int(11) NOT NULL default '0',
  `cat_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`date_read`)
) TYPE=MyISAM;
     
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

do you have a demo somewhere?
http://www.studmed.dk Portal for doctors and medical students in Denmark
erwanpia
Posts: 39
Joined: Wed 21. Apr 2004, 10:28
Location: Breizh - Europa
Contact:

Post by erwanpia »

sorry I created a new thread instead of replying can you moderator correct that one :?

anyway bold - sorry you're frold - answer to yours is here :

http://phpwcms.de/forum/viewtopic.php?p=28540
User avatar
pSouper
Posts: 1552
Joined: Tue 11. Nov 2003, 15:45
Location: London
Contact:

Complete script : save and enjoy - as i do

Post by pSouper »

just a quick post to turn this long thread in to a quick fix...
good work on the script erwanpia... way coolio daddio 8)

Code: Select all

<?PHP
//  WHAT AM I ?------------------------------------------------------------------------------         
// THIS IS THE MOST_READ {RT} by 'erwanpia'
// http://phpwcms.de/forum/viewtopic.php?t=4816
// no credit at all is to be taken by pSouper as he has done nothing at all :)

// WHAT TO DO?
// parse SQL query (phpMyAdmin will do this ;) )...
/*
CREATE TABLE `phpwcms_article_read_count` (
  `date_read` datetime NOT NULL default '0000-00-00 00:00:00',
  `article_id` int(11) NOT NULL default '0',
  `cat_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`date_read`)
) TYPE=MyISAM;
*/
// Then...
// save this script as phpwcms_template/inc_script/frontend_render/most_read.php

// HOW TO USE?
// As this is a new replacement tag it is to use used as with any replacement tag.
// place {READ_MORE} tag in the desired page and your done.

// WHERE TO GO FOR HELP & SUPPORT?
// the origonal thread is the best place to start http://www.phpwcms.de/forum/viewtopic.php?t=4820
//------------------------------------------------------------------------------  

function get_most_read_articles($template_default, $max_cnt_links=0, $dbcon) {
   // find all new articles

   $max_cnt_links = intval($max_cnt_links);
   $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
   
   $sql ="SELECT article_id, count( article_id ) c
FROM ".DB_PREPEND."phpwcms_article_read_count
GROUP BY article_id
ORDER BY c DESC, date_read desc 
$limit ";
   $arr_id=array();
   $arr_count=array();
   if($result = mysql_query($sql, $dbcon)) {
      $count_results = mysql_num_rows($result); $count = 0;
      while ($row = mysql_fetch_row($result))
         {
         array_push($arr_id, $row[0]);
         $arr_count[$row[0]]= $row[1];
         }
reset($arr_id);
   $new_links = "";
   $count_results = count($arr_id); $count = 0;
while (list($k,$v)=each($arr_id))
{
   $sql =   "SELECT article_id, article_title, article_cid, UNIX_TIMESTAMP(article_tstamp) AS article_date ".
         "FROM ".DB_PREPEND."phpwcms_article WHERE ".
         "article_public=1 AND article_aktiv=1 AND article_deleted=0 AND ".
         "article_begin < NOW() AND article_end > NOW()  and article_id = ". $v ;

   // new articles list

   $target = ($template_default["link_target"]) ? ' target="'.$template_default["link_target"].'"' : "";
   if($result = mysql_query($sql, $dbcon)) {
   
      while ($row = mysql_fetch_row($result)) {
         $count++;
         if($template_default["link_length"] && strlen($row[1]) > $template_default["link_length"]) {
            $article_title = substr($row[1], 0, $template_default["link_length"]).$template_default["cut_title_add"];
         } else {
            $article_title = $row[1];
         }
         /*if(trim($template_default["date_format"])) {
            $article_title = international_date_format(
                           $template_default["date_language"],
                           $template_default["date_format"],
                           $row[3]).$article_title;
         }*/
         $article_title = "• " . $article_title ;
         $show_count = $arr_count[$row[0]] . " readers";
         $new_links .= $template_default["link_before"].$template_default["link_symbol"];
         $new_links .= '<a href="index.php?id='.$row[2].','.$row[0].',0,0,1,0"';
         $new_links .= $target." title='$show_count' alt='$show_count'>".html_specialchars($article_title)."</a>";
         
         //try to remove possible unwanted after - if not enclosed before.link.after
         if($new_links && !$template_default["link_before"] && $count < $count_results) {
            $new_links .= $template_default["link_after"];
         }
      }
      mysql_free_result($result);
   }
}
}
   //enclose whole
   if($new_links) $new_links = $template_default["before"].$new_links.$template_default["after"];
   
   return $new_links;
}
// MOD // most read article sorted by popularity
if( ! ( strpos($content["all"],'{MOST_READ:')===false ) ) {
   $content["all"] = preg_replace('/\{MOST_READ:(\d+)\}/e','get_most_read_articles($template_default["news"],$1,$db);',$content["all"]);
}
/////////
          if ($count_str_sql=="" && $content["article_id"]) {
          $count_str_sql ="insert into phpwcms_article_read_count values(now() , " .$content["article_id"] . "," . $content["cat_id"]  . ")";
          $count_sql = mysql_query($count_str_sql,$db);
          if (!$count_sql) echo "sql error : " . $count_sql . " - " . $count_str_sql. "\n<br>";
          echo mysql_error();
          }
//------------------------------------------------------------------------------
?>
Last edited by pSouper on Wed 6. Jul 2005, 12:35, edited 1 time in total.
iceman
Posts: 82
Joined: Tue 13. Jul 2004, 09:44
Contact:

Post by iceman »

Hi Can you please help.
I have created the table (no problems), I have created the php file "phpwcms_template\inc_script\frontend_render\most_read.php" and the contents are as below.

However nothing is being inserted into the table.

I had to create the "inc_script" and "frontend_render" directories - is this normal? What is supposed to be executing the script in this directory?

Please help.

PS: 1.1RC4.

Thanks

Mike
Last edited by iceman on Thu 30. Jun 2005, 04:56, edited 1 time in total.
iceman
Posts: 82
Joined: Tue 13. Jul 2004, 09:44
Contact:

Post by iceman »

ah found that you need at least 1.1RC4 27.08 version to do this.

I am running 22.06 :(
brans

Post by brans »

there shouldn't be no problem to update your release?
iceman
Posts: 82
Joined: Tue 13. Jul 2004, 09:44
Contact:

Post by iceman »

I have upgraded my local version to 1.2.1 and it went fairly smoothly, a couple of problems but I need to do some more investigation before I post asking for solutions.
User avatar
pSouper
Posts: 1552
Joined: Tue 11. Nov 2003, 15:45
Location: London
Contact:

BUG & FIX

Post by pSouper »

Hi guys,

I notice a small in the code that will affect ALL versions of this code.
When a category is set to a number other than -1 (show first article in full)
the variable $content["article_id"] is undefined therefore throws an SQL error.

FIX..

Code: Select all

// FIND
		  if ($count_str_sql=="") {

// REPLACE WITH
		  if ($count_str_sql=="" && $content["article_id"]) {

The additional argument in the IF statements ensures $content["article_id"] is set before loggin - this makes sence as an artilce should not be recorded as 'read' if it has not been displayed yet (or displayed in full at least).
User avatar
Kosse
Posts: 1066
Joined: Thu 9. Sep 2004, 12:08
Location: Brussels, Belgium
Contact:

Post by Kosse »

Hi PSouper,

shouldn' t you notify this on mantis (to OG or move this single post to bugs?) ?

Cheers
User avatar
pSouper
Posts: 1552
Joined: Tue 11. Nov 2003, 15:45
Location: London
Contact:

Post by pSouper »

Kosse wrote:shouldn' t you notify this on mantis (to OG or move this single post to bugs?) ?
Not this time Kosse as it is an unaficial mod therfor an unaficial fix.

I would imagine OG has enough on his plate without dealing with our small mods ;)
User avatar
Kosse
Posts: 1066
Joined: Thu 9. Sep 2004, 12:08
Location: Brussels, Belgium
Contact:

Post by Kosse »

:? mmm, yep, makes sense! Wise

Nevertheless, all this good stuff is too good to be lost in the forum. :P

Cheers
Da5id
Posts: 42
Joined: Fri 13. Jan 2006, 20:44

Little problem found {MOST_READ} Replacement Tag

Post by Da5id »

There is a little mistake in the source code:

Code: Select all

$count_str_sql ="insert into phpwcms_article_read_count values(now() , " .$content["article_id"] . "," . $content["cat_id"]  . ")";
should be

Code: Select all

$count_str_sql ="insert into ".DB_PREPEND."phpwcms_article_read_count values(now() , " .$content["article_id"] . "," . $content["cat_id"]  . ")";
Tested with phpwcms Version 1.26
Works great!
Post Reply