Help us understand the problem. What is going on with this article?

雑なページネーションを作りましたよ。

今回、雑なページネーションを作った経緯は自分で使用するために作ったものを
お裾分けとして公開です。因みにコピペしてご自由直して頂いければと思います。

動作環境PHP7.3です😅
動作させた動画がこちら。
尚、データは私のところに来た質問箱のデータですw。
2020-08-20_07-35-24.gif

index.php
<?php
include_once "./lib/pagination.php";
$db = new db();
$db->count_number();
$db->pagination();
$html1 = $db->html($db->select_set());
$html2 = $db->count_display();
$db->colosedb();

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="Description" content="Enter your description here"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
<link rel="stylesheet" href="assets/css/style.css">
<title>pagination</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col">
        <?=$html1?> 
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col">
        <?=$html2?>
        </div>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
lib/pagination.php
<?php
include_once "config.php";
date_default_timezone_set("Asia/Tokyo");
class db
{
    public $pdb = null;
    public $Page_mode = null;
    public $Displayed_results = 10;//表示するデータ数
    public $Order_by_mode = " asc ";
    public $Order_by_key = " id ";
    public $sql_all = "select * from test";
    public $sql_table_name = "test";
    public $sql_set = "";
    public $cnt_max = 1;
    public $page_max = 1;
    public $page_active = 1;
    public $Equality = 3;//ページネーションのあれ。間隔

    public function __construct()
    {
        $dsn = "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST . ";charset=utf8;";
        try {
            $this->pdb = new PDO($dsn, DB_USER, DB_PASSWORD);
        } catch (PDOException $e) {
            //print('Error:'.$e->getMessage());
        }
    }
    public function count_number()
    {
        try {
            $statement = $this->pdb->prepare($this->sql_all);
            $statement->execute();
            $result = $statement->fetchAll();
            $statement = null;

            if (empty($result)) {
                $this->cnt_max = 1;
                $this->page_min = 1;
            } else {
                $this->cnt_max = count($result);
                $this->page_max = $this->cnt_max % $this->Displayed_results === 0?((int)($this->cnt_max / $this->Displayed_results)):((int)($this->cnt_max / $this->Displayed_results) + 1);
            }
            return $result;
        } catch (PDOException $e) {
            //print('Error:'.$e->getMessage());
            $this->closedb();
        }
    }

    public function pagination()
    {
        $variable = $_GET["pagemode"];

        switch ($variable) {
            case 'FIRST':
                $this->Page_mode = 1;
                break;
            case 'END':
                $this->Page_mode = $this->page_max;
                break;
            case 'BACKPAGE':
                if ($variable && (int)$_GET["pageactive"] && ((int)$_GET["pageactive"] > 1 && (int)$_GET["pageactive"] <= $this->page_max)) {
                    $this->Page_mode = (int)$_GET["pageactive"];
                } else {
                    $this->Page_mode = 1;
                }
                break;
            case 'NEXTPAGE':
                if ($variable && (int)$_GET["pageactive"] && ((int)$_GET["pageactive"] > 1 && (int)$_GET["pageactive"] <= $this->page_max)) {
                    $this->Page_mode = (int)$_GET["pageactive"];
                } else {
                    $this->Page_mode = $this->page_max;
                }
                break;
            default:
                if ($variable && (int)$_GET["pageactive"] && ((int)$_GET["pageactive"] > 1 && (int)$_GET["pageactive"] <= $this->page_max)) {
                    $this->Page_mode = (int)$_GET["pageactive"];
                } else {
                    $this->Page_mode = 1;
                }
                break;
        }

        $this->page_active = $this->Page_mode;

        $start = ($this->Page_mode - 1) * $this->Displayed_results;
        $this->sql_set = "select * from " . $this->sql_table_name . " order by " . $this->Order_by_key . " " . $this->Order_by_mode . "limit $start," . $this->Displayed_results . ";";
    }

