<?php

/**
 * 前台 热门搜索 模块 Model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: searchlist.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://un.zhuna.cn；
 */
class searchlist extends Model
{

    private $db;
    private $search_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->search_table = DB_PREFIX . 'search';
    }

    public function getSearchlist($cityid = 0, $limit = 0)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->search_table");
        $url = PHP_SELF . '?m=searchlist&page=';

        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(100);
        $limit_page = $pages->getSqlLimit();

        $sql = "SELECT s_title, s_key, s_hn, s_cityid, s_count FROM $this->search_table ";
        if ($cityid <> 0)
            $sql .= "WHERE s_cityid = $cityid ";
        $sql .= "ORDER BY s_count DESC, s_updatetime DESC";
        if ($limit <> 0) {
            $sql .= " LIMIT $limit";
        } else {
            $sql .= " LIMIT $limit_page";
        }
        $rs = $this->db->getAll($sql);
        foreach ($rs AS $k => $v) {
            $cityname = $this->M('common')->getCityinfo($v['s_cityid']);
            if ($v['s_hn'] <> '') {
                $rs[$k]['title'] = $cityname . $v['s_hn'] . '酒店';
                $rs[$k]['s_hn_urlencode'] = urlencode($v['s_hn']);
                $rs[$k]['s_key_urlencode'] = '';
            } elseif ($v['s_key'] <> '') {
                $rs[$k]['title'] = $cityname . $v['s_key'] . '附近酒店';
                $rs[$k]['s_hn_urlencode'] = '';
                $rs[$k]['s_key_urlencode'] = urlencode($v['s_key']);
            }
        }

        if ($count == 0) {
            $ErrorMsg = "暂无热门搜索记录!";
        } else {
            $ErrorMsg = '';
        }

        $result['rs'] = $rs;
        $result['ErrorMsg'] = $ErrorMsg;
        $result['page'] = $pages->show();
        return $result;
    }

}