多数据库查询及数据的分页

//需求 根据不同的时间 连接到不同的数据库
//使用第三方 http://medoo.lvtao.net/ 轻量级的PHP数据库框架, 提高开发效率

参考

function cn_db($StartMonth,$EndMonth)
    {
      $ToStartMonth = strtotime($StartMonth); //转换一下
      $ToEndMonth = strtotime($EndMonth); //一样转换一下
      $i = false; //开始标示
      while ($ToStartMonth < $ToEndMonth) {
        $NewMonth = !$i ? date('Y-m', strtotime('+0 Month', $ToStartMonth)) : date('Y-m', strtotime('+1 Month', $ToStartMonth));
        $ToStartMonth = strtotime($NewMonth);
        $i = true;
        echo $NewMonth . '
'
; } } cn_db('2017-05','2017-12');

control

//
function.php


//计算需要连接的 数据库
function isSmallTodays($ToStartMonth,$ToEndMonth)
{
    $moreConn = [];
    $i = false; //开始标示
    while( $ToStartMonth < $ToEndMonth ) {
        $NewMonth = !$i ? date('Ym', strtotime('+0 Month', $ToStartMonth)) : date('Ym', strtotime(' +1 Month', $ToStartMonth));
        $ToStartMonth = strtotime( $NewMonth );
        $i = true;

        $database = new \Medoo\Medoo([
            // 必须配置项
            'database_type' => 'mysql',
            'database_name' => DB_SERVER_LOG."$NewMonth",
            'server' => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',

            // 可选参数
            'port' => 3306,

            // 可选,定义表的前缀
            'prefix' => 'fa_',

            // 连接参数扩展, 更多参考 http://www.php.net/manual/en/pdo.setattribute.php
            'option' => [
                PDO::ATTR_CASE => PDO::CASE_NATURAL
            ]
        ]);
        array_push($moreConn,$database);
    }

    return $moreConn;
}


//单数据库
function connectDB($start_time){
    $conns = new \Medoo\Medoo([
        // 必须配置项
        'database_type' => 'mysql',
        'database_name' => DB_SERVER_LOG.$start_time,
        'server' => "localhost",
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8',

        // 可选参数
        'port' => 3306,

        // 可选,定义表的前缀
        'prefix' => 'fa_',

        // 连接参数扩展, 更多参考 http://www.php.net/manual/en/pdo.setattribute.php
        'option' => [
            PDO::ATTR_CASE => PDO::CASE_NATURAL
        ]
    ]);

    return $conns;
}




function mergeData($result)
{
        $datas = [];
        $len = count($result);
        if ($result) {
            if ($len > 0) {
                for ($i = 0; $i <= $len - 1; $i++) {
                    foreach ($result[$i] as $value){
                        array_push($datas, $value);
                    };
                }
            }
        }


    return $datas;

}





//copy 分页


/**
 * 数组分页函数  核心函数  array_slice
 * 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中
 * $count   每页多少条数据
 * $page   当前第几页
 * $array   查询出来的所有数组
 * order 0 - 不变     1- 反序'
 */

function page_array($count,$page,$array,$order){
    $page=(empty($page))? '1':$page; #判断当前页面是否为空 如果为空就表示为第一页面
    $start=($page-1)*$count; #计算每次分页的开始位置
    if($order==1){
        $array=array_reverse($array);
    }
    $totals=count($array);
    $countpage= intval(ceil($totals/$count)); #计算总页面数

    $pagedata=array_slice($array,$start,$count);

    $pages = array('pagedata'=>$pagedata,'countpage'=>$countpage);
    return $pages;  #返回查询数据
}

/**
 * 分页及显示函数
 * $countpage 全局变量,照写
 * $url 当前url
 */

function show_array($countpage,$url){
    $page=empty($_GET['page'])?1:$_GET['page'];
     if($page > 1){
         $uppage=$page-1;

     }else{
         $uppage=1;
     }

     if($page < $countpage){
         $nextpage=$page+1;

     }else{
         $nextpage=$countpage;
     }

        $str='<div style="border:1px; width:300px; height:30px; color:#9999CC">';
    $str.="<span>共  {$countpage}  页 / 第 {$page}span>";
// $str.="<span><a href='{$url}?page={$uppage}'> 上一页  a>span>";

    $str.="<span><a href='{$url}page={$uppage}'> 上一页  a>span>";
    $str.="<span><a href='{$url}page={$nextpage}'>下一页  a>span>";
    $str.="<span><a href='{$url}page={$countpage}'>尾页  a>span>";
    $str.='div>';
    return $str;
}




