【发布时间】:2016-04-05 15:26:40
【问题描述】:
我想编写一个带有搜索排序和分页的表格。 这三个操作都应该相互连接。 我写了一个用于搜索和分页的代码 但我无法在其中添加排序逻辑 谁能指导一个简单的方法来执行这个 这是我的代码
<?php
include 'dbconfig.php';
$records_per_page=10;
$name = "";
$email= "";
$queryCondition = "";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("name","email");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "name":
$name = $v;
$queryCondition .= "username LIKE '" . $v . "%'";
break;
case "email":
$email = $v;
$queryCondition .= "user_email LIKE '" . $v . "%'";
break;
}
}
}
}
//if(isset($_POST["order"]))
//{
// $order=$_POST["order"];
//
//}
// else{
// $order="user_id";
//}
//if(isset($_POST["by"])){
// $by=$_POST["by"];
//if($by=="asc" & !isset($_POST["page"])){
// $by="desc";
//
// }else if($by=="desc" & !isset($_POST["page"]))
// {
//
// $by="asc";
//
// }
// else
// {
// $by=$_POST["by"];
// }
//}
//else
//{
// $by="asc";
//}
$orderby = " ORDER BY $order $by";
$sql = "SELECT * FROM tbl_register " . $queryCondition;
$href = 'demo2.php';
$perPage = 10;
$page = 1;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . $orderby . " limit " . $start . "," . $perPage;
// $result = $db_handle->runQuery($query);
$result = mysql_query($query);
while($row=mysql_fetch_array($result)) {
$resultset[] = $row;
}
if(!empty($resultset)) {
$resultset["perpage"] = showperpage($sql, $perPage, $href,$order,$by);
}
?>
<html>
<head>
</head>
<body>
<div id="user-grid">
<form name="frmSearch" method="post" action="demo2.php">
<div class="search-box">
<p><input type="text" placeholder="Name" name="search[name]" class="demoInputBox" value="<?php echo $name; ?>" /><input type="text" placeholder="Email" name="search[email]" class="demoInputBox" value="<?php echo $email; ?>" /><input type="submit" name="go" class="btnSearch" value="Search"><input type="reset" class="btnSearch" value="Reset" onclick="window.location='demo2.php'"></p>
</div>
<table id="tbl">
<thead>
<tr>
<!-- <th><a href='demo2.php?by=<?php// echo $by; ?>&order=user_id'>Id</a></th>
<th><a href='demo2.php?by=<?php// echo $by; ?>&order=username'>UserName</a></th>
<th><a href='demo2.php?by=<?php// echo $by; ?>&order=user_email'>Email</a></th>
<th><a href='demo2.php?by=<?php// echo $by; ?>&order=user_contact'>Phno.</a></th>-->
<th><input type="submit" name="id" value="user_no" class="btn-link"/></th>
<th><input type="submit" name="username" value="username" class="btn-link"/></th>
<th><input type="submit" name="user_email" value="user_email" class="btn-link"/></th>
<th><input type="submit" name="user_contact" value="user_contact" class="btn-link"/></th>
</tr>
</thead>
<tbody>
<?php
foreach($resultset as $k=>$v) {
if(is_numeric($k)) {
?>
<tr>
<td><?php echo $resultset[$k]["user_no"]; ?></td>
<td><?php echo $resultset[$k]["username"]; ?></td>
<td><?php echo $resultset[$k]["user_email"]; ?></td>
<td><?php echo $resultset[$k]["user_contact"]; ?></td>
</tr>
<?php
}
}
if(isset($resultset["perpage"])) {
?>
<tr>
<td colspan="6" align=right> <?php echo $resultset["perpage"]; ?></td>
</tr>
<?php } ?>
<tbody>
</table>
</form>
</div>
</body>
</html>
<?php
function perpage($count, $per_page = '10',$href) {
$output = '';
if(!isset($_POST["page"])) $_POST["page"] = 1;
if($per_page != 0)
$pages = ceil($count/$per_page);
if($pages>1) {
for($i=1; $i<=$pages; $i++) {
if($_POST["page"] == $i)
$output = $output . '<span id='.$i.' >'.$i.'</span> ';
else
$output = $output . '<input type="submit" name="page" value="' . $i . '" />';
//$output=$output.'<a href="demo2.php?page='.$i.'&by=$by&order=$order">' .$i. ' </a> ';
}
}
return $output;
}
function showperpage($sql, $per_page = 10, $href,$order,$by) {
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$perpage = perpage($count, $per_page,$href);
return $perpage;
}
?>
<style>
#tbl
{
width:500px;
border:1px solid #00a2d1;
margin-top:10px;
}
#tbl tr {
background: #fff
}
#tbl td{
border:1px solid #00a2d1
}
#tbl th
{
background: #00a2d1;
border:1px solid #fff
}
#frmSearch button[type="submit"]{
background-color:#00a2d1;
color:#fff
}
.btn-link{
border:none;
outline:none;
background:none;
cursor:pointer;
color:#0000EE;
padding:0;
text-decoration:underline;
font-family:inherit;
font-size:inherit;
}
</style>
当用户点击表头时,我希望如何在其中添加排序逻辑,它应该在 asc n desc 和每个表头之间切换。
【问题讨论】:
标签: javascript php sorting