【问题标题】:MySQL PHP Search result with pagination [duplicate]带有分页的MySQL PHP搜索结果[重复]
【发布时间】:2016-04-15 10:20:10
【问题描述】:

使用 php 创建一个新项目:使用搜索功能 (search.php) 从 mysql 数据库中检索数据 - 在结果页面 (find.php) 上的搜索结果中添加分页。问题是,当显示第一个搜索时,它显示了正确的数据;但是,当我单击第二页(分页按钮)时,它会再次填充所有数据 - 而不是我搜索的术语:请帮助!

搜索.php

<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>

find.php

<?php

include_once("config.php");


if(isset($_POST['button'])){ 
    $search=$_POST['search'];
}

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);

if($last < 1){
    $last = 1;
}
$pagenum = 1;

if(isset($_GET['page'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}

if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");  
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";


$paginationCtrls = '';

$self = $_SERVER['PHP_SELF'];

if($last != 1){
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$self.'?page='.$previous.'">Previous</a>';
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>';
            }
        }
    }
    $paginationCtrls .= '<span class="current">'.$pagenum.'</span>';

    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>';
        if($i >= $pagenum+4){
            break;
        }
    }
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= '<a href="'.$self.'?page='.$next.'">Next</a> ';
    }
}

?>

<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>

<?php
while($row = mysql_fetch_array($result)){
?>

<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>

<?php
}

?>

【问题讨论】:

  • 第一个查询不应包含限制。第二个应该包含一个限制和一个偏移量

标签: php mysql search pagination


【解决方案1】:

您必须在分页链接中设置搜索词。试试这个。

<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
<?php

include_once("config.php");


if(isset($_POST['button'])){ 
    $search=$_POST['search'];
}


$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit "); 
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);

if($last < 1){
    $last = 1;
}
$pagenum = 1;

if(isset($_GET['page'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
// get search tearm from url
if(isset($_GET['search'])){ 
    $search=$_GET['search'];
}
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");  
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";


$paginationCtrls = '';

$self = $_SERVER['PHP_SELF'];
// you must have to set search tearm in pagination links
if($last != 1){
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$self.'?page='.$previous.'&search='.$search.'">Previous</a>';
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$self.'?page='.$i.'&search='.$search.'">'.$i.'</a>';
            }
        }
    }
    $paginationCtrls .= '<span class="current">'.$pagenum.'</span>';

    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$self.'?page='.$i.'&search='.$search.'">'.$i.'</a>';
        if($i >= $pagenum+4){
            break;
        }
    }
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= '<a href="'.$self.'?page='.$next.'&search='.$search.'">Next</a> ';
    }
}

?>

<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>

<?php
while($row = mysql_fetch_array($result)){
?>

<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>

<?php
}

?>

【讨论】:

  • 不会工作,POST 的形式,但网址是 GET
  • 这是什么问题?
  • 在使用 POST 进行初始搜索后,搜索词现在位于 GET 全局中以供未来页面使用
  • @Dagon Just as I said 在上面的 cmets 中,但没有人在听我说话。也许他们会听你的;-)
  • @Fred-ii- 抱歉没有注意到你 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 2012-07-31
相关资源
最近更新 更多