搜索结果中的分页

时间:2015-10-26 12:13:50

标签: php mysql pagination

我想在搜索结果中进行分页。我有一页,有两个表用于搜索,第二个用于搜索结果。

    <form method="get">
<table align="center" border="1" cellpadding="10" cellspacing="10">
<tr>
<td>Username</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="search" value="search">
</td>
</tr>
</table>
<table align="center" border="1" cellpadding="10" cellspacing="10">
<tr>
<th>User</th>
<th>Address</th>
</tr>
<?php
while($ft=$pge->fetch_object())
{
?>
<tr>
<td><?php echo $ft->user ?></td>
<td><?php echo $ft->address ?></td>
</tr>
<?php
}
?>
</table>
<center>
<?php
$totalrecords=$totalrecords/2;
for($i=0;$i<$totalrecords;$i++)
{
?>
<a href="pagination.php?pgid=<?php echo $i ?>"><?php echo $i+1  ?></a>
<?php
}
?>
</center>
</form>

默认我已应用分页但当我想通过搜索找到用户详细信息时我也想在搜索结果中进行分页。 但是我还没有完成。这是我的分页代码。

 require('connect.php');
 $sel="select * from submit";
 $ex=$cn->query($sel) or die($cn->error);
 //$ft=$ex->fetch_object();
 $totalrecords=$ex->num_rows;


 if(isset($_REQUEST['pgid']))
 {
   $pgid=$_REQUEST['pgid'];
   $pgn=$pgid*2;
   $pg="select * from submit limit $pgn,2";
   $pge=$cn->query($pg) or die($cn->error);

 }
 else
 {
   $pg="select * from submit limit 0,2";
   $pge=$cn->query($pg) or die($cn->error);
 }

 if(isset($_REQUEST['search']))
 {
  $u=$_REQUEST['user'];

  $searchquery="select * from submit where user like '%$u%'";
  $pge=$cn->query($searchquery) or die($cn->query);

 }

1 个答案:

答案 0 :(得分:1)

创建自定义MySQL查询尝试此代码: -

$pg="select * from submit where 1=1";
if(isset($_REQUEST['search']))
 {
  $u=$_REQUEST['search'];
  $pg.=" and user like '%$u%'";
 }
if(isset($_REQUEST['pgid']))
 {
   $pgid=$_REQUEST['pgid'];
   $pgn=$pgid*2;
   $pg.=" limit $pgn,2";
 }
 else
 {
   $pg.=" limit 0,2";
 }

 $pge=$cn->query($pg) or die($cn->error);