排序不适用于分页

时间:2017-05-25 02:19:10

标签: php sql

在将其与分页结合之前,排序是完美的。但是现在我面临的问题是什么时候将排序值传递给分页:

  

您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以便在'?page = 1 ASC LIMIT 0,10'附近使用正确的语法。在第3行

以下是我的代码:

<?php

session_start();
$host = 'localhost';
$user = 'root';
$pass ='password';
$name = 'dbname';
$con = new mysqli ($host,$user,$pass,$name);
?>

<html>
<h2>Your Redeemed Ticket (<?php echo $_SESSION['username'];?>):</h2>


<table border='1' align='center' cellpadding='10'>
    <tr align = "center">
    <td>
         <a href="user_history.php?sort=purchase_id">Purchase ID</a>
    <td>
        <a href="user_history.php?sort=ticket_type">Ticket Type</a>
    <td>
        <a href="user_history.php?sort=qty">Quantity</a>
    <td>
        <a href="user_history.php?sort=date">Date</a>
    </tr>


</html>
<?php



    // default sorting
    if(isset($_GET['sort'])){
        $sortOrder = $_GET['sort'] ? :0;
        /*Pagination*/
        $limit = 10;  
        if (isset($_GET["page"])) 
        { 
            $page  = $_GET["page"]; 
        } 
        else 
        { 
            $page=1; 
        }

        $start_from = ($page-1) * $limit;
        $username = $_SESSION['username'];
    $data = 
    "
    SELECT *
    FROM redeem 
    WHERE redeem_by = '$username' ORDER BY ".$sortOrder." ASC LIMIT $start_from, $limit
    ";
    $result = $con->query($data);

    if ($result === false)
            die (mysqli_error($con));
    while($rows = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td align='center'>"; 
            echo $rows['purchase_id']; 
        echo "<td align='center'>";
            echo $rows['ticket_type'];
        echo "<td align='center'>";
            echo $rows['qty'];
        echo "<td align='center'>";
            echo $rows['date'];
        echo "</tr>";
    }
    echo "</table>";


    /*PAGE NUMBER DISPLAY*/
    $data2 = "SELECT COUNT(redeem_by) FROM redeem";  
    $rs_result = mysqli_query($con,$data2);  
    $row = mysqli_fetch_row($rs_result);  
    $total_records = $row[0];  
    $total_pages = ceil($total_records / $limit);  
    $pagLink = '';
    for ($i=1; $i<=$total_pages; $i++)
        {  
             $pagLink .="<a href='user_history.php?sort=".$sortOrder."?page=$i'>" .$i."/</a>"; 
        };  
    echo "<center>".$pagLink."</center>";  

    /*LOGOUT*/
    if(isset($_POST['logout']))
    {
        session_destroy();
        header ("location:login.php");
        exit();
    }
    }

?>

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些奇怪的问题,但您可能会在此处追溯您的搞砸GET变量的问题:

<a href='user_history.php?sort=".$sortOrder."?page=$i'>"

显然,当你有url参数时,你可以用&#39;?&#39;来描述起点。但在此之后,您需要使用&#39;&amp;&#39;

分隔其他参数

所以它应该是:

<a href='user_history.php?sort=".$sortOrder."&page=$i'>"