just a quick post to turn this long thread in to a quick fix...
good work on the script erwanpia... way coolio daddio
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();
}
//------------------------------------------------------------------------------
?>