时间:2016-04-30 07:50:29

标签: php pagination

我在PHP中有一些关于分页的问题,我希望有人可以帮助我:)。

请参阅下面的我的代码和屏幕截图。

http://postimg.org/image/hg18gajyp/

我没有任何问题从我的数据库中分页我的所有结果,只要我点击页面它就能正常工作,它给了我想要的结果。但是,当我尝试过滤结果并点击其他页面时,它会从我的数据库中获取我所有数据的结果,请参阅下面的屏幕截图

http://s32.postimg.org/et9erbset/image.jpg

正如您所看到的那样,它会将结果过滤到所有BSCS课程,但是当我尝试点击那些1 2 3时,它会从我的数据库中获取所有数据的结果,就像我上传的第一个屏幕截图一样。

这是我的代码

<?php
session_start();
if($_SESSION['loggedadmin'] == true){
$course='';

include "include/dbstud.php";

$page = "";
$page = $_GET["page"];

if($page=="" || $page=="1")
  { 
  $page1=0;
  }
    else
    {
    $page1=($page*15)-15;
    }


  $sql = "SELECT * FROM studentnums LIMIT $page1,15";   

if(!empty($_POST['submit'])){


if(isset($_POST['course'])){ $search_term = $_POST['course']; } 
if(isset($_POST['course'])){ $search_term =          strip_tags($_POST['course']); }
if(isset($_POST['course'])){ $search_term = stripslashes($_POST['course']); } 

    $course = mysqli_real_escape_string($connection, $course);
    $course='';


        $pagec = "";
        $pagec = $_GET["pagec"];

        if($pagec=="" || $pagec=="1")
        { 
           $page2=0;
        }
           else
           {
             $page2=($pagec*15)-15;
           }
   $sql = "SELECT * FROM studentnums WHERE course ='$search_term' LIMIT $page2,15";

 $query2 = mysqli_query($connection, "SELECT * FROM studentnums WHERE course ='$search_term'") or die(mysql_error());

    $cou1 = mysqli_num_rows($query2); 
    $a1 = $cou1/15;
    $a1 = ceil($a1);

     }

    if(isset($a1))
     {
     $a = $a1;
     }

 $query = mysqli_query($connection, $sql) or die(mysql_error());

  $query1 = mysqli_query($connection, "SELECT * FROM studentnums") or die(mysql_error());

    $cou = mysqli_num_rows($query1); 
    $a = $cou/15;
    $a = ceil($a);

 }


else
if($_SESSION['loggedadmin'] == false){
header("Location: ../index.php"); 
$connection->close();
}

?>

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

为了其他同样遇到同样问题的人,我之前会发布我的问题答案。

<?php
session_start();
if($_SESSION['loggedadmin'] == true){
$course='';

require "include/dbstud.php";

if(isset($_GET["page"])) {
    $course = $_GET["course"];
    $page = $_GET["page"]; }

else {
    $page = ""; }

if($page=="" || $page=="1") { 
    $page1=0; }

else {
    $page1=($page*13)-13; }

    $sql = "SELECT * FROM studentnums WHERE course ='$course' LIMIT $page1,13"; 
    $query = mysqli_query($connection, $sql) or die(mysql_error()); 
    $queryrows = mysqli_query($connection, "SELECT * FROM studentnums WHERE course ='$course'") or die(mysql_error());

    $cou = mysqli_num_rows($queryrows); 
    $a = $cou/13;
    $a = ceil($a);   


  if(!empty($_POST['submit'])) {

        if(isset($_POST['course'])){
             $search_term = $_POST['course']; } 

        if(isset($_POST['course'])){
             $search_term = strip_tags($_POST['course']); }

        if(isset($_POST['course'])){ 
             $search_term = stripslashes($_POST['course']); } 

             $search_term = mysqli_real_escape_string($connection, $_POST['course']);


    $queryrows = mysqli_query($connection, "SELECT * FROM studentnums WHERE course ='$search_term'") or die(mysql_error());

    $cou = mysqli_num_rows($queryrows); 
    $a = $cou/13;
    $a = ceil($a);   


    $sql = "SELECT * FROM studentnums WHERE course ='$search_term' LIMIT 0,13"; 
    $query = mysqli_query($connection, $sql) or die(mysql_error()); }


}
else if($_SESSION['loggedadmin'] == false){
header("Location: ../index.php"); 
$connection->close(); }

?>

它现在过滤结果并对其进行分页而没有任何问题。