简体   繁体   中英

Search results pagination logic in PHP

I am developing a search site which will enable users to search for their favorite restaurants in the city. My database is setup and my search page is setup. I was also able to get the search results returned. However, I am having trouble with pagination logic and unable to get the results. I suspect the MySQL query needs to be tweaked but I am at a loss on what the changes are.

Here is my Search.php code:

<title>Results</title> <link href='./style.css' type='text/css' />
<style>b,p {
    font-family: 'Segoe UI Web Light', 'Segoe UI Light', 'Segoe UI Web Regular', 'Segoe       UI', 'Segoe UI Symbol', 'Helvetica Neue',
Arial; color:#000; font-size:18px;

}

body { font-family: 'Segoe UI  Light', 'Segoe UI Light', 'Segoe UI Web
Regular', 'Segoe UI', 'Segoe UI Symbol', 'Helvetica Neue', Arial;
color:#000; font-size:22px;

} a { text-decoration:none; color:#47a3da} a:hover {
text-decoration:underline; color:black }
* { margin:0px; padding:0px }

ol.timeline   { list-style:none}ol.timeline li{
position:relative;border-bottom:2px #dedede dashed; padding:12px;
}ol.timeline li:first-child{}     .morebox    {   font-weight:light;
  color:#333333;  text-align:center;  border:solid 1px #333333;
  padding:9px;    margin-top:9px;     margin-bottom:9px;
  -moz-border-radius: 7px;-webkit-border-radius: 7px;     }   .morebox a{
color:#333333; text-decoration:none}  .morebox a:hover{ color:#00688B;
text-decoration:none}
#container{margin-left:55px; width:780px } </style>

<?php

$start = 0; $per_page = 4;

if(!isset($_POST['query'])) {

$page = 1;

}  

else {

$page = $_POST['query'];

}

if($page<=1) {

$start = 0; 

}

else {

$start = $page * $per_page - $per_page;

}

####### databse connection  ########   $con = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $con) or die("no"); $sql = "SELECT
hotelname,address,comments,preview,cuisname FROM database.Hotel INNER
JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id INNER JOIN
Cuisine ON Cuisine.id = HotelCuisine.cuisine_id WHERE keywords LIKE
'%$page%'";

$num_rows = mysql_num_rows(mysql_query($sql)); $num_pages =
ceil($num_rows / $per_page); $sql .= " LIMIT $start, $per_page";

?>

<body>

<?php

//####### Fetch Results From Table ########

$result = mysql_query($sql); while($row = mysql_fetch_array($result))
{ $hotelname = $result['hotelname'];  $address = $result['address']; 
$comments = $result['comments'];  $preview = $result['preview']; 

?>

<?php

####### echo the result from table ########

echo "<br>Name: <b>$hotelname</b><br>";  echo "Location:
$address<br>";  echo "$comments<br>"; echo "<a
href=$url>$preview</a><br>"; 

}

?>

</body>

<?php

####### Math OF +1 and -1 for the page ########

$prev = $page - 1; $next = $page + 1;

echo"<hr>"; echo "<a href='?page=$prev'>prev</a> "; echo " <a
href='?page=$next'>next</a> ";

?>

First, are you sure the code you pasted here is the one when you echo the $sql? From my understanding, the query should be

SELECT hotelname,address,comments,preview,cuisname FROM database.Hotel INNER JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id INNER JOIN Cuisine ON Cuisine.id = HotelCuisine.cuisine_id WHERE keywords LIKE '%biryani%' LIMIT 0, 4

Here 0 is the start index of the result and 4 is the number of rows return.

Also, your code is not efficient when you retrieve the number of rows. This one can be optimized.

If I am going ti implement it, I would use below logic:

<?php
$start = 0; 
$per_page = 4;

if(!isset($_POST['page'])) {
    $page = 1;
} else {
    $page = intval($_POST['page']);
}


####### databse connection  ########   
$con = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $con) or die("no"); 

$sql = "SELECT COUNT(1) AS num_of_rows FROM database.Hotel INNER
        JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id INNER JOIN
        Cuisine ON Cuisine.id = HotelCuisine.cuisine_id WHERE keywords LIKE
        '%$_POST['query']%'";

$num_rows = mysql_fetch_array(mysql_query($sql))["num_of_rows"];
$num_pages = ceil($num_rows / $per_page);

$sql = "SELECT hotelname,address,comments,preview,cuisname FROM database.Hotel INNER
        JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id INNER JOIN
        Cuisine ON Cuisine.id = HotelCuisine.cuisine_id WHERE keywords LIKE
        '%$_POST['query']%' LIMIT ".($page-1)*$per_page.",".$per_page;
$result = mysql_query($sql);
?>
<body>
<?php
//####### Fetch Results From Table ########
while($row = mysql_fetch_array($result)){ 
    $hotelname = $result['hotelname'];  
    $address = $result['address']; 
    $comments = $result['comments'];  
    $preview = $result['preview']; 
    ####### echo the result from table ########
    echo "<br>Name: <b>$hotelname</b><br>";  
    echo "Location: $address<br>";  
    echo "$comments<br>"; 
    echo "<a href=$url>$preview</a><br>"; 
}

####### Math OF +1 and -1 for the page ########
$prev = $page - 1; 
$next = $page + 1;

echo"<hr>"; 
echo "<a href='?page=$prev'>prev</a> "; 
echo " <a href='?page=$next'>next</a> ";
?>
</body>

Again I would suggest you to run the $sql on command line or in phpMyAdmin first to check whether the query is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM