【问题标题】:Printing results to view results over multiple pages [duplicate]打印结果以查看多页结果[重复]
【发布时间】: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>

【问题讨论】:

标签: php html mysql pdo


【解决方案1】:

在您的 SQL 中,使用 LIMIT 子句:

直接来自文档: [LIMIT {[offset,] row_count | row_count OFFSET offset}] https://dev.mysql.com/doc/refman/5.5/en/select.html

所以在你的例子中,你会做这样的事情:

$results_per_page = 24;
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 0;

$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
                       LIMIT :offset, :row_count");

$stmt->bindValue(':search_term', '%' . $search_term . '%');
$stmt->bindValue(':offset', ($current_page * $results_per_page));
$stmt->bindValue(':row_count', $results_per_page);
$stmt->execute();

使用$_GET 变量来改变页面: http://localhost/?page=2

关于总结果的计数,您必须运行另一个查询来选择该搜索查询的所有结果,但只返回 COUNT(*) 输出:

SELECT COUNT(*) 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

【讨论】:

  • 如果你明白我的意思,在这种情况下你将如何使用 $_GET 来更改同一页面上的页面
  • 如果我正确理解了您的问题,您将使用$_GET['page'] 变量来查看当前页面是否是该页面,并且不要使该链接可点击。如果这就是您问题的答案,请详细说明。
  • 我正在尝试显示例如每页 1 个结果,因此结果 1 在第 1 页上,然后如果我单击链接 2,它将带我到同一页面,但有第二个结果,除了对我来说它看起来很好并且有效
  • 如果您想更改每页的结果数,只需将 $results_per_page 变量更改为 1(我已更新变量名称以使其更易于理解)。
猜你喜欢
  • 1970-01-01
  • 2014-04-01
  • 2016-10-31
  • 1970-01-01
  • 2021-12-15
  • 2021-03-18
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多