control

 public function indexOp()
    {

        $condition = array();

        if ($_GET['role_id']) {
            $condition['role_id'] = $_GET['role_id'];
        }
        if ($_GET['role_name']) {
            $condition['role_name'] = $_GET['role_name'];
        }

        if ($_GET['online_time']) {
            $condition['online_time'] = $_GET['online_time'];
        }


        $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_start_time']);
        $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_end_time']);

        $start_unixtime = $if_start_time ? ($_GET['query_start_time']) : null;
        $end_unixtime = $if_end_time ? ($_GET['query_end_time']) : null;

        if ($start_unixtime || $end_unixtime) {
            $condition['login_time'] = array('time', array($start_unixtime, $end_unixtime));
        }
        $searchtime = $condition['login_time'][1];


        if ($searchtime) {
            $StartMonth = $searchtime[0]; //开始日期
            $EndMonth = $searchtime[1]; //结束日期

            $start_time = explode('-', $searchtime[0]);
            $end_time = explode('-', $searchtime[1]);


            //多数据库 连接
            if (strtotime($EndMonth) > strtotime($StartMonth)) {

                $ToStartMonth = strtotime($StartMonth); //转换一下
                $ToEndMonth = strtotime("$EndMonth -1 month"); //一样转换一下

                $newEndTime =  date("Y-m-d",$ToEndMonth);
                $newStartTime =  date("Y-m-d",$ToStartMonth);

                //时间小于 一个月
                if($newEndTime[1] == $newStartTime[1] && $newEndTime[2] <= $newStartTime[2]){
                    $conn = [];
                    $conn1 =  connectDB($start_time[0].$start_time[1]);
                    array_push($conn,$conn1);
                    $conn2 =  connectDB($end_time[0].$end_time[1]);
                     array_push($conn,$conn2);
                }else{
                    $conn = isSmallTodays($ToStartMonth, $ToEndMonth);
                }

            }

            //单一数据库connect and search
            if ($end_time[1] == $start_time[1] && $start_time[0] == $end_time[0]) {
                $ToStartMonth = date('Ym',strtotime($StartMonth));
                $conns =  connectDB($ToStartMonth);
                 unset($condition['login_time']);

                 if($condition['online_time'] > 0){
                     $condi = array("login_time[<>]" => [strtotime($StartMonth), strtotime($EndMonth)],"online_time[>=]" =>$condition['online_time']);

                 }else {
                     $condi = array("login_time[<>]" => [strtotime($StartMonth), strtotime($EndMonth)]);
                 }

                unset($condition['online_time']);
                $condi =  array_merge($condi,$condition);
                $datas = $conns->select('player_login_log',"*",$condi);

            }
        }

        //多数据查询
        if($conn){
            $result = $this->searchFilters($conn, $condition);
        }

        echo 'pre>';
        //合并数据
        if($result){
            $datas = mergeData($result);
        }

        //导出表
        if($_GET['export']){
            $pagesize = 100000;
             $this->export($datas,$_GET['export']);
        }

        if($_GET['page']){
            $page = $_GET['page'];
        }
        $pagesize = 20;


        Tpl::output('datas', $datas);
        Tpl::output('pagesize', $pagesize);
        Tpl::showpage('log/player_login_log.index');
    }



    public function searchFilters($conn,$filters)
    {
        $start_time = strtotime($filters['login_time'][1][0]);
        $end_time = strtotime($filters['login_time'][1][1]);

        unset($filters['login_time']);

        if($filters['online_time'] > 0){
            $condi = array("login_time[<>]" => [($start_time), ($end_time)],"online_time[>=]" =>$filters['online_time']);

        }else {
            $condi = array("login_time[<>]" => [($start_time), ($end_time)]);
        }
        unset($filters['online_time']);

        $condi =  array_merge($condi,$filters);

        $conns = count($conn);

            $datas = [];
            for($i = 0;$i <= $conns-1;$i++){

                $lists = $conn[$i]->select('player_login_log',"*",$condi);
                if($lists){
                    array_push($datas,$lists);
                }
            }

        echo 'pre>';
        return $datas;
    }


//html

 $pages =  page_array($output['pagesize'],$_GET['page'],$datas,0);

  if($pages['countpage'] > 0){
                    $str = '';
                    foreach ($_REQUEST as $key =>$value){
                    $str .= $key."=".$value.'&';
                    }
                    $str = '?'.$str;

                    $newurl = substr($str,1,strrpos($str,'page'));
                    $showpage = show_array($pages['countpage'],$str);
                    echo  $showpage;

//跳转
 <div style="text-align:right;">
     <label class="submit-border">
        第  <input style="width: 50px" type="number" name="page"  value=" $_GET['page'] ?>" />  页
       <input type="submit" name="page_up" class="submit" value="跳转" />
                        label>
                    div>

内的 遍历时间及分页方法 来自别处

你可能感兴趣的:(多数据库查询及数据的分页)