页码不起作用

时间:2018-12-12 04:35:56

标签: php pagination

我当前正在尝试在表中实现分页,但是,每当我单击页面时,即使地址栏上的链接明确指出page = 2,page = 3等,它们也不会显示下一页。我想知道我的代码是否有任何错误。谢谢

    $totalPerPage = 20;
    $size= "50";
    $sql = ("SELECT DISTINCT * FROM shoe s 
            INNER JOIN shoelace sc ON s.cod = sc.cod
            WHERE s.shoefit =?");

    $stmt = $db->prepare($sql);
    $stmt->execute([$size]);

    $numberofResults = $stmt->rowCount();
        $numberOfPages = ceil($numberofResults/$totalPerPage);

        if(isset($_GET['page'])){
            $page = $_GET['page'];
        }else{
            $page = 1;
        }

        $thePageFormula = ($page-1)*$totalPerPage;
        $query = ("SELECT DISTINCT * FROM shoe s 
                INNER JOIN w_shoelace sc ON s.cod = sc.cod
                WHERE s.shoefit =?
                LIMIT $thePageFormula,$totalPerPage");

        $stmt2 = $db->prepare($query);
        $stmt2->execute([$size]);


while($row = $stmt2->fetch(PDO::FETCH_ASSOC)){
    //echos infromation in table form
    echo "<table><tr onclick='javascript:showRow(this);'><td>" 
    . $row['shoename'] . "</td><td>"
    . utf8_encode($row['shoecolor']) . "</td><td>"
    <img src='data:image/jpeg;base64,".base64_encode($row['shoeimg'])."'width='50' height='30'/></td><tr>"; 
    echo "</table>";
} 



   for ($page=1; $page <= $numberOfPages ; $page++){ 

    echo "<div class='pagination'> 
            <a href='?page=" . $page . "'>" . $page . "</a>
        </div>";

} 

1 个答案:

答案 0 :(得分:0)

我认为问题出在此SQL查询中

SELECT DISTINCT * FROM shoe s 
                INNER JOIN w_shoelace sc ON s.cod = sc.cod
                WHERE s.shoefit =?
                LIMIT $thePageFormula,$totalPerPage

对于分页,您必须始终保持SQL查询的 offset 值,这意味着它应该从哪里开始从数据库中获取数据。上面的查询应该看起来像这样

SELECT DISTINCT * FROM shoe s 
                    INNER JOIN w_shoelace sc ON s.cod = sc.cod
                    WHERE s.shoefit =?
                    LIMIT *limit_to_show_per_page*, *offset*

如果您开始从数据库中获取数据并且第一页的限制为10,则偏移值必须为0,如果转到第二页,则每页的限制将固定为10,偏移量将从0更改为到10等等。 希望你明白这一点。

相关问题