    public function select_set()
    {
        try {

            $statement = $this->pdb->prepare($this->sql_set);
            $statement->execute();
            $result = $statement->fetchAll();
            $statement = null;
            return $result;
        } catch (PDOException $e) {
            //print('Error:'.$e->getMessage());
            $this->closedb();
        }
    }

    public function html($variable){
        $str = "";
        if(!empty($variable)){
            foreach ($variable as $key => $value) {
                $str.="<p>".$value["id"].":".$value["txts"]."</p>";
            }
        }
        return $str;
    }

    public function count_display()
    {

        $variable = $_GET["pagemode"];
        switch ($variable) {
            case 'FIRST':
                $FIRST = '
                <li class="page-item active">
                    <span class="page-link">FIRST</span>
                </li>
                ';
                break;

            default:
                $FIRST = '
            <li class="page-item">
                <a class="page-link" href="?pageactive=' . $this->page_active . '&pagemode=FIRST" aria-label="FIRST">
                    FIRST
                </a>
            </li>
            ';
                break;
        }
        switch ($variable) {
            case 'END':
                $END = '
                <li class="page-item active">
                    <span class="page-link">END</span>
                </li>
                ';
                break;

            default:
                $END = '
            <li class="page-item">
                <a class="page-link" href="?pageactive=' . $this->page_active . '&pagemode=END" aria-label="END">
                    END
                </a>
            </li>
            ';
                break;
        }

        $PAGE = "";
        $p_min = ($this->page_active-$this->Equality)<1?1:$this->page_active-$this->Equality;
        $p_max = ($this->page_active+$this->Equality)>$this->page_max?$this->page_max:$this->page_active + $this->Equality;

        if(($this->page_active - $p_min)!==(($this->page_active - $p_max)  * -1)){
            if(($this->page_active - $p_min)>(($this->page_active - $p_max)  * -1)){
                $p_min = $p_min - ($this->Equality + (($this->page_active - $p_max)));
                if($p_min<1){
                    $p_min = 1;
                }
            }else{
                $p_max = $p_max + ($this->Equality + (($this->page_active - $p_min) * -1));
                if($p_max>$this->page_max){
                    $p_max = $this->page_max;
                }
            }
        }

        for ($i = $p_min; $i <= $p_max; $i++) {
            if ($this->page_active === $i) {
                $PAGE.= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
            } else {
                $PAGE.= '<li class="page-item"><a class="page-link" href="?pageactive=' . $i . '&pagemode=' . $i . '">' . $i . '</a></li>';
            }
        }

        $html = '<nav aria-label="Page navigation example">
        <ul class="pagination">
        ' . $FIRST . '
          <li class="page-item">
            <a class="page-link" href="?pageactive=' . ($this->page_active  -1?$this->page_active  -1:1) . '&pagemode=BACKPAGE" aria-label="Previous">
              <span aria-hidden="true">&laquo;</span>
              <span class="sr-only">Previous</span>
            </a>
          </li>
          ' . $PAGE . '
          <li class="page-item">
            <a class="page-link" href="?pageactive=' . ($this->page_active  +1 > $this->page_max?$this->page_max:$this->page_active  +1) . '&pagemode=NEXTPAGE" aria-label="Next">
              <span aria-hidden="true">&raquo;</span>
              <span class="sr-only">Next</span>
            </a>
          </li>
          ' . $END . '
        </ul>
      </nav>';
        return $html;
    }


    public function colosedb()
    {
        return $this->pdb = null;
    }
}

lib/config.php
<?php
/** MySQL データベース名 */
define('DB_NAME', 'dbname');
/** MySQL データベースのユーザー名 */
define('DB_USER', 'id');
/** MySQL データベースのパスワード */
define('DB_PASSWORD', 'pass');
/** MySQL のホスト名 */
define('DB_HOST', 'localhost');
question909
アイディアや個人用として使用しています。
https://taoka-toshiaki.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away