php paginator class


<?
include_once($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/urlrewrite.php');
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/header.php");

$APPLICATION->SetPageProperty("NOT_SHOW_NAV_CHAIN", "Y");
$APPLICATION->AddChainItem("Обратная связь", "/feedback.php");

require __DIR__ ."/FeedBackApi.php";
$paginator = new FeedBackApi($_GET);


?>

    <style>
        .mypagination {
            display: inline-block;
            color:#0a246a;
        }
        .mypagination a {
            color: black;
            float: left;
            padding: 8px 16px;
            text-decoration: none;
            border-top: 2px solid grey;
        }
        .mypagination a.active {
            color: red;
            border-top: 2px solid red;

        }

        .mypagination a:hover:not(.active) {
            background-color: #ddd;
        }
        .mypagination a.next {
            position:relative;
            width: 150px;
            padding-right:20px;
        }
        .mypagination .rigth-arrow {
            top: -16px;
            font-size: 42px;
            position: absolute;
        }
        .mypagination a.prev {
            position:relative;
            width: 150px;
            padding-left: 40px;

        }
        .mypagination .left-arrow {
            font-size: 42px;
            position: absolute;
            right: 14px;
            left: 0px;
            top: -16px;
        }

    </style>
    <script>
        function pageChange(page) {
            $("[name=page]").val(page);
            $("#sendForm").submit();
        }
    </script>
    <form  id="sendForm">
        <input type="hidden" name="page" value="<?=$_GET['page']?>">
        <?=$paginator->getPaginator(225);?>
        <?=$paginator->inputText("id","id" , "id", "=");?>
        <?$a = $paginator->getRes(); ?>
        <input type="submit">
    </form>
<?
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/footer.php"); ?>

 

<?php
const DB_HOST = "localhost";
const DB_NAME = "bitrix";
const DB_USER = "root";
const DB_PASS = "1";
const DB_TABLE = "b_file";
const PAGINATOR_ITEMS_ON_PAGE = 10;


class FeedBackApi
{



    private $get;
    private $page;
    private $like;

    public function __construct($get)
    {
        $this->get=$get;
        $this->setPage();
        $this->like = "";
    }


    function  printPageByNumber ($num , $count)
    {
        if ($num==$count) $active= "class='active'"; else $active="";
//        if ($count!=0)
        return "\n<a onclick='pageChange($count)' $active >$count</a>";
    }

    function getPaginator($countRes)
    {
        $strRes = ' <div class="mypagination">';
        if ( $this->page > 1){
            $strRes .= '<a onclick="pageChange(1)" > Первая </a>' ;
            $strRes .= '<a onclick="pageChange('. ($this->page-1) .
                ')" class="prev" > Предыдущая <span class="left-arrow">&#8592;</span> </a>' ;
        }
        $limit = ceil($countRes / PAGINATOR_ITEMS_ON_PAGE )  ;

        if ($countRes < 10 * PAGINATOR_ITEMS_ON_PAGE  ){
            $strRes .= $this->printPageByNumber($this->page , 1 ) ;
            for ($i=2 ; $i<$limit ; $i++){
                $strRes .= $this->printPageByNumber($this->page , $i ) ;
            }
            if ($limit!="1" && $limit!="0" )
                $strRes .= $this->printPageByNumber($this->page , $limit ) ;
        }
        else {
            if ($this->page < 5) {
                $strRes .= $this->printPageByNumber($this->page , 1 ) ;
                for ($i=2 ; $i<9 ; $i++){
                    $strRes .= $this->printPageByNumber($this->page , $i ) ;
                }
                $strRes .= $this->printPageByNumber("-" , ".." ) ;
            } else {
                $strRes .= $this->printPageByNumber("-" , ".." ) ;
                for ($i=$this->page-3 ; $i<$this->page ; $i++){
                    $strRes .= $this->printPageByNumber($this->page , $i ) ;
                }
                $strRes .= $this->printPageByNumber($this->page , $this->page ) ;
                for ($i=$this->page+1 ; $i< $this->page+4 ; $i++){
                    if ($i <= $limit)
                        $strRes .= $this->printPageByNumber($this->page , $i ) ;
                }
                if ($this->page+4 <= $limit)
                    $strRes .= $this->printPageByNumber("-" , ".." ) ;
            }
        }

        if ($this->page!=$limit){
            $strRes .=  '<a onclick="pageChange( ' . ($this->page+1) .
                ')" class="next">Следующая<span class="rigth-arrow">&#8594;</span></a>' ;
            $strRes .=  '<a onclick="pageChange( ' .$limit. ')">Последняя ('.$limit.')</a>' ;
        }
        $strRes .=  '</div> ' ;

        return $strRes ;
    }

    function setVal($name , $defVal)
    {
        if (!empty($this->get[$name]) && isset($this->get[$name])  ){
            return $this->get[$name];
        } else {
            return $this->page = $defVal;
        }
    }

    public function setPage()
    {
        $val = $this->setVal("page","1");
        if ($val>0)
            $this->page = $val;
        else $this->page = "1";
    }

    function getRes()
    {
        $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if (!$conn) die( mysqli_connect_error());
        if (!$conn->set_charset("utf8")) die("utf8");
        $sql = "SELECT *  " .
                    "FROM ".DB_TABLE." h " .
                    "WHERE h.id > 0 ". $this->like . "  " .
                    "order by id  desc ".
                    "LIMIT ". ($this->page-1)*PAGINATOR_ITEMS_ON_PAGE ."   , ". PAGINATOR_ITEMS_ON_PAGE . " "
        ;
        $res = $conn->query($sql);
        $res = mysqli_fetch_all($res , MYSQLI_ASSOC);
        return $res;
    }


    function inputText($label, $name , $dbname , $operand)
    {
        $par = $this->get[$name];
        if ($par != null && $par!="" ){
            $this->like .=(" AND $dbname  $operand '$par' ");
        } else $par = "";
        $str="<div class='form-group '>\n" .
            "    <label for='".$name."' class='control-label'>".$label."</label>\n" .
            "    <div>\n" .
            "        <input class='form-control ' id='".$name."' name='".$name."' value='".$par."' type='text'>\n" .
            "    </div>\n" .
            "</div>\n" ;
         return  $str;
    }



}