<?php
require_once('header.php');
include_once("classes/pageclass.php");

$category_id = intval($_GET["category_id"]);
$kw          = flc_request(trim($_GET["kw"]));

$page        = intval($_GET["page"]);
if ($page==0) $page=1;

location_to_murl("search.php?category_id=".$category_id."&kw=".$kw."&page=".$page);

//$tagsql     = "select * from {$tablepre}category where category_status=1 and category_desc like '%".$kw."%' limit 1";
$tagsql  = "SELECT ca.category_id,ca.category_desc FROM category ca
            INNER JOIN categorymng cm ON ca.category_id = cm.category_id
            INNER JOIN message m ON cm.msg_id = m.msg_id
            WHERE m.msg_title LIKE '%".$kw."%' OR m.msg_keyword LIKE '%".$kw."%' OR m.msg_tuijian=1 ORDER BY m.msg_tuijian asc LIMIT 1";

$tagarr  = $db->get_row($tagsql);

$taglist = explode(",",$tagarr['category_desc']);
$taglist = array_slice($taglist,0,12);

$str = "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 (m.msg_title like '%".$kw."%' or m.msg_keyword like '%".$kw."%') and 
		m.msg_status=1 and m.msg_starttime<='".$getnow."' and m.msg_endtime>='".$getnow."' 
		GROUP BY cm.msg_id order by m.msg_id desc,pic.attach_id asc"; 	   

$rs      = $db->query($str,true);
$myrow   = $rs->fetchAll();
$numrows = count($myrow);

if ($html_flag==1){
   $p       = new html_page;
   $p->setvar(array("category_id" => $category_id,"kw" => $kw)); 
   $p->set($site_num,$numrows,$psize,'search'); 
}else{
   $p       = new desk_page;
   $p->setvar(array("category_id" => $category_id,"kw" => $kw)); 
   $p->set($site_num,$numrows,$psize);  
}

$rsx=$db->query($str." limit ".$p->limit(),true);

//如果有找到就不显示推荐信息,否则显示
if ($numrows!=0){
	 $i=0;
	 $ListArray = array();
	 while ($myrow = $rsx->fetch()) {
		 $ListArray[$i]['msg_id']=$myrow['msg_id'];
		 $ListArray[$i]['msg_title']=$myrow['msg_title'];
		 $ListArray[$i]['category_parentid']=$myrow['category_parentid'];
		 $ListArray[$i]['category_id']=$myrow['category_id'];
		 $ListArray[$i]['msg_demourl']=$myrow['msg_demourl'];		 
		 $ListArray[$i]['msg_downurl']=$myrow['msg_downurl'];
		 $ListArray[$i]['msg_integral']=$myrow['msg_integral'];
		 $ListArray[$i]['msg_poster']=$myrow['msg_poster'];
		 $ListArray[$i]['msg_url']=$myrow['msg_url'];
		 $ListArray[$i]['thumbnail_path']=$myrow['thumbnail_path'];
		 $ListArray[$i]['hits_num']=$myrow['hits_num'];
		 $ListArray[$i]['msg_starttime']=$myrow['msg_starttime'];
		 $ListArray[$i]['msg_endtime']=$myrow['msg_endtime'];
		 $ListArray[$i]['fav_num']=$myrow['fav_num'];
		 $ListArray[$i]['msg_isad']=$myrow['msg_isad'];
		 $ListArray[$i]['msg_tuijian']=$myrow['msg_tuijian'];
		 $ListArray[$i]['msg_createtime']=dateFormat($myrow['msg_createtime'],'Y-m-d');
		 $i++;
	 }
}else{
     $str = "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 m.msg_tuijian=1 and m.msg_status=1 and m.msg_starttime<='".$getnow."' and m.msg_endtime>='".$getnow."' 
		GROUP BY cm.msg_id order by m.msg_id desc,pic.attach_id asc limit 4"; 	   
     $tj_arr = $db->get_all($str);
     
}

$map_nav="搜索到 <font color='red'>".$numrows."</font> 条跟".$kw."相关的内容";

$category_arr = get_category($category_id);
$list_title   = $kw.$category_arr['category_name']; //"网站源码_网站模板_网页素材";

$list_keywords=$kw.','.$category_arr['category_name'];
$list_desc="飞天素材网搜索到".$numrows."条跟".$kw."相关的内容,海量资源供您下载使用。";

$pager=$p->output(1);
include($template->getfile('search'));
?>