【发布时间】:2016-02-29 13:36:01
【问题描述】:
我正在尝试创建一段代码,将我的查询结果限制为例如每页 2 个结果,其余结果可以通过单击页码来查看,这与谷歌的搜索结果完全相同设施,所以如果我想要更多结果,我只需转到第 2 页。如何使用 PhP 完成。没有javascript。
<!DOCTYPE html>
<html>
<head>
<title>Database</title>
<link href="style.css" rel="stylesheet" type="text/css"> <!-- This is linking style sheet (css)into this HTML page-->
<link href='https://fonts.googleapis.com/css?family=PT+Serif:400italic' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="navigation">
<form action="index.php" method="get">
<input type="submit" name="mainpage" value="Main Page" class="submitbut" id="but1" />
</form>
</div>
<form action="index.php" method="post">
<input type="text" name="search" id="searching" />
<input type="submit" name="data_submit" value="Search" id="scan" />
</form>
<?php
if( isset( $_GET['mainpage'] ) ) exit( header( "Location: mainpage.php" ) );
if ( isset( $_POST["data_submit"] ) ){
$search_term = ( $_POST['search']);
if($search_term == ""){
echo "You need to enter a value";
}else{
$conn = new PDO( 'mysql:host=localhost;dbname=u1358595', 'root' );
/*
replace root with this
'u1358595'
'26nov94'
*/
$stmt = $conn->prepare("SELECT * FROM `guest` g
INNER JOIN `booking` b ON g.`guest_id`=b.`guest_id`
INNER JOIN `hotel` h ON b.`hotel_id`=h.`hotel_id`
WHERE g.`last_name` LIKE :search_term OR g.`first_name` LIKE :search_term;");
$stmt->bindValue(':search_term', '%' . $search_term . '%');
$stmt->execute();
$count=($stmt->rowCount());
echo "There are ".$count." results that match your search";
echo "
<table>
<tr>
<th>Guests Matched</th>
</tr>";
while($hotel = $stmt->fetch()) {
echo "
<tr>
<td><a href='details.php?name=".$hotel['last_name']."'>".$hotel['first_name']." ".$hotel['last_name']."</a></td>
</tr>";
}
echo "</table>";
$conn = NULL;
}
}
?>
</body>
</html>
【问题讨论】:
-
请注意,虽然您在生产中使用准备好的语句是绝对正确的,但我更愿意在开发中避免使用它们,以便更容易回显发送到 mysql 的实际查询。