如何应用分页排序?

时间:2017-01-04 06:15:20

标签: php jquery mysql sorting pagination

我尝试使用分页进行排序,但它只是排序第一页,因为页码是一个很好的我,但第二页和第三页的记录没有排序。

这是第一页代码

include_once("session.php");
include_once("functions.php");
include_once("connection.php");

$username = $_SESSION["username"];


$orderby = "job_name";
$order = "asc";
//$conn2 = mysqli_connect("localhost","root","","jobportal");
//$conn3 = mysqli_connect("localhost","root","","jobportal");
//$conn4 = mysqli_connect("localhost","root","","jobportal");


$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";

$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();

$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];

$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_GET["page_approved"])){
    $page_approved = $_GET["page_approved"];
}
if($page_approved){
    $start_approved = ($page_approved-1)*$limit;
}
else{
    $start_approved = 0;
}

if(isset($_GET["page_pending"])){
    $page_pending = $_GET["page_pending"];
}
if($page_pending){
    $start_pending = ($page_pending-1)*$limit;
}
else{
    $start_pending = 0;
}

$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby." ".$order." limit $start_approved,$limit";
$stmt1 = $conn->query($sql1);

$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby." ".$order." limit $start_pending,$limit";
$stmt2 = $conn->query($sql2);

<script>
    var page_no_approved=1;
    var page_no_pending=1;
    function order_column1(column_name,column_order){
        $.ajax({
        url: "get_job_list.php",
        data: "orderby1="+column_name+"&order1="+column_order+"&page_no_approved="+page_no_approved,
        type: "POST",
        success: function(data){
            $("#showresults").html(data);
       }
    });
}
function order_column2(column_name,column_order){

    $.ajax({
    url: "get_job_list.php",
    data: "orderby2="+column_name+"&order2="+column_order+"&page_no_pending="+page_no_pending,
    type: "POST",
    success: function(data){
        $("#showresults").html(data);
    }
    });
}
</script>

这是第二页代码(ajax)

include_once("session.php");
include_once("functions.php");
include_once("connection.php");

$username = $_SESSION["username"];

$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";

$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();

$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];

$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_POST["page_no_approved"])){
    $page_approved = $_POST["page_no_approved"];
}
if($page_approved){
    $start_approved = ($page_approved-1)*$limit;
}
else{
    $start_approved = 0;
}

if(isset($_POST["page_no_pending"])){
    $page_pending = $_POST["page_no_pending"];
}
if($page_pending){
    $start_pending = ($page_pending-1)*$limit;
}
else{
    $start_pending = 0;
}

$orderby1 = "job_name";
$order1 = "asc";
$orderby2 = "job_name";
$order2 = "asc";

if(!empty($_POST["orderby1"])){
    $orderby1 = $_POST["orderby1"];
}
if(!empty($_POST["order1"])){
    $order1 = $_POST["order1"];
}

if(!empty($_POST["orderby2"])){
    $orderby2 = $_POST["orderby2"];
}
if(!empty($_POST["order2"])){
    $order2 = $_POST["order2"];
}

$date_order1 = "asc";
$job_name_order1 = "asc";
$industry_order1 = "asc";
$date_order2 = "asc";
$job_name_order2 = "asc";
$industry_order2 = "asc";

if($orderby1 == "date" && $order1=="asc"){
    $date_order1 = "desc";
}
if($orderby1 == "job_name" && $order1=="asc"){
    $job_name_order1 = "desc";
}
if($orderby1 == "industry" && $order1 == "asc"){
    $industry_order1 = "desc";
}
if($orderby2 == "date" && $order2=="asc"){
    $date_order2 = "desc";
}
if($orderby2 == "job_name" && $order2=="asc"){
    $job_name_order2 = "desc";
}
if($orderby2 == "industry" && $order2 == "asc"){
    $industry_order2 = "desc";
}

$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby1." ".$order1." limit $start_approved,$limit";
$result1 = $conn->query($sql1);

$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby2." ".$order2." limit $start_pending,$limit";
$result2 = $conn->query($sql2);

编辑:我没有包含显示数据的代码,因为它会使已经很大的代码更长并且可能不可读。如果有人能指出我应用分页概念的地方,或者有人能用小例子来解释这个概念,那将会有所帮助。

0 个答案:

没有答案