2017-08-02 47 views
0

我搜索了类似的问题,并在这里,只有答案,发现一个,但没有得到证实的问题是如何固定的(will_paginate - other queries not showing on page 2 and beyondPHP PAGINATE 2

基本上我使用过的PAGINATE一个去同一个测试页面一个搜索框,然后在每个页面上只回显一个结果。它的作品在第一页上,但在第二页上没有显示任何内容。

任何人都可以建议,如果我失去了一些愚蠢的东西或缺少点?在我的页面上它只是一个文本框和搜索,然后我希望它分页如果不止一个结果。

继承人的完整代码

<?php 
 

 
\t require ("cw/connect.php"); 
 
    if(isset($_POST['search'])) { 
 
    $search = mysqli_real_escape_string($connect,$_POST['search']); 
 
    $search = preg_replace("#[^0-9a-z]i#","", $search); 
 
\t $start=0; 
 
\t $limit=2; 
 
    //Check Form is not blank 
 
\t if (empty($search)){ 
 
     $msg = "Please enter a search criteria"; 
 
\t } 
 
\t else { 
 
\t \t 
 

 
    $query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE 
 
\t first LIKE '%".$search."%' 
 
\t OR 
 
\t last LIKE '%".$search."%' 
 
\t OR 
 
\t email LIKE '%".$search."%' 
 
\t OR 
 
\t about LIKE '%".$search."%' 
 
\t OR 
 
\t username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!"); 
 

 
\t $count = mysqli_num_rows($query); 
 
\t 
 
\t if(isset($_GET['id'])) 
 
\t { 
 
\t \t $id=$_GET['id']; 
 
\t \t $start=($id-1)*$limit; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $id=1; 
 
\t } 
 
\t $page=ceil($count/$limit); 
 
    $query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE 
 
\t first LIKE '%".$search."%' 
 
\t OR 
 
\t last LIKE '%".$search."%' 
 
\t OR 
 
\t email LIKE '%".$search."%' 
 
\t OR 
 
\t about LIKE '%".$search."%' 
 
\t OR 
 
\t username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!"); 
 
\t \t 
 
\t \t 
 
    if($count == 0){ 
 
     $msg = "There was no search results!"; 
 

 
    }else{ 
 

 
     while ($row = mysqli_fetch_array($query)) { 
 
\t \t 
 
     $id = $row ['id']; 
 
     $username = $row ['username']; 
 
     $first = $row ['first']; 
 
     $last = $row ['last']; 
 
     $email = $row ['email']; 
 
     $about = $row ['about']; 
 
\t \t 
 
\t \t //The layout and what will be shown on page 
 
     $msg .='<p><b>ID:</b> '.$id. 
 
\t \t '<br><b>User:</b> '.$username. 
 
\t \t '<br><b>First Name:</b> '.$first. 
 
\t \t '<br><b>Last Name:</b> '.$last. 
 
\t \t '<br><b>Email:</b> '.$email. 
 
\t \t '<br><b>About:</b> '.$about. 
 
\t \t '</p> 
 
\t \t <hr>'; 
 

 
     } 
 
\t ?> 
 
\t 
 
\t <ul> 
 
\t 
 
\t <?php 
 
\t if($id > 1) { 
 
\t ?> 
 
\t 
 
\t <li><a href="?id=<?php echo ($id-1);?>">Previous</a></li> 
 
\t <?php } ?> 
 
\t 
 
\t 
 
\t <?php 
 
\t for($i=1;$i <= $page; $i++) 
 
\t { 
 
\t ?> 
 
\t 
 
\t \t <li><a href="?id=<?php echo $i;?>"><?php echo $i; ?></a></li> 
 
     
 
\t <?php \t \t 
 
\t } 
 
\t ?> 
 
\t 
 
\t <?php 
 
\t if($id!=$page) 
 
\t { 
 
\t \t 
 
\t ?> 
 
\t 
 
\t <li><a href="?id=<?php echo ($id+1);?>">Next</a></li> 
 
\t 
 
\t </ul> 
 
\t 
 
\t <?php 
 
    } 
 
    } 
 
    } 
 
\t } 
 
    ?> 
 
<form action ="" method = "post"> 
 
<input name="search" type="text" size="30" placeholder="eg name"/> 
 
<input type="submit" value="search"/> <?php echo "$msg";?> 
 
</form> 
 

 
</p> 
 
<br/><br/>

+0

你为什么要包括'CW/connect.php'两次? –

+0

抱歉,我复印时出现错误,现在我已将其删除 – CwStrange

+0

为什么您首先获取所有匹配的记录,然后获取具有限制和偏移量的_all_记录(无条件)?您还应该检查第二个查询的实际外观。 –

回答

0

我已经重构的代码颇有几分。

  1. 我已经删除了第二个查询,因为您所做的计数不需要在该位置并且可以向下移动。因此删除查询。
  2. 我用正斜杠更新了你的按钮链接,所以它不会追加?每次(?ID = 1 ID = 1 ID = 1后3次点击?)
  3. 所做的HTML分页更好地使用内置在PHP模板系统
  4. 最大的一个阅读 - 你失去你的搜索查询点击链接后,我将表单转换为GET,并将参数添加到上一页和下一页的链接中。

所以我的结果是这样的 - 请注意 - 我还没有测试它:

require("cw/connect.php"); 
$msg = ""; 
if (isset($_GET['search'])) { 
    $search = mysqli_real_escape_string($connect,$_GET['search']); 
    $search = preg_replace("#[^0-9a-z]i#","", $search); 

    if (empty($search)) { 
     $msg = "Please enter a search criteria"; 
    } else { 
     $items_per_page = 1; 
     $page_id = 1; 
     $start = 0; 

     // added a little validation 
     if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 1) { 
      $page_id = $_GET['id']; 
      $start = ceil($page_id * $items_per_page); 
     } 

     $query = "SELECT id, username, first, last, email, about FROM users WHERE 
      first LIKE '%".$search."%' OR 
      last LIKE '%".$search."%' OR 
      email LIKE '%".$search."%' OR 
      about LIKE '%".$search."%' OR 
      username LIKE '%".$search."%'"; 

     $result_query = mysqli_query($connect, $query . "LIMIT ".$start.",".$items_per_page) or die ("There are no search results!"); 
     $count_query = mysqli_query($connect, $query) or die("There are no search results"); 

     $count = mysqli_num_rows($count_query); 
     $page = ceil($count/$items_per_page); 

     if ($count <= 0) { 
      $msg = "There where no search results!"; 
     } else { 
      while ($row = mysqli_fetch_array($result_query)) { 
       $user_id  = $row ['id']; 
       $username = $row ['username']; 
       $first = $row ['first']; 
       $last  = $row ['last']; 
       $email = $row ['email']; 
       $about = $row ['about']; 

       //The layout and what will be shown on page 
       $msg .= '<p><b>ID:</b> '.$user_id. 
        '<br><b>User:</b> '.$username. 
        '<br><b>First Name:</b> '.$first. 
        '<br><b>Last Name:</b> '.$last. 
        '<br><b>Email:</b> '.$email. 
        '<br><b>About:</b> '.$about. 
        '</p><hr>'; 
      } ?> 
      <ul> 
       <?php if ($page_id > 1): ?> 
        <li><a href="/?id=<?php echo($page_id - 1); ?>&search=<?php echo $search; ?>">Previous</a></li> 
       <?php endif; ?> 

       <?php for ($i = 1; $i <= $page; $i++): ?> 
        <li><a href="/?id=<?php echo $i; ?>&search=<?php echo $search; ?>"><?php echo $i; ?></a></li> 
       <?php endfor; ?> 

       <?php if ($page_id != $page): ?> 
        <li><a href="/?id=<?php echo($page_id + 1); ?>&search=<?php echo $search; ?>">Next</a></li> 
       <?php endif; ?> 
      </ul> 
    <?php } 
    } 
} 
?> 
<form action="" method="get"> 
    <input name="search" type="text" size="30" placeholder="eg name"/> 
    <input type="submit" value="search"/> 
</form> 
<?php echo "$msg"; ?> 
+0

嗨sbrn,谢谢您的提问,我感谢您的更新:)。测试这个我似乎得到了和以前一样的结果,结果显示在搜索提交按预期提供,但是当涉及到第2页时等等它再次变为空白 – CwStrange

+0

道歉,它不会空白,当我点击下一页时,它在URL中更改,但内容与第一页相同,分页不会增加到应显示的页面数量。每页说1个结果。如果theres 10结果,我应该看到10页链接1,2,3,4等....但现在我的方式看到前| | 1 | Next – CwStrange

+0

我已经更新了我的答案。如果您遇到任何问题,请告知我。 – sbrn