php分页功能的问题

时间:2016-09-05 08:33:45

标签: php mysql

我有两个php文件: 1.function.php 2. index.php

functions.php文件有一系列查询MySQL数据库以生成不同结果集的函数。例如,我有两个功能;一个返回2016年的所有数据,另一个返回年份为2015年的所有数据。

对于这两个查询,我希望对返回的结果集进行php分页。返回年份为2015年和2016年的所有数据的结果的函数示例如下所示:

       function get2015(){

    $con = dbConnect();

    $refs_per_page = 20;

    $query = "select * from mytable where year = 2015";
    $sql=$con->prepare($query);
    $sql->execute();
    $total = $sql->rowCount();
    $pages = ceil($total/$refs_per_page);
    echo "$total <br>";

    if (isset($_GET['page']) && is_numeric($_GET["page"])){
    $page = (int) $_GET['page'];
    }//endif


    if($page =="" || $page ==1){
    $page=0;
    }
    else{
    $page = ($page * $refs_per_page)-$refs_per_page;
    }
    //
    $query = "select * from mytable where year = 2015 limit $page, $refs_per_page";
    $sql=$con->prepare($query);
    $sql->execute();

    $sql->setFetchMode(PDO::FETCH_ASSOC);

        while ($row=$sql->fetch()){
        $title = $row['title'];
        $authors = $row['authors'];
        echo "<b>$title</b>" . "<br>" . $authors . "<p>";
        }



        //
        for($x=1; $x<=$pages; $x++){

            ?><a href = "index.php?page= <?php echo $x;?>" style="text-decoration:none"> <?php echo $x;?> </a> <?php
    }
    }
//function get records for 2016
function get2016(){

$con = dbConnect();

$refs_per_page = 20;

$query = "select * from mytable where year = 2016";
$sql=$con->prepare($query);
$sql->execute();
$total = $sql->rowCount();
$pages = ceil($total/$refs_per_page);
echo "$total <br>";

if (isset($_GET['page']) && is_numeric($_GET["page"])){
$page = (int) $_GET['page'];
}//endif


if($page =="" || $page ==1){
$page=0;
}
else{
$page = ($page * $refs_per_page)-$refs_per_page;
}
//
$query = "select * from mytable where year = 2016 limit $page, $refs_per_page";
$sql=$con->prepare($query);
$sql->execute();

$sql->setFetchMode(PDO::FETCH_ASSOC);

    while ($row=$sql->fetch()){
    $title = $row['title'];
    $authors = $row['authors'];
    echo "<b>$title</b>" . "<br>" . $authors . "<p>";
    }



    //
    for($x=1; $x<=$pages; $x++){

        ?><a href = "index.php?page= <?php echo $x;?>" style="text-decoration:none"> <?php echo $x;?> </a> <?php
}
}

index.php代码中的代码如下所示

<?php
include "functions.php";
$page = $_GET["page"];
if($page){
 if($page=="get2015"){
    get2015();
    }
if($page=="get2016"){
    get2016();
    }
}
?>
<html>
Archive<br>
<a href="index.php?page=get2015"> 2015</a><br>
<a href="index.php?page=get2016"> 2016</a><br>
</html>
<?php ?>

第一页渲染没问题但是当我点击第二页时页面变为空白。下面的@Syed建议使用一个偏移量变量,虽然它有用但与上面代码中的$ page变量相同。我如何让分页工作?我非常怀疑问题是我如何在index.php上调用页面

1 个答案:

答案 0 :(得分:1)

你可以使$offset变量减去1,然后乘以$per_page

例如

index.php中的用户偏移量变量就是这样计算的。

// offset should be equal to 0 and query should be 
// SELECT * FROM your_table WHERE year='2015' LIMIT 0, 20
(page=1 - 1) = 0 * $per_page = 20 

index.php?page=2中的用户偏移量变量就是这样计算的。

// offset should be equal to 20 and query should be 
// SELECT * FROM your_table WHERE year='2015' LIMIT 20, 20
(page=2 - 1) = 1 * $per_page = 20 

// offset should be equal to 40 and query should be 
// SELECT * FROM your_table WHERE year='2015' LIMIT 40, 20
(page=3 - 1) = 2 * $per_page = 40 

$sql = sprintf("SELECT COUNT(*) FROM your_table WHERE year='%s'", $_GET['year']);
// Query to database and store in pages variable
$total_record = $con->query($sql)->result();

$page = ( isset($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1;
$per_page = 20;
$offset = ($page - 1) * $per_page;
$pages = ceil($total/$per_page);

$sql = sprintf("SELECT * FROM your_table WHERE year='%s' LIMIT %s, %s", $_GET['year'], $offset, $per_page);

// QUERY TO DATABASE

for($i=1; $i<=$pages; $i++){

    if ($page == $i) {
        echo '<span class="active">'. $i .'</span>';
    }else{
        echo '<a href = "index.php?year="youryear"&page='. $i .'" style="text-decoration:none">' .$i .'</a>';
    }
}