MySQL / PHP - 页码分页有时只显示10页

时间:2017-07-09 15:08:13

标签: php mysql

我的MYSQL / PHP仪表板出现问题。 - 目前我有50页,但目前它们都显示在同一页面上。

http://imgur.com/wDfTWUa - 正如您在附件中看到的那样。 - 我只想显示10个页面,并且能够点击其余页面而不会看到4行页面。

Exsampel< - 2 3 4 5 6 7 8 9 10 - >当你在?page = 1,如果你在页面上?page = 10< - 11 12 13 14 15 16 17 18 19->

希望你能帮助我。

代码:

<?php

include 'config.php';

$sidenr = $_GET['page'];

$sidenr2 = ($sidenr -1) * 10;

echo $sidenr2;
echo "<br><br>";

$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))        
    {
        echo $row['id']."<br>";
    }

    $result = mysqli_query($conn, "SELECT * FROM test");
    $num_rows = mysqli_num_rows($result);

    $sideantal = $num_rows / 10;

    echo "Der skal være antal sider: ". $sideantal;

    echo "<br><br>antal rækker ". $num_rows . "<br><br>";

    ?>
    <br><br>
    <?php

    for ($number = 1; $number <= $sideantal; $number++) {
    echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
    } 
    ?>

3 个答案:

答案 0 :(得分:1)

尝试改变这个:

for ($number = 1; $number <= $sideantal; $number++) {
    echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
    </li>";
}

到此:

for ($number = 1; $number <= $sideantal; $number++) {
    if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
        echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
        </li>";
    }
}

答案 1 :(得分:0)

您可以尝试以下代码:

for ($number = 1; $number <= $sideantal; $number++) {
    /** If the loop count is greater than the current page but less than current page plus 10 */
    if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;


    /** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */

    if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;

    else $is_valid = false;

    if ($is_valid)  {
        echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
    }
}

答案 2 :(得分:0)

function getPageRange($current, $max, $total_pages = 10) {
    $desired_pages = $max < $total_pages ? $max : $total_pages;
    $middle = ceil($desired_pages/2);
    if ($current <= $middle){
        return [1, $desired_pages];
    }
    if ($current > $middle && $current <= ($max - $middle)) {
        return [
            $current - $middle,
            $current + $middle
        ];
    }
    if ($current <= $max ) {
        return [
            $current - ($desired_pages - 1),
            $max
        ];
    }

}
list($min,$max) = getPageRange($sidenr, $sideantal);


foreach (range($min, $max) as $number) {
    echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}