Wątek przeniesiony 2017-07-06 05:09 z PHP przez Patryk27.

Paginacja - problem z wyswietleniem wierszy z bazy

0

Witam,
Mam oto taki kod, który ma służyć jako paginacja niestety nie wyświetla żadnych wyników,lecz poprawnie pobiera informację o ilości danych w bazie. Czy ktoś może widzi błąd?
Z góry dziękuję za pomoc.


<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

$connection = new mysqli($servername, $username, $password, $dbname);
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}
$sql = "select count(id) from test";
$querry = mysqli_query($connection, $sql);
$row = mysqli_fetch_row($querry);
$rows = $row[0];
$page_rows = 6;
$last = ceil($rows / $page_rows);
if ($last < 1) {
    $last = 1;
}
$pagenum = 1;
if (isset($_get['pn'])) {
    $pagenum = preg_replace('#[^0-9]#', '', $_get['pn']);
}
if ($pagenum < 1) {
    $pagenum = 1;
} else if ($pagenum > $last) {
    $pagenum = $last;
}
$phpself = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);
$limit = 'LIMIT' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
$sql = "select id, imie from test where approved ='1' order by id desc $limit";
$query = mysqli_query($connection, $sql);
$textline1 = "Test (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = ''; 
if ($last != 1) {
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="' .$phpself. '?pn=' . $previous . '">Previous</a> &nbsp; &nbsp; ';
       
        for ($i = $pagenum - 4; $i < $pagenum; $i++) {
            if ($i > 0) {
                $paginationCtrls .= '<a href="' . $phpself . '?pn=' . $i . '">' . $i . '</a> &nbsp;';
            }
        }
    }
    $paginationCtrls .= '' . $pagenum . '&nbsp; ';
    for ($i = $pagenum + 1; $i <= $last; $i++) {
        $paginationCtrls .= '<a href="' .$phpself. '?pn=' . $i . '">' . $i . '</a> &nbsp;';
        if ($i >= $pagenum + 4) {
            break;
        }
    }
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href ="' . $phpself . '?pn=' . $next . '">next</a>';
    }
}
$list = '';
while ($row = mysqli_fetch_array($querry, MYSQLI_ASSOC)) {
    $id = $row["id"];
    $imie = $row["imie"];
    $list .= '<p><a href=test.php?id=' . $id . '">' . $imie . '</a>';
}
$connection->close();
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <h2><?php echo $textline1; ?> Paged</h2>
            <p><?php echo $textline2; ?></p>
            <p><?php echo $list; ?></p>
            <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
            
        </div>
    </body>
</html>

0

Zrób sobie echo wygenerowanego z LIMIT zapytania.

1 użytkowników online, w tym zalogowanych: 0, gości: 1