안효원 안효원 2022-02-11
address complete
@52da4d15638ebd7c02d27e3f2c8f2eb3618970dd
20220209/Address.php
--- 20220209/Address.php
+++ 20220209/Address.php
@@ -32,9 +32,9 @@
         }
     }
 
-    public function getListPage($limitStart, $onePage){
+    public function getListPage($limitStart, $onePage, $searchSql){
         try {
-            $sql = "select * from address limit " . $limitStart . ", " . $onePage;
+            $sql = "select * from address" . $searchSql . "limit " . $limitStart . ", " . $onePage;
             $stmt = $this->db->prepare($sql);
             $stmt->execute();
             $result = $stmt->fetchAll();
@@ -77,9 +77,9 @@
         }
     }
 
-    public function getCountList(){
+    public function getCountList($searchSql){
         try {
-            $sql = "select count(*) as count from address";
+            $sql = "select count(*) as count from address " . $searchSql;
             $stmt = $this->db->prepare($sql);
             $stmt->execute();
             $result = $stmt->fetch();
20220209/address_view.php
--- 20220209/address_view.php
+++ 20220209/address_view.php
@@ -3,13 +3,13 @@
  * @var $paging
  * @var $limitStart
  * @var $onePage
+ * @var $searchSql
  */
     require_once("page.php");
     require_once("Address.php");
     $address = new Address();
-    $list = $address->getListPage($limitStart, $onePage);
 
-    $addressCount = $address->getCountList();
+    $list = $address->getListPage($limitStart, $onePage, $searchSql);
 ?>
 <!doctype html>
 <html lang="ko">
@@ -81,6 +81,20 @@
         <?php }?>
     </table>
     <?php echo $paging; ?>
+    <div class="searchBar"><br>
+        <form action="address_view.php" method="get">
+            <label for="searchKey">
+                <select name="searchKey">
+                    <option value="">전체</option>
+                    <option value="name">이름</option>
+                </select>
+            </label>
+            <label for="searchWord">
+                <input type="text" name="searchWord">
+            </label>
+            <button type="submit">검색</button>
+        </form>
+    </div>
 </body>
 </html>
 
20220209/page.php
--- 20220209/page.php
+++ 20220209/page.php
@@ -10,8 +10,13 @@
         $page = 1;
     }
 
+    $searchKey = isset($_GET['searchKey']) ? $_GET['searchKey'] : "";
+    $searchWord = isset($_GET['searchWord']) ? $_GET['searchWord'] : "";
+    $searchSql = ($searchKey != "") ? " where " . $searchKey . " LIKE '%" . $searchWord . "%' " : " ";
+
+
     //전체 게시글 카운팅
-    $row = $address->getCountList();
+    $row = $address->getCountList($searchSql);
     $allPost = $row['count'];
 
     //한 페이지에 보여줄 게시글 수
@@ -24,31 +29,38 @@
     //한 블록에 보여줄 페이지
     $oneBlock = 5;
 
-    //블록의 첫 페이지
-    $firstPage = $page - floor($oneBlock / 2);
-    if($page == 1 || $page == 2){
-        $firstPage = 1;
+
+    if($allPage > $oneBlock){
+        $firstPage = $page - floor($oneBlock / 2);
+        if($page == 1 || $page == 2){
+            $firstPage = 1;
+        }
+
+        //블록의 마지막 페이지
+        $lastPage = $page + floor($oneBlock / 2);
+        if($page == $allPage || $page == ($allPage-1)){
+            $lastPage = $allPage;
+        }
     }
 
-    //블록의 마지막 페이지
-    $lastPage = $page + floor($oneBlock / 2);
-    if($page == $allPage || $page == ($allPage-1)){
-        $lastPage = $allPage;
-    }
+    //블록의 첫 페이지
+
+
+
 
     //page variable
     $paging = "<ul>";
     
     //처음페이지로 이동
     if($page > 1){
-        $paging .= "<li><a href='address_view.php?page=" . (1) . "'>처음</a></li>";
+        $paging .= "<li><a href='address_view.php?page=" . (1) . "&searchKey=" . $searchKey . "&searchWord=" . $searchWord . "'>처음</a></li>";
     }
     
     //이전페이지로 이동
     $pre = $page - 1;
     //이전 페이지 예외처리
     if($page > 1){
-        $paging .= "<li><a href='address_view.php?page=" . $pre . "'>이전</a></li>";
+        $paging .= "<li><a href='address_view.php?page=" . $pre . "&searchKey=" . $searchKey . "&searchWord=" . $searchWord . "'>이전</a></li>";
     }
         
     for($i = $firstPage; $i<=$lastPage; $i++){
@@ -57,22 +69,20 @@
         }else{
             $back = "none";
         }
-        $paging .= "<li style='background-color: " . $back . "' ><a href='address_view.php?page=" . $i . "'>" . $i . "</a></li>";
+        $paging .= "<li style='background-color: " . $back . "' ><a href='address_view.php?page=" . $i . "&searchKey=" . $searchKey . "&searchWord=" . $searchWord . "'>" . $i . "</a></li>";
     }
 
     //다음페이지로 이동
     $next = $page + 1;
     //페이지 예외처리
     if($page < $allPage){
-        $paging .= "<li><a href='address_view.php?page=" . $next . "'>다음</a></li>";
+        $paging .= "<li><a href='address_view.php?page=" . $next . "&searchKey=" . $searchKey . "&searchWord=" . $searchWord . "'>다음</a></li>";
     }
 
     //처음페이지로 이동
     if($page < $allPage){
         $paging .= "<li><a href='address_view.php?page=" . $allPage . "'>마지막</a></li>";
     }
-
-
     $paging .= "</ul>";
 
     //limit 세팅
Add a comment
List