分页工作很奇怪

时间:2013-10-15 12:07:48

标签: php pagination

我不知道脚本是否有效,问题是 - 如果我删除或添加记录,或者会更改$ query。=“WHERE visible = 1,例如,我将删除5条记录,它显示我这样的事情 enter image description here

paginator没有看到已删除的记录,如果刷新firefox http://localhost/dwwithphp/public/admin/list_photos_5.php?s=40&p=11&sort=ln当我刷新http://localhost/dwwithphp/public/admin/list_photos_5.php然后看到更改时没有任何反应

刷新/admin/list_photos_5.php

enter image description here

这样的分页工作还是我做错了什么?

 
<?php
//1. database connect
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "3edcvfr4";
$dbname = "dw_bookstore";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

//connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//change utf8
if (!mysqli_set_charset($connection, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($connection));
} else {
    //printf("set na: %s\n", mysqli_character_set_name($connection));
}
?>
<?php
// Number of records to show per page:
$display = 5;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.

    $pages = $_GET['p'];

    } else { // Need to determine.

    // Determine how many pages there are...
    $query = "SELECT COUNT(id) FROM photographs";
    $result = mysqli_query ($connection, $query);
    $row = mysqli_fetch_array ($result, MYSQLI_NUM);
    $records = $row[0];

    // Calculate the number of pages...
    if ($records > $display) { // More than 1 page.
        $pages = ceil ($records/$display);
    } else {
        $pages = 1;
    }

} // End of p IF.


// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}

// Determine the sort...
// Default is by id.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
// Determine the sorting order:
switch ($sort) {
    case 'ln':
        $order_by = 'filename ASC';
        break;
    case 'fn':
        $order_by = 'size ASC';
        break;
    case 'rd':
        $order_by = 'nazwa ASC';
        break;
    default:
        $order_by = 'id ASC';
        $sort = 'rd';
        break;
}

// Make the query:
    $query  = "SELECT id, filename, size, type, nazwa, kod "; 
    $query .= "FROM photographs "; 
    $query .= "WHERE visible = 1 ";
    $query .= "ORDER BY $order_by LIMIT $start, $display ";     
    $result = mysqli_query ($connection, $query);

?>
<?php
    // 2. Perform database query
    $query2  = "SELECT * ";
    $query2 .= "FROM photographs ";
    $query2 .= "WHERE visible = 1 ";
    $query2 .= "ORDER BY nazwa ASC ";
    $result2 = mysqli_query($connection, $query2);
    // Test if there was a query error
    if (!$result2) {
        die("Database query failed.");
    }


?>


<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>List_n01</title>
<link href="../../stylesheets/main_2.css" rel="stylesheet" type="text/css" media="screen, projection" />
</head>

<body>
<div id="wrapper">
  <div id="header">
    <h2>Cennik: Panel Administracyjny</h2>
  </div>

  <div id="mainContent">
  <h1>Graphic Design</h1>
  <?php

  // Count the number of returned rows:
$num = mysqli_num_rows($result2);

if ($num > 0) { // If it ran OK, display the records.
    // Print how many rows there are:
    echo "<p>W bazie znajduje się $num pozycji.</p>\n"; ?>

    <table id="article">
          <caption></caption>
          <colgroup>
          </colgroup>
          <tr>
                <th><a href="list_photos_5.php?sort=ln">id</a></th>
                <th>filename</th>
                <th>size</th>
                <th>type<a href="list_photos_5.php?sort=fn">Nazwa:</a></th>
                <th>name</th>


          </tr>

    <?php

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        echo '<tr>
        <td align="left">' . $row['id'] . '</td>
        <td align="left">' . $row['filename'] . '</td>
        <td align="left">' . $row['size'] . '</td>
        <td align="left">' . $row['type'] . '</td>
        <td align="left">' . $row['nazwa'] . '</td>
    </tr>
    ';
} // End of WHILE loop.

echo '</table>';
?>
    <?php
    // 4. Release returned data
     mysqli_free_result($result);   

} else { // If no records were returned.
    echo '<p class="error">There are currently no rows.</p>';

}

?>
<?php
// Make the links to other pages, if necessary.
if ($pages > 1) {

    echo '<br /><p>';
    $current_page = ($start/$display) + 1;

    // If it's not the first page, make a Previous button:
    if ($current_page != 1) {
        echo '<a href="list_photos_5.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> ';
    }

    // Make all the numbered pages:
     for ($i = 1; $i <= $pages; $i++) {    
        if ($i != $current_page) {
            $distance = $current_page - $i;
            if (abs($distance) < 5){
                echo '<a href="list_photos_5.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
            } 
        } else {
            echo $i . ' ';
        }
    } // End of FOR loop

    // If it's not the last page, make a Next button:
    if ($current_page != $pages) {
        echo '<a href="list_photos_5.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>';
    }

    echo '</p>'; // Close the paragraph.

} // End of links section.
?>

  </div>

  <div id="footer">
<p>Copyright <?php echo date("Y",  time()); ?>, Unixlab</p></div>
  </div>
</body>
</html>
<?php
  // 5. Close database connection
  mysqli_close($connection);
?>

1 个答案:

答案 0 :(得分:0)

这是你的罪魁祸首:

//确定有多少页面... if(isset($ _ GET ['p'])&amp;&amp; is_numeric($ _ GET ['p'])){//已经确定。

$pages = $_GET['p'];

基本上您使用以前设置的页面计数。只需在每次加载页面时重新运行页面数量,或者接受在数据库发生更新时,某些用户可能无法直接看到新的页面数量。