doupai8533 2015-04-30 05:45
浏览 43

用分页搜索[重复]

I have this search with pagination, the search function is working fine and the pagination is also working fine but my real problem is that I don't know how to merge those two. Every time I try to search, the search result is showing but the pagination is not functioning normal. Please someone help me about this issue. I don't know where to start.

I have this code:

<?php

$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("region_survey", $con);

$sql="SELECT * FROM municipality";




if(isset($_POST['search'])){

$search_term=mysql_real_escape_string($_POST['search_box']);

$sql .= " WHERE province_id LIKE '%{$search_term}%' ";

}

$query=mysql_query($sql) or die (mysql_error());


?>

<form name="search_form" method="POST" action="">

Search:<input type="text" name="search_box" value="" />
<input type="submit" name="search" value="search the table" />

</form>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
//Include the PS_Pagination class
include('ps.php');

//Connect to mysql db
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('region_survey', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM municipality';



//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 15, 17);

//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//Count total number of records after search
mysql_select_db("region_survey", $con);

if(isset($_POST['search'])){

$search_term=mysql_real_escape_string($_POST['search_box']);


$sql="SELECT * FROM municipality WHERE province_id LIKE '%$search_term%'";

}

$result = mysql_query($sql, $con);
$row = mysql_num_rows($result);

echo "Total Number: ";
echo $row;
?>


<table border="1" cellpadding="0" cellspacing="0" id="resultTable">
        <tr>
          <th> <strong>ID</strong> </th>
          <th> <strong>Province ID</strong> </th>
          <th> <strong>Municipality Name</strong> </th>
        </tr>
<?php
while($row = mysql_fetch_array($query))

{
?>
<tr>
<td> <?php echo $row["id"]; ?> </td>
<td> <?php echo $row["province_id"]; ?></td>
<td> <?php echo $row["municipality_name"]; ?></td>
<td><input name="selector[]" type="checkbox"
id="checkbox[]" value="<?php echo $row['id'];?>"></td>
        <td><a href="update.php<?php echo '?id='.$row['id']; ?>">Edit</a>        </td>
</tr>
<?php
}
?>
   </table>    

   <?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</body>
</html>
</div>
  • 写回答

1条回答 默认 最新

  • douzhi4311 2015-04-30 05:50
    关注

    Pages will have URL like example.com/search/term/?page=2 (or example.com/search.php?term=xyz&page=2, doesn't matter if you redirect URLs or not).

    In PHP then will be something like:

    $pagelimit = 10; // records per page
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // set current page
    

    And LIMIT with OFFSET in your SQL query:

    $sql = "SELECT * 
            FROM municipality 
            WHERE province_id LIKE '%$search_term%' 
            LIMIT " . ($page - 1) * $pagelimit . ", " . $pagelimit;
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了