<?php

    session_start();
    header("Cache-Control: no-cache, must-revalidate");

    include_once(__DIR__ . "/configs.php");
    $link = mysqli_connect(HOST, DB_USER, DB_PWD, DB);
    $configs = getConfigs();

    //取得配置 
    function getConfigs()
    {
        global $link;
        $sql = "SELECT * FROM  configs  WHERE 1 ";
        $result = mysqli_query($link, $sql);
        while ($r = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            $data[$r['key_name']] = $r['value'];
        }
        return $data;
    }

    //取得推荐位
    function get_recommend()
    {
        global $link;

        $sql = "SELECT * FROM websites WHERE url_type =1  and  upload_img !=''   ORDER BY  sorts ASC ";
        $result = mysqli_query($link, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            $data[$row['id']] = $row;
        }
        return $data;
    }

    //取得分类
    function getCates()
    {
        global $link;
        $sql = "SELECT * FROM  cates WHERE 1   order by sorts asc ";
        $result = mysqli_query($link, $sql);
        while ($r = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            $cates[$r['cate_id']] = $r;
        }
        return $cates;
    }

    // 取得显示 的数据内容 
    function shows($url_type = '')
    {
        global $link;

        $sql = "SELECT * FROM websites WHERE 1 ";
        if ($url_type != '') {
            $sql .=" and  url_type ='" . $url_type . "' ";
        }
          $sql .="  ORDER BY  sorts ASC ";
        $result = mysqli_query($link, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            if ($url_type != '') {
                $data[$row['cate_id']] = $row;
            } else {
                $data[] = $row;
            }
        }
        return $data;
    }

    //取得留言列表
    function getMsgBook()
    {
        global $link;
        $sql = "select * from guestbook where 1    order by id  DESC ";
        $result = mysqli_query($link, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            $data[] = $row;
        }
        return $data;
    }
   //取得留言总数
    function getMsgBookNumber()
    {
        global $link;
        $sql = "select count(*) as counts  from guestbook where 1    ";
        $result = mysqli_query($link, $sql);
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $row['counts'];
    }

    function shows_all()
    {
          global $link;

        $sql = "SELECT * FROM websites WHERE 1 and  recommend ='0' ";

        $sql .="  ORDER BY  sorts ASC ";
        $result = mysqli_query($link, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
           $data[$row['cate_id']][$row['id']] = $row;
        }
        return $data;
    }

    //取得网址总数
    function getUrlNumber()
    {
        global $link;
        $sql = "select count(*) as counts  from websites where 1    ";
        $result = mysqli_query($link, $sql);
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $row['counts'];
    }

?>