2017-12-14 2 views
0

저는 PHP 학생이며 첫 번째 앱을 개발하고 있습니다. 아래 코드를 사용하여 검색 결과에 페이지 매김을 추가해야합니다. 작업 버튼과 데이터를 테이블에 넣기가 어렵 기 때문에 데이터 테이블이나 다른 플러그인을 사용할 수 없습니다.페이지 넘김 onOn PHP로 검색 결과 만

구현하기가 너무 힘들 수있는 몇 가지 간단한 방법을 알고 있다면 많은 도움이됩니다.

나는이 어플의 개발자의 예제를 사용하고 있습니다 : 예를 아래 how to search and filter with php

if(isset($_POST['search'])) 
{ 
    $valueToSearch = $_POST['valueToSearch']; 
    // search in all table columns 
    // using concat mysql function 
    $query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%'"; 
    $search_result = filterTable($query); 

} 
else { 
    $query = "SELECT * FROM `users`"; 
    $search_result = filterTable($query); 
} 

// function to connect and execute the query 
function filterTable($query) 
{ 
    $connect = mysqli_connect("localhost", "root", "", "test_db"); 
    $filter_Result = mysqli_query($connect, $query); 
    return $filter_Result; 
} 

?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>PHP HTML TABLE DATA SEARCH</title> 
     <style> 
      table,tr,th,td 
      { 
       border: 1px solid black; 
      } 
     </style> 
    </head> 
    <body> 

     <form action="php_html_table_data_filter.php" method="post"> 
      <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br> 
      <input type="submit" name="search" value="Filter"><br><br> 

      <table> 
       <tr> 
        <th>Id</th> 
        <th>First Name</th> 
        <th>Last Name</th> 
        <th>Age</th> 
       </tr> 

     <!-- populate table from mysql database --> 
       <?php while($row = mysqli_fetch_array($search_result)):?> 
       <tr> 
        <td><?php echo $row['id'];?></td> 
        <td><?php echo $row['fname'];?></td> 
        <td><?php echo $row['lname'];?></td> 
        <td><?php echo $row['age'];?></td> 
       </tr> 
       <?php endwhile;?> 
      </table> 
     </form> 

    </body> 
</html> 
+1

요청시 두 개의 매개 변수를 전달하고 'offset' 및'limit'을 전달하고 쿼리 끝에 'limit offset, limit'와 같은 쿼리에서 사용합니다. –

답변

0

이 요구 사항에 우물을 작동합니다 : 당신은 결과를 페이지 단위 limit and offset를 사용해야합니다

mysql_connect("localhost","wellho","wawawawa"); 
mysql_select_db("wellho"); 

$perpage = 10; 
$html = ""; 
$startat = $_REQUEST[page] * $perpage; 
$limlim = "%".$_REQUEST[look4]."%"; 

$q = mysql_query("select count(entry_id) from mt_entry where entry_title like '$limlim'"); 
$row = mysql_fetch_array($q); 
$havesome = $row[0]; 
$pages = floor(($row[0]-1)/$perpage) +1 ; 

$q = mysql_query("select * from mt_entry where entry_title like '$limlim' order by entry_id desc limit $startat,$perpage"); 

while ($row = mysql_fetch_assoc($q)) { 
     $text = strip_tags($row[entry_text]); 
     $text = substr($text,0,300); 
     $html .= "<dt>$row[entry_id] - <a href=/mouth/$row[entry_id]_.html target=pix>$row[entry_title]</a></dt>"; 
     $html .= "<dd>$text ....<br><br></dd>"; 
     }; 

$lynx = "Please choose the next page you want to view:"; 
for ($k=0; $k<$pages; $k++) { 
     if ($k != $_REQUEST[page]) { 
     $lynx .= " <a href=$PHP_SELF"."?page=$k&look4=".urlencode(stripslashes($_REQUEST[look4])).">".($k+1)."</a>"; 
     } else { 
     $lynx .= " <b>--".($k+1)."--</b>"; 
     } 
} 
if ($pages < 2) { 
     $lynx = "All results shown on this page"; 
} 
if ($havesome == 0) { 
     $lynx = "Sorry - no titles matched. Please change your search string"; 
     } 
?> 
<html><head> 
<title>Showing blog entries</title> 
<body> 
<h2>Search titles on "The Horse's Mouth"</h2> 
<form>Search only for titles including ... <input name=look4 
value="<?= htmlspecialchars(stripslashes($_REQUEST[look4])) ?>"> 
(Please leave box empty to select all titles)<br> 
<input type=submit></form><br> 
<h2>Here are the entries you selected - page <?= $_REQUEST[page]+1 ?>:</h2><br> 
<?= $html ?> 
<?= $lynx ?> 
</body> 
+0

나는 그것을 시도 할 것입니다! 덕분에 – Jdev

1

.

<?php 

... 

// If no 'page' parameter is found, default to 1 
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1; 
// Results per page 
$limit = 10; 
// Offset = (page - 1) * limit. (page 1 = 0, page 2 = 10, etc...) 
$offset = ($currentPage - 1) * $limit; 

if(isset($_POST['search'])) 
{ 
    $valueToSearch = $_POST['valueToSearch']; 
    // search in all table columns 
    // using concat mysql function 
    $query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%' LIMIT $limit OFFSET $offset"; 
    $search_result = filterTable($query); 

} 
else { 
    $query = "SELECT * FROM `users` LIMIT $limit OFFSET $offset"; 
    $search_result = filterTable($query); 
} 

// function to connect and execute the query 
function filterTable($query) 
{ 
    $connect = mysqli_connect("localhost", "root", "", "test_db"); 
    $filter_Result = mysqli_query($connect, $query); 
    return $filter_Result; 
} 

?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>PHP HTML TABLE DATA SEARCH</title> 
     <style> 
      table,tr,th,td 
      { 
       border: 1px solid black; 
      } 
     </style> 
    </head> 
    <body> 

     <form action="php_html_table_data_filter.php" method="post"> 
      <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br> 
      <input type="submit" name="search" value="Filter"><br><br> 

      <table> 
       <tr> 
        <th>Id</th> 
        <th>First Name</th> 
        <th>Last Name</th> 
        <th>Age</th> 
       </tr> 

     <!-- populate table from mysql database --> 
       <?php while($row = mysqli_fetch_array($search_result)):?> 
       <tr> 
        <td><?php echo $row['id'];?></td> 
        <td><?php echo $row['fname'];?></td> 
        <td><?php echo $row['lname'];?></td> 
        <td><?php echo $row['age'];?></td> 
       </tr> 
       <?php endwhile;?> 
      </table> 
     </form> 

     <a href="php_html_table_data_filter.php?page=<?php echo $page-- ?>">Previous</a> 
     <a href="php_html_table_data_filter.php?page=<?php echo $page++ ?>">Next</a> 

    </body> 
</html> 

추신 : 이전 예에서는 페이지가 첫 번째인지 마지막인지를 확인하지 않았으므로 그렇게해야합니다.

+0

나는 그것을 시험해 볼 것이다! 고맙습니다 – Jdev