结合两个班级

时间:2017-04-18 18:10:41

标签: php twitter-bootstrap

我有2个班级:

带有一些函数的

class.user.php:

    <?php

require_once 'dbconfig.php';

date_default_timezone_set('Europe/Brussels');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
        }

带有函数的class.paginator.php:

<?php

date_default_timezone_set('Europe/Brussels');

class Paginator {
    private $_conn;
        private $_limit;
        private $_page;
        private $_query;
        private $_total;

    public function __construct( $conn, $query ) {

       $this->_conn = $conn;
    $this->_query = $query;

    $rs= $this->_conn->query( $this->_query );
    $this->_total = $rs->num_rows;
        }

    public function getData( $limit = 10, $page = 1 ) {

        $this->_limit   = $page;
        $this->_page    = $limit;

        if ( $this->_limit == 'all' ) {
            $query      = $this->_query;
        } else {
            $query      = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
        }
        $rs             = $this->_conn->query( $query );

        while ( $row = $rs->fetch_assoc() ) {
            $results[]  = $row;
        }

        $result         = new stdClass();
        $result->page   = $this->_page;
        $result->limit  = $this->_limit;
        $result->total  = $this->_total;
        $result->data   = $results;

        return $result;
    }

    public function createLinks( $links, $list_class ) {
        if ( $this->_limit == 'all' ) {
            return '';
        }

        $last       = ceil( $this->_total / $this->_limit );

        $start      = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
        $end        = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;

        $html       = '<ul class="' . $list_class . ' custom-pagination">'
                . '<select id="itemspage" class="form-control">
            <option value="5">5</option>
            <option value="10" selected>10</option>
            <option value="15">15</option>
            <option value="25">25</option>
            <option value="50">50</option>
        </select>';

        $class      = ( $this->_page == 1 ) ? "hidden" : "";
        $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';

        if ( $start > 1 ) {
            $html   .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';
            $html   .= '<li class="disabled"><span>...</span></li>';
        }

        for ( $i = $start ; $i <= $end; $i++ ) {
            $class  = ( $this->_page == $i ) ? "active" : "";
            $html   .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
        }

        if ( $end < $last ) {
            $html   .= '<li class="disabled"><span>...</span></li>';
            $html   .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
        }

        $class      = ( $this->_page == $last ) ? "hidden" : "";
        $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';

        $html       .= '</ul>';

        return $html;
    }
}

对于分页,我使用以下代码:

require_once 'class.paginator.php';

    $conn       = new mysqli( 'localhost', 'user', 'password', 'database' );

    $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 10;
    $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
    $query      = "SELECT * FROM sbs INNER JOIN city ON sbs.sbs_city=city.city_id WHERE sbs_subcat='afhaal' AND sbs_status=1 AND sbs_country=$country";

    $Paginator  = new Paginator( $conn, $query );

    $results    = $Paginator->getData( $page, $limit );

结果显示良好

链接是这个代码:

<?php echo $Paginator->createLinks( $links, 'pagination pagination' ); ?></center>

我想结合这两个类,所以我只需要使用一个类(class.user.php)

dbconfig.php就像:

private $host = "localhost";
    private $db_name = "database";
    private $username = "username";
    private $password = "password";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8", $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }

我只是在使用__construct()...将函数放在一起不是问题。

先谢谢你的帮助!

0 个答案:

没有答案