在网站上的Php分页

时间:2016-05-24 09:56:55

标签: php pagination

随着数据的增长,对分页的需求非常重要。一切正常。我的SELECT语句中有一个限制并计算行数。目标是在页面底部进行分页,限制为10.页面包含数据库中的最新行。我需要

吗?
<?php

$servername     = "localhost";
$username       = "";
$password       = "";
$dbname         = "";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$nr     = 0;
$count = mysql_query("SELECT count(*) AS TOTAL FROM quotes");
$row = mysql_fetch_array($sum);

$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $output     = "";
    while($row  = $result->fetch_assoc() ) {
        $topic   = trim($row["topic"]);
        $quote   = trim($row["quote"]);
        $author  = trim($row["author"]);
        $id      = trim($row["id"]);

        $output .= injectNColumnWrapper(3, $nr, "container row", $nr);
        $output .="<div class='col s12 m6 l4 z-depth-1'>";
        $output .="<div class='card-panel grey darken-4 white-text center'>";
        $output .=" <h5>Citat: {$id}</h5>";
        $output .="</div>";
        $output .="<pre class='flow-text black-text' wrap='soft'>";        
        $output .="<p class='flow-text-p citat'>&#34{$quote}&#34</p>";
        $output .="<p style='font-weight:bold; class='flow-text-p author'>{$author}</p>";
        $output .="<p class='flow-text-p topic'>{$topic}</p>";
        $output .="</pre>";
        $output .="<div class='content_wrapper'>";
        $output .="<h4></h4>";
        $output .="<div class='voting_wrapper' id='vote-{$id}'>";
        $output .="<div class='voting_btn'>";
        $output .="<div class='up_button'>&nbsp;</div>";
        $output .="<span class='up_votes'>0</span>";
        $output .="</div>";
        $output .="<div class='voting_btn'>";
        $output .="<div class='down_button'>&nbsp;</div>";
        $output .="<span class='down_votes'>0</span>";
        $output .="</div>";
        $output .="<br>";
        $output .="</div>";
        $output .="</div>";
        $output .="</div>";
        $nr++;

    }
    $output    .= "</div>";
    echo $output;
}else {
    echo "0 results";
}

$conn->close();


function injectNColumnWrapper($cols_per_row, $closePoint, $cssClass="container row", $nthElem=""){
    $blockDisplay       = "";
    if( ($closePoint == 0) ){
        $blockDisplay   = "<div class='" . $cssClass . " container_nr_" . $nthElem . "'>"  . PHP_EOL;
    }else if( ($closePoint % $cols_per_row) == 0 && ($closePoint != 0) ){
        $blockDisplay   = "</div><div class='" . $cssClass . " container_nr_" . $nthElem . "'>"  . PHP_EOL;
    }
    return $blockDisplay;
}
?>

3 个答案:

答案 0 :(得分:0)

$limit = 10;
$start = $nr * $limit
$sql = "SELECT * FROM quotes ORDER BY date DESC limit $start $limit";

其中$ nr是当前页面的编号-1,所以如果你有第1页,它应该是0,如果是2然后是1,等等。

要获得页数,您需要计算所有结果并按限制进行除法,例如100/10 = 10页。简单的循环应该渲染页面。

答案 1 :(得分:0)

在查询中使用偏移量

您可以在分页中显示一组链接,然后从URL中捕获该参数,最后使用适当的偏移量构建查询。

分页链接:

// Note: $row['TOTAL'] comes from your counting sql (count(*))
for ($i = 0; $i< $row['TOTAL']; $i++) {
echo " <a href='?p=$i'>$i</a> |";
}

在您的网址中获取请求的网页:

$requestedPage = !empty($_GET['p']) ? intval($_GET['p']) : 0;

构建查询:

$pagination = 10 * $requestedPage;
$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10 OFFSET $pagination"; 

答案 2 :(得分:0)

您应该提供开始长度(偏移)值来限制MySQL中的数据以进行分页。 EG:

SELECT * FROM table_name WHERE your_condition LIMIT 0,9 - to fetch first 10 records

SELECT * FROM table_name WHERE your_condition LIMIT 10,19 - to fetch second set of  10 records
例如

Refer。它是duplicate question