看到一個很酷的熱門內容呈現方式:
Display the most popular content in a way like c|Net's what's hot
範例:
<?php
/**
* This php snippet displays a list of links
* to most viewed pages
*
* To increase/decrease the number of nodes listed
* change the $list_length value to suit.
*
* Works with drupal 4.7.x & 4.6.x & 4.5.x
*
* Snippet submitted by aam
*/
$output = "\n";
$output .= "
/* ************** Hottest of all time *************************/
$list_length = 15;
$sql = "SELECT b.title, a.nid, b.created AS min_time, CASE WHEN a.totalcount > 0 THEN a.totalcount ELSE 1 END AS rec_cnt FROM node_counter a LEFT JOIN node b ON a.nid = b.nid ORDER BY rec_cnt DESC LIMIT $list_length";
$all_nodes = array();
$result = db_query($sql);
$i=0;
$max_time_all = 19000101;
$min_time_all = 99991231;
$max_rec_cnt_all = 0;
$min_rec_cnt_all = 999999999;
$rec_cnt_all = 0;
while ($mynode = db_fetch_object($result)) {
$i += 1;
$all_nodes[$i] = array(
'title' => $mynode->title,
'path' => ('/node/'.$mynode->nid),
'min_time' => format_date($mynode->min_time,'custom', 'Ymd')+0,
'rec_cnt' => $mynode->rec_cnt
);
$rec_cnt_all += $mynode->rec_cnt;
if ( $all_nodes[$i]['min_time'] > $max_time_all ) $max_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['min_time'] < $min_time_all ) $min_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['rec_cnt'] > $max_rec_cnt_all ) $max_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
if ( $all_nodes[$i]['rec_cnt'] < $min_rec_cnt_all ) $min_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
}
$a_x = 0; $a_y = 0;
$d_x = 500; $d_y = 500;
$ratio = 0.4;
//$output .= " rec_cnt_all = $rec_cnt_all \n";
//$output .= " max_time_all = $max_time_all \n";
//$output .= " min_time_all = $min_time_all \n";
//$output .= " max_rec_cnt_all = $max_rec_cnt_all \n";
//$output .= " min_rec_cnt_all = $min_rec_cnt_all \n";
$output .= "
$sum_rec_cnt = 0;
$myitems = array();
$i=1;
foreach ($all_nodes as $mynode) {
$myitems[$i] = $mynode;
$i += 1;
$sum_rec_cnt += $mynode['rec_cnt'];
if ( ($sum_rec_cnt+$mynode['rec_cnt'])/$rec_cnt_all > $ratio ) {
if ( $d_y-$a_y <= $d_x-$a_x ) {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_x-$a_x) );
$pos=$a_y;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_y-$a_y));
}
// set the target area
$a_x += $width;
}
else {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_y-$a_y) );
$pos=$a_x;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_x-$a_x));
}
// set the target area
$a_y += $width;
}
// reset algorithm
$i = 1;
$rec_cnt_all -= $sum_rec_cnt;
$sum_rec_cnt = 0;
unset($myitems);
$myitems = array();
}
}
$output .= "
\n";
/* ************** Hottest of today *************************/
$list_length = 15;
$sql = "SELECT b.title, a.nid, b.created AS min_time, CASE WHEN a.daycount > 0 THEN a.daycount ELSE 1 END AS rec_cnt FROM node_counter a LEFT JOIN node b ON a.nid = b.nid ORDER BY rec_cnt DESC LIMIT $list_length";
$all_nodes = array();
$result = db_query($sql);
$i=0;
$max_time_all = 19000101;
$min_time_all = 99991231;
$max_rec_cnt_all = 0;
$min_rec_cnt_all = 999999999;
$rec_cnt_all = 0;
while ($mynode = db_fetch_object($result)) {
$i += 1;
$all_nodes[$i] = array(
'title' => $mynode->title,
'path' => ('/node/'.$mynode->nid),
'min_time' => format_date($mynode->min_time,'custom', 'Ymd')+0,
'rec_cnt' => $mynode->rec_cnt
);
$rec_cnt_all += $mynode->rec_cnt;
if ( $all_nodes[$i]['min_time'] > $max_time_all ) $max_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['min_time'] < $min_time_all ) $min_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['rec_cnt'] > $max_rec_cnt_all ) $max_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
if ( $all_nodes[$i]['rec_cnt'] < $min_rec_cnt_all ) $min_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
}
$a_x = 0; $a_y = 0;
$d_x = 500; $d_y = 500;
$ratio = 0.4;
//$output .= " rec_cnt_all = $rec_cnt_all \n";
//$output .= " max_time_all = $max_time_all \n";
//$output .= " min_time_all = $min_time_all \n";
//$output .= " max_rec_cnt_all = $max_rec_cnt_all \n";
//$output .= " min_rec_cnt_all = $min_rec_cnt_all \n";
$output .= "
$sum_rec_cnt = 0;
$myitems = array();
$i=1;
foreach ($all_nodes as $mynode) {
$myitems[$i] = $mynode;
$i += 1;
$sum_rec_cnt += $mynode['rec_cnt'];
if ( ($sum_rec_cnt+$mynode['rec_cnt'])/$rec_cnt_all > $ratio ) {
if ( $d_y-$a_y <= $d_x-$a_x ) {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_x-$a_x) );
$pos=$a_y;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_y-$a_y));
}
// set the target area
$a_x += $width;
}
else {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_y-$a_y) );
$pos=$a_x;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_x-$a_x));
}
// set the target area
$a_y += $width;
}
// reset algorithm
$i = 1;
$rec_cnt_all -= $sum_rec_cnt;
$sum_rec_cnt = 0;
unset($myitems);
$myitems = array();
}
}
$output .= "
\n";
/* ************** Hottest of last week *************************/
$list_length = 15;
$sql = "SELECT title, path, MAX(timestamp) AS min_time, COUNT(*) AS rec_cnt FROM accesslog WHERE path LIKE '%book/%' OR path LIKE '%node/%' OR path LIKE '%image/%' GROUP BY title, path ORDER BY rec_cnt DESC LIMIT $list_length";
$all_nodes = array();
$result = db_query($sql);
$i=0;
$max_time_all = 19000101;
$min_time_all = 99991231;
$max_rec_cnt_all = 0;
$min_rec_cnt_all = 999999999;
$rec_cnt_all = 0;
while ($mynode = db_fetch_object($result)) {
$i += 1;
$all_nodes[$i] = array(
'title' => $mynode->title,
'path' => $mynode->path,
'min_time' => format_date($mynode->min_time,'custom', 'Ymd')+0,
'rec_cnt' => $mynode->rec_cnt
);
$rec_cnt_all += $mynode->rec_cnt;
if ( $all_nodes[$i]['min_time'] > $max_time_all ) $max_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['min_time'] < $min_time_all ) $min_time_all = $all_nodes[$i]['min_time'];
if ( $all_nodes[$i]['rec_cnt'] > $max_rec_cnt_all ) $max_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
if ( $all_nodes[$i]['rec_cnt'] < $min_rec_cnt_all ) $min_rec_cnt_all = $all_nodes[$i]['rec_cnt'];
}
$a_x = 0; $a_y = 0;
$d_x = 500; $d_y = 500;
$ratio = 0.4;
//$output .= " rec_cnt_all = $rec_cnt_all \n";
//$output .= " max_time_all = $max_time_all \n";
//$output .= " min_time_all = $min_time_all \n";
//$output .= " max_rec_cnt_all = $max_rec_cnt_all \n";
//$output .= " min_rec_cnt_all = $min_rec_cnt_all \n";
$output .= "
$sum_rec_cnt = 0;
$myitems = array();
$i=1;
foreach ($all_nodes as $mynode) {
$myitems[$i] = $mynode;
$i += 1;
$sum_rec_cnt += $mynode['rec_cnt'];
if ( ($sum_rec_cnt+$mynode['rec_cnt'])/$rec_cnt_all > $ratio ) {
if ( $d_y-$a_y <= $d_x-$a_x ) {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_x-$a_x) );
$pos=$a_y;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_y-$a_y));
}
// set the target area
$a_x += $width;
}
else {
// drawing area
$width = round( ($sum_rec_cnt/$rec_cnt_all) * ($d_y-$a_y) );
$pos=$a_x;
foreach($myitems as $node) {
$output .= "
\n";
$pos += round(($node['rec_cnt']/$sum_rec_cnt)*($d_x-$a_x));
}
// set the target area
$a_y += $width;
}
// reset algorithm
$i = 1;
$rec_cnt_all -= $sum_rec_cnt;
$sum_rec_cnt = 0;
unset($myitems);
$myitems = array();
}
}
$output .= "
\n";
/* ******* end of container ************ */
$output .= "
\n";
$output .= "\n";
$output .= "熱門內容 全部\n";
$output .= " 今天\n";
$output .= " 上週\n";
$output .= "區塊愈大,代表愈熱門。\n";
$output .= "區塊顏色愈深,表示是比較舊的內容\n";
$output .= "\n";
print $output;
?>
這個酷
不曉得會不會有安全問題@@
安全問題?
安全問題? 為甚麼啊?
沒有
是我自己看錯了!真的挺棒的:)
真的挺棒的,加以應
真的挺棒的,加以應用應該會很有趣...