<?php
require_once ('header.php');

$cid = intval($_GET["cid"]);
$msg_id = intval($_GET["msg_id"]);

location_to_murl("view.php?cid=".$cid."&msg_id=".$msg_id);

$wznav=get_msgcate($cid);
if ($html_flag==1){
   $map_nav = '<a href=list-'.$wznav['cate1_id'].'.html>'.$wznav['cate1_name'].'</a><span>></span>
<a href=list-c-'.$wznav['cate2_id'].'.html>'.$wznav['cate2_name'].'</a>';
}else{
   $map_nav = '<a href=list.php?cateid='.$wznav['cate1_id'].'>'.$wznav['cate1_name'].'</a><span>></span>
<a href=list.php?cid='.$wznav['cate2_id'].'>'.$wznav['cate2_name'].'</a>';
}

/*
$sql="select * from {$tablepre}message m 
      inner join {$tablepre}categorymng cm on m.msg_id=cm.msg_id 
      inner join {$tablepre}category c on c.category_id=cm.category_id where cm.msg_id='".$msg_id."'";
*/
$sql="select m.*,pic.attach_path,cm.* from {$tablepre}message m 
      inner join {$tablepre}categorymng cm on m.msg_id=cm.msg_id 
	  inner join {$tablepre}attachmng pic ON pic.msg_id = m.msg_id and pic.category_id = cm.category_id
      inner join {$tablepre}category c on c.category_id=cm.category_id where cm.msg_id='".$msg_id."' and c.category_id='".$cid."'";
$row = $db->get_row($sql);

$title=$row["msg_title"];
$content=$row["msg_content"];
$keyword=$row["msg_keyword"];
$category_id=$row["category_id"];
$category_name=$row["category_name"];
$dt=dateFormat($row["msg_createtime"],"Y-m-d H:i");
$category_hits=$row['category_hits'];
$hits_num=$row['hits_num'];
$fav_num=$row['fav_num'];
$down_num=$row['down_num'];
$down_num=$row['down_num']; 
$msg_demourl=$row['msg_demourl'];
$msg_downurl=$row['msg_downurl'];
$attach_path=$row['attach_path'];

$content=$row["msg_content"];

$kwarr=explode(",",$keyword);
foreach ($kwarr as $k=>$v){
    //$likestr.=" msg_title like '%".$v."%' or";
	$likestr.=" msg_keyword like '%".$v."%' or";
}

$otherkw=substr($likestr,0,strlen($likestr)-2);

//识别是否文档文件
$ext = end(explode('.', $msg_downurl));
$ext = strtolower($ext);

//$likestr .=" msg_tuijian=1";
//$otherkw=substr($likestr,0,strlen($likestr));

$sql="select m.*,cm.category_id,cm.hits_num,cm.down_num,cm.fav_num,pic.thumbnail_path,
      (select category_parentid from {$tablepre}category where category_id=cm.category_id) as category_parentid
      from {$tablepre}message m 
      inner join {$tablepre}categorymng cm on cm.msg_id=m.msg_id
	  inner join {$tablepre}attachmng pic ON pic.msg_id = m.msg_id
	  where pic.index_box=1 and cm.msg_id<>'".$msg_id."' and (".$otherkw.") and m.msg_status=1 GROUP BY cm.msg_id order by m.msg_id desc,pic.attach_id asc limit 4";
$arrkw  = $db->get_all($sql);

//如果关键字查询的记录小于4，就用推荐信息补充
$kw_num = count($arrkw);
if ($kw_num>0 && $kw_num<4){
   $sql="select m.*,cm.category_id,cm.hits_num,cm.down_num,cm.fav_num,pic.thumbnail_path from {$tablepre}message m 
      inner join {$tablepre}categorymng cm on cm.msg_id=m.msg_id
	  inner join {$tablepre}attachmng pic ON pic.msg_id = m.msg_id
	  where pic.index_box=1 and cm.msg_id<>'".$msg_id."' and msg_tuijian=1 and m.msg_status=1 GROUP BY cm.msg_id order by m.msg_id desc,pic.attach_id asc limit ".(4-$kw_num)."";
   $arrtj  = $db->get_all($sql);
   $arrkw  = array_merge($arrkw,$arrtj);
}

$sql="select m.*,cm.category_id,cm.hits_num,cm.down_num,cm.fav_num,pic.thumbnail_path from {$tablepre}message m 
      inner join {$tablepre}categorymng cm on cm.msg_id=m.msg_id
	  inner join {$tablepre}attachmng pic ON pic.msg_id = m.msg_id
	  where pic.index_box=1 and cm.msg_id<>'".$msg_id."' and cm.category_id='".$cid."' and m.msg_status=1 GROUP BY cm.msg_id order by cm.down_num desc limit 10";
$tparr  = $db->get_all($sql);

//下载排行数组键值重新排
foreach ($tparr as $key=>$val){
   $toparr[$key+1]=$val;
}

$sql = "select * from {$tablepre}category where category_id='".$cid."'";
//$sql = "select * from {$tablepre}category where category_parentid=0";
$row = $db->get_row($sql);
$tag = explode(",",$row['category_desc']);

include($template->getfile('view'));
?>