2016-04-15 481 views
0

使用php創建一個新項目:使用搜索功能從mysql數據庫中檢索數據(search.php) - 爲搜索結果頁面(find.php)添加分頁。問題是,當顯示第一個搜索時,它顯示正確的數據;然而,當我點擊第二頁(分頁按鈕)時,它再次填充所有數據 - 不是我搜索的術語:請幫助!MySQL PHP搜索結果與分頁

的search.php

<form action="find.php" method="post"> 
<input name="search" type="search" autofocus><input type="submit" name="button"> 
</form> 

find.php

<?php 

include_once("config.php"); 


if(isset($_POST['button'])){ 
    $search=$_POST['search']; 
} 

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$row = mysql_num_rows($result); 
$page_rows = 3; 
$last = ceil($row/$page_rows); 

if($last < 1){ 
    $last = 1; 
} 
$pagenum = 1; 

if(isset($_GET['page'])){ 
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['page']); 
} 

if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
} 

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>"; 


$paginationCtrls = ''; 

$self = $_SERVER['PHP_SELF']; 

if($last != 1){ 
    if ($pagenum > 1) { 
     $previous = $pagenum - 1; 
     $paginationCtrls .= '<a href="'.$self.'?page='.$previous.'">Previous</a>'; 
     for($i = $pagenum-4; $i < $pagenum; $i++){ 
      if($i > 0){ 
       $paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>'; 
      } 
     } 
    } 
    $paginationCtrls .= '<span class="current">'.$pagenum.'</span>'; 

    for($i = $pagenum+1; $i <= $last; $i++){ 
     $paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>'; 
     if($i >= $pagenum+4){ 
      break; 
     } 
    } 
    if ($pagenum != $last) { 
     $next = $pagenum + 1; 
     $paginationCtrls .= '<a href="'.$self.'?page='.$next.'">Next</a> '; 
    } 
} 

?> 

<table border='1' cellpadding='10' style="border-collapse:collapse;"> 
<tr> 
<th>id</th> 
<th>name</th> 
<th>address</th> 
<th>phone</th> 
<th>email</th> 
<th>website</th> 
<th>locations</th> 
<th>info</th> 
</tr> 

<?php 
while($row = mysql_fetch_array($result)){ 
?> 

<tr> 
<td><?php echo $row['id']; ?></td> 
<td><?php echo $row['name']; ?></td> 
<td><?php echo $row['address']; ?></td> 
<td><?php echo $row['phone']; ?></td> 
<td><?php echo $row['email']; ?></td> 
<td><?php echo $row['website']; ?></td> 
<td><?php echo $row['locations']; ?></td> 
<td><?php echo $row['info']; ?></td> 
</tr> 

<?php 
} 

?> 
+0

第一個查詢不應該包含限制。第二個應該包含一個限制和一個抵消 –

回答

-1

您必須設置搜索條件中的分頁鏈接。嘗試這個。

<form action="find.php" method="post"> 
<input name="search" type="search" autofocus><input type="submit" name="button"> 
</form> 
<?php 

include_once("config.php"); 


if(isset($_POST['button'])){ 
    $search=$_POST['search']; 
} 


$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$row = mysql_num_rows($result); 
$page_rows = 3; 
$last = ceil($row/$page_rows); 

if($last < 1){ 
    $last = 1; 
} 
$pagenum = 1; 

if(isset($_GET['page'])){ 
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['page']); 
} 
// get search tearm from url 
if(isset($_GET['search'])){ 
    $search=$_GET['search']; 
} 
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
} 

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>"; 


$paginationCtrls = ''; 

$self = $_SERVER['PHP_SELF']; 
// you must have to set search tearm in pagination links 
if($last != 1){ 
    if ($pagenum > 1) { 
     $previous = $pagenum - 1; 
     $paginationCtrls .= '<a href="'.$self.'?page='.$previous.'&search='.$search.'">Previous</a>'; 
     for($i = $pagenum-4; $i < $pagenum; $i++){ 
      if($i > 0){ 
       $paginationCtrls .= '<a href="'.$self.'?page='.$i.'&search='.$search.'">'.$i.'</a>'; 
      } 
     } 
    } 
    $paginationCtrls .= '<span class="current">'.$pagenum.'</span>'; 

    for($i = $pagenum+1; $i <= $last; $i++){ 
     $paginationCtrls .= '<a href="'.$self.'?page='.$i.'&search='.$search.'">'.$i.'</a>'; 
     if($i >= $pagenum+4){ 
      break; 
     } 
    } 
    if ($pagenum != $last) { 
     $next = $pagenum + 1; 
     $paginationCtrls .= '<a href="'.$self.'?page='.$next.'&search='.$search.'">Next</a> '; 
    } 
} 

?> 

<table border='1' cellpadding='10' style="border-collapse:collapse;"> 
<tr> 
<th>id</th> 
<th>name</th> 
<th>address</th> 
<th>phone</th> 
<th>email</th> 
<th>website</th> 
<th>locations</th> 
<th>info</th> 
</tr> 

<?php 
while($row = mysql_fetch_array($result)){ 
?> 

<tr> 
<td><?php echo $row['id']; ?></td> 
<td><?php echo $row['name']; ?></td> 
<td><?php echo $row['address']; ?></td> 
<td><?php echo $row['phone']; ?></td> 
<td><?php echo $row['email']; ?></td> 
<td><?php echo $row['website']; ?></td> 
<td><?php echo $row['locations']; ?></td> 
<td><?php echo $row['info']; ?></td> 
</tr> 

<?php 
} 

?> 
+0

將無法​​正常工作,POST的形式,但網址是GET – 2016-04-15 02:25:14

+0

這是什麼問題? –

+0

在使用POST進行初始搜索之後,搜索項目現在位於GET全局中以供將來頁面 – 2016-04-15 02:27:34