php分页没有显示在第2页

时间:2017-08-02 13:22:37

标签: php mysqli

我在这里搜索了类似的问题和答案,但只找到一个,但未确认问题是如何解决的(will_paginate - other queries not showing on page 2 and beyond

基本上我在使用搜索框的测试页面进行分页,然后在每个页面上只回显一个结果。它在第一页上工作但在第2页上没有显示任何内容。

任何人都可以告诉我,如果我错过了一些愚蠢的事情或者错过了这一点吗?在我的页面上它只是一个文本框和搜索,然后我希望它分页如果有多个结果。

下载完整的代码



<?php

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

    $query = mysqli_query($connect,"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."%' LIMIT $start, $limit") or die ("There was no search results!");

	$count = mysqli_num_rows($query);
	
	if(isset($_GET['id']))
	{
		$id=$_GET['id'];
		$start=($id-1)*$limit;
	}
	else
	{
		$id=1;
	}
	$page=ceil($count/$limit);
    $query = mysqli_query($connect,"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."%' LIMIT $start, $limit") or die ("There was no search results!");
		
		
    if($count == 0){
      $msg = "There was no search results!";

    }else{

      while ($row = mysqli_fetch_array($query)) {
		  
        $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> '.$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($id > 1) {
	  ?>
	  
	  <li><a href="?id=<?php echo ($id-1);?>">Previous</a></li>
	  <?php } ?>
	  
	  
	  <?php
	  for($i=1;$i <= $page; $i++)
	  {
	  ?>
	  
		<li><a href="?id=<?php echo $i;?>"><?php echo $i; ?></a></li> 
     
	 <?php		
	  }
	  ?>
	  
	  <?php
	  if($id!=$page)
	  {
		  
	  ?>
	  
	  <li><a href="?id=<?php echo ($id+1);?>">Next</a></li>
	  
	  </ul> 
	  
	<?php
    }
  }
  }
	}
  ?>
<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/>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我已经重新调整了代码。

  1. 我删除了第二个查询,因为您在该位置不需要执行计数并且可以向下移动。因此删除查询。
  2. 我用正斜杠更新了你的按钮链接,所以它不会附加?每次(?id = 1?id = 1?id = 1点击3次后)
  3. 使用内置的php模板系统
  4. 更好地阅读HTML分页
  5. 最大的 - 您在点击链接后丢失了搜索查询,因此我已将表单转换为GET并将参数添加到上一页和下一页的链接中。
  6. 所以我的结果是 - 小心 - 我没有测试过它:

    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"; ?>