dopod0901 2013-10-21 13:23
浏览 19
已采纳

too long

hello I try to combine the two scripts from the book PHP 6 and MySQL 5 for Dynamic Web Sites. I did search and pagination, but when I go to the next page - did not work. I posted two screenshots below. if someone could show me how I make a mistake I will be grateful.

enter image description hereenter image description here

<?php require_once("../../includes/functions_2.php"); ?>


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

//sprawdzenie polaczenia
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}

//zmaiana znako na utf8
if (!mysqli_set_charset($connection, "utf8")) {
    printf("Error loading character set utf8: %s
", mysqli_error($connection));
} else {
    //printf("Kodowanie ustawione na: %s
", mysqli_character_set_name($connection));
}
?>


<?php
// Number of records to show per page:
$display = 3;

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


    $pages = $_GET['p'];

    } else { // Need to determine.


    @$query = $_GET['query'];

    $query4  = "SELECT COUNT(id) "; 
    $query4 .= "FROM photographs ";
    $query4 .= "WHERE `nazwa` LIKE '%".$query."%' ";
    //$query .= "WHERE visible = 1 ";
    $result = @mysqli_query ($connection, $query4);
    $row = @mysqli_fetch_array ($result, MYSQLI_NUM);
    $records = $row[0];

    // Count the number of records:
    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;
}

// Make the query:
    @$query = $_GET['query'];

    $query3  = "SELECT * "; 
    $query3 .= "FROM photographs "; 
    $query3 .= "WHERE `nazwa` LIKE '%".$query."%' ";
    $query3 .= "OR `kod` LIKE '%".$query."%' ";
    //$query .= "AND visible = 1 ";
    $query3 .= "ORDER BY id ASC LIMIT $start, $display ";       
    $result3 = mysqli_query ($connection, $query3);


?>

<?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>
"; ?>

   <form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="GET">
     <fieldset>
   <ul class="formList">
      <li>
        <input type="text" name="query" placeholder="Szukana fraza... " />
        <input type="submit" value="Search" />
     </li>
      </fieldset>
    </form>

    <table id="article">
      <caption></caption>
          <colgroup>
          </colgroup>
          <tr>
                <th><a href="list_photos_41.php?sort=ln">Zdjęcie:</a></th>
                <th>Typ:</th>
                <th>Wielkość:</th>
                <th>Nazwa:<a href="list_photos_41.php?sort=fn">Nazwa:</a></th>
                <th>Kod:</th>
                <th>Edytuj:</th>
                <th>Szczegóły:</th>
                <th>Usuń:</th>
          </tr>

    <?php
            // 3. Use returned data (if any)
            while($row = mysqli_fetch_assoc($result3)) {
            // output data from each row
        ?>
        <tr>
              <td><img src="../images/<?php echo $row['filename']; ?>" width="150" class="article" /></td> 
              <td><?php echo $row['type']; ?></td>
              <td><?php echo size_as_kb($row['size']); ?></td>
              <td><?php echo $row['nazwa']; ?></td>
              <td><?php echo $row['kod']; ?></td>
              <td><a href="photo_update.php?id=<?php echo $row['id']; ?>">Edytuj</a></td>
              <td><a href="list_photos_detail.php?id=<?php echo $row['id']; ?>">Detale</a></td>
              <td><a href="delete_photo.php?id=<?php echo $row['id']; ?>"onclick="return confirm('Czy napewno chcesz usunąć? (Ta opcja usuwa zdjęcie z danymi z bazy)');">{Usuń}</a></td>
       </tr>


      <?php
            }
        ?> 
</table>

    <?php
    // 4. Release returned data
     mysqli_free_result($result3);  

} 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_43.php?s=' . ($start - $display) . '&p=' . $pages .'">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_43.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $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_43.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }

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

} // End of links section.
?>

  </div>

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

1条回答 默认 最新

  • doq1969 2013-10-21 13:33
    关注

    you are facing issues because in your second url, the query parameter is missing, you should have also have the query=car parameter in get as the data that is been searched is searched with that parameter according to the script...

    Change code from around line 184-204 to the following

    // If it's not the first page, make a Previous button:
    if ($current_page != 1) {
        echo '<a href="list_photos_43.php?s=' . ($start - $display) . '&p=' . $pages .'&query='.$_GET['query'].'">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_43.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&query='.$_GET['query'].'">' . $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_43.php?s=' . ($start + $display) . '&p=' . $pages . '&query='.$_GET['query'].'">Next</a>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答