搜索和分页不起作用

时间:2013-10-21 13:23:33

标签: php mysql pagination search-engine

你好我尝试将PHP 6和MySQL 5中的两个脚本组合成动态网站。我做了搜索和分页,但是当我进入下一页时 - 没有用。 我在下面发布了两个截图。 如果有人能告诉我如何犯错,我将不胜感激。

enter image description here enter 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\n", mysqli_connect_error());
    exit();
}

//zmaiana znako na utf8
if (!mysqli_set_charset($connection, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($connection));
} else {
    //printf("Kodowanie ustawione na: %s\n", 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>\n"; ?>

   <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 个答案:

答案 0 :(得分:0)

您遇到问题,因为在您的第二个网址中,查询参数丢失,您应该在get中包含 query = car 参数作为数据已根据脚本...

使用该参数搜索已搜索的内容

将代码从第184-204行更改为以下

// 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>';
}