안효원 안효원 2022-02-10
paging function add
@bb12f064a1bd0cc0fc92a3bfdc2c4ec2474c636a
20220209/Address.php
--- 20220209/Address.php
+++ 20220209/Address.php
@@ -23,12 +23,24 @@
 
     public function getList(){
         try {
-            $stmt = $this->db->prepare("select * from address");
+            $stmt = $this->db->prepare("select * from address LIMIT 0, 5");
             $stmt->execute();
             $result = $stmt->fetchAll();
             return $result;
         }catch (Exception $e){
             echo $e;
+        }
+    }
+
+    public function getListPage($limitStart, $onePage){
+        try {
+            $sql = "select * from address limit " . $limitStart . ", " . $onePage;
+            $stmt = $this->db->prepare($sql);
+            $stmt->execute();
+            $result = $stmt->fetchAll();
+            return $result;
+        }catch (Exception $e){
+            return $e;
         }
     }
 
@@ -65,6 +77,19 @@
         }
     }
 
+    public function getCountList(){
+        try {
+            $sql = "select count(*) as count from address";
+            $stmt = $this->db->prepare($sql);
+            $stmt->execute();
+            $result = $stmt->fetch();
+            return $result;
+        }catch(Exception $e){
+            return $e;
+        }
+
+    }
+
 
 
 }
20220209/address_view.php
--- 20220209/address_view.php
+++ 20220209/address_view.php
@@ -1,7 +1,15 @@
 <?php
+/**
+ * @var $paging
+ * @var $limitStart
+ * @var $onePage
+ */
+    require_once("page.php");
     require_once("Address.php");
     $address = new Address();
-    $list = $address->getList();
+    $list = $address->getListPage($limitStart, $onePage);
+
+    $addressCount = $address->getCountList();
 ?>
 <!doctype html>
 <html lang="ko">
@@ -14,8 +22,11 @@
     <style>
         * {margin: 0 auto; padding: 0; text-align: center;}
         td, th {padding: 10px;}
-        a {text-decoration: none; color: red;}
-        a:hover {background-color: tomato; color: black;}
+        a {text-decoration: none;  width: 100%; height: 100%; color: black;}
+        a:hover {background-color: black; color: white;}
+        li {display: inline-block; border: 1px solid black; padding: 5px;}
+        li:hover{background-color: black; color: white;}
+        ul {margin-top: 30px;}
     </style>
 </head>
 <body>
@@ -69,6 +80,7 @@
             </tr>
         <?php }?>
     </table>
+    <?php echo $paging; ?>
 </body>
 </html>
 
 
20220209/page.php (added)
+++ 20220209/page.php
@@ -0,0 +1,86 @@
+<?php
+
+    require_once("Address.php");
+    $address = new Address();
+
+    //페이지 get 변수가 있다면 받아오고 없으면 1페이지을 보여줌
+    if(isset($_GET['page'])){
+        $page = $_GET['page'];
+    }else{
+        $page = 1;
+    }
+
+
+
+
+    //전체 게시글 카운팅
+    $row = $address->getCountList();
+    $allPost = $row['count'];
+
+    //한 페이지에 보여줄 게시글 수
+    $onePage = 5;
+
+    //전체 페이지 수
+    $allPage = ceil($allPost/$onePage);
+
+    //page variable
+    $paging = "<ul>";
+    
+    //처음페이지로 이동
+    if($page > 1){
+        $paging .= "<li><a href='address_view.php?page=" . (1) . "'>처음</a></li>";
+    }
+    
+    //이전페이지로 이동
+    $pre = $page - 1;
+    //이전 페이지 예외처리
+    if($page > 1){
+        $paging .= "<li><a href='address_view.php?page=" . $pre . "'>이전</a></li>";
+    }
+        
+    for($i = 1; $i<=$allPage; $i++){
+        if($page == $i){
+            $back = "tomato";
+        }else{
+            $back = "none";
+        }
+        $paging .= "<li style='background-color: " . $back . "' ><a href='address_view.php?page=" . $i . "'>" . $i . "</a></li>";
+    }
+
+    //다음페이지로 이동
+    $next = $page + 1;
+    //페이지 예외처리
+    if($page < $allPage){
+        $paging .= "<li><a href='address_view.php?page=" . $next . "'>다음</a></li>";
+    }
+
+    //처음페이지로 이동
+    if($page < $allPage){
+        $paging .= "<li><a href='address_view.php?page=" . $allPage . "'>마지막</a></li>";
+    }
+
+
+    $paging .= "</ul>";
+
+    //limit 세팅
+    $limitStart = ($onePage * $page) - $onePage;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Add a comment
List