搜索分页,但搜索不起作用

时间:2015-05-01 05:12:30

标签: php

我有test3.php的代码,通过分页和搜索,分页工作正常,但搜索无效。我不知道代码中的问题是什么。有人可以帮我解决我的搜索功能。我在互联网上搜索过,但我无法找到解决问题的方法。请帮帮我。谢谢。

  
<html>
<head>
<title>Pagination</title>



<body>

<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>

<?php

mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("region_survey") or die (mysql_error());

$sql = mysql_query("SELECT * FROM municipality ORDER BY id ASC");

$nr = mysql_num_rows($sql);  
if (isset($_GET['pn'])) { 
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else { 
$pn = 1;
} 

$itemsPerPage = 10; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {
$pn = 1;
}   else if ($pn > $lastPage) { 
$pn = $lastPage; 
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
}   else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    }

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("SELECT * FROM municipality ORDER BY id ASC $limit"); 

$paginationDisplay = ""; 

if ($lastPage != "1"){

$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

if ($pn != 1) {
    $previous = $pn - 1;
$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '? pn=' . $previous . '"> Back</a> ';
} 

$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

if ($pn != $lastPage) {
    $nextPage = $pn + 1;
$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?  pn=' . $nextPage . '"> Next</a> ';
} 
}

$outputList = '';

?>
<div id="tables">
<table>

    <tr>
        <th>LO Name</th>
        <th>Province</th>
        <th>Location</th>
        <th>Action</th>
        <th>Update</th>
    </tr>


<?php

while($row = mysql_fetch_array($sql2)) {
    $id=$row['id'];
?>

    <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
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("region_survey", $con);

$result = mysql_query("select count(1) FROM municipality");
$row = mysql_fetch_array($result);


$total = $row[0];
echo "Total rows: " . $total;

mysql_close($con);
?>

<br />
<br />
<?php echo $paginationDisplay; ?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您尚未在查询

下添加查询将在您的数据库中使用的关键字

mysql_query("SELECT * FROM municipality where <your-field-name> LIKE %".$_POST['<your input box name>']." ORDER BY id ASC");

希望这能帮助您解决问题