【发布时间】:2019-08-12 06:10:10
【问题描述】:
我有一些代码可以通过单击标题对 php 结果表进行排序。我在此代码中添加了过滤功能,因此我可以过滤结果,然后我希望能够对归档的结果进行排序。我可以在第一次填充表格时对表格进行排序,然后我可以过滤结果,但我无法对过滤后的结果进行排序。表单操作approved_vendor_criteria_CopyA_filter 与代码相同,唯一的例外是在while($row = mysqli_fetch_array($result)) 我将$result 更改为$search_result 并且我可以过滤,但是当我对过滤后的结果进行排序时,表格会重新填充所有未过滤的结果。
我已尝试调整代码,但无法使其正常工作。
<form action="approved_vendor_criteria_CopyA_filter.php" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `approved_vendor_criteria` WHERE CONCAT(`RM_Code`, `Status`, `COA_Description`, `Distributor_Name`, `Distributor_ID`, `Distributor_Code`, `MFG_Name`, `MFG_ID`, `MFG_Code`, `Container_Type`, `Container_Capacity`, `Container_Capacity_Units`, `Approved_Vendor_Criteria_Table_ID`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `approved_vendor_criteria`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "password", "db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
$conn = mysqli_connect("localhost", "root", "password", "db");
$orderBy = "RM_Code";
$order = "asc";
if(!empty($_GET["orderby"])) {
$orderBy = $_GET["orderby"];
}
if(!empty($_GET["order"])) {
$order = $_GET["order"];
}
$RM_CodeNextOrder = "asc";
$StatusNextOrder = "asc";
$COA_DescriptionNextOrder = "desc";
$Distributor_NameNextOrder = "desc";
$Distributor_IDNextOrder = "desc";
$Distributor_CodeNextOrder = "desc";
$MFG_NameNextOrder = "desc";
$MFG_IDNextOrder = "desc";
$MFG_CodeNextOrder = "desc";
$Container_TypeNextOrder = "desc";
$Container_CapacityNextOrder = "desc";
$Container_Capacity_UnitsNextOrder = "desc";
$Approved_Vendor_Criteria_Table_IDNextOrder = "desc";
if($orderBy == "RM_Code" and $order == "asc") {
$RM_CodeNextOrder = "desc";
}
if($orderBy == "Status" and $order == "asc") {
$StatusNextOrder = "desc";
}
if($orderBy == "COA_Description" and $order == "desc") {
$COA_DescriptionNextOrder = "asc";
}
if($orderBy == "Distributor_Name" and $order == "desc") {
$Distributor_NameNextOrder = "asc";
}
if($orderBy == "Distributor_ID" and $order == "desc") {
$Distributor_IDNextOrder = "asc";
}
if($orderBy == "Distributor_Code" and $order == "desc") {
$Distributor_CodeNextOrder = "asc";
}
if($orderBy == "MFG_Name" and $order == "desc") {
$MFG_NameNextOrder = "asc";
}
if($orderBy == "MFG_ID" and $order == "desc") {
$MFG_IDNextOrder = "asc";
}
if($orderBy == "MFG_Code" and $order == "desc") {
$MFG_CodeNextOrder = "asc";
}
if($orderBy == "Container_Type" and $order == "desc") {
$Container_TypeNextOrder = "asc";
}
if($orderBy == "Container_Capacity" and $order == "desc") {
$Container_CapacityNextOrder = "asc";
}
if($orderBy == "Container_Capacity_Units" and $order == "desc") {
$Container_Capacity_UnitsNextOrder = "asc";
}
if($orderBy == "Approved_Vendor_Criteria_Table_ID" and $order == "desc") {
$Approved_Vendor_Criteria_Table_IDNextOrder = "asc";
}
$sql = "SELECT * from approved_vendor_criteria ORDER BY " . $orderBy . " " . $order;
$result = mysqli_query($conn,$sql);
?>
<html>
<head>
<title>Approved Vendor Criteria</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
.table-content{border-top:#CCCCCC 4px solid; width:50%;}
.table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;}
.table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;}
.column-title {text-decoration:none; color:#09f;}
</style>
</head>
<body>
<div class="demo-content">
<h2 class="title_with_link">Approved Vendor Criteria</h2>
<form name="frmSearch" method="post" action="">
<?php if(!empty($result)) { ?>
<table class="table-content">
<thead>
<tr>
<th width="10%"><span><a href="?orderby=RM_Code&order=<?php echo $RM_CodeNextOrder; ?>" class="column-title">RM_Code</a></span></th>
<th width="10%"><span><a href="?orderby=Status&order=<?php echo $StatusNextOrder; ?>" class="column-title">Status</a></span></th>
<th width="30%"><span><a href="?orderby=COA_Description&order=<?php echo $COA_DescriptionNextOrder; ?>" class="column-title">COA_Description</a></span></th> <th width="20%"><span><a href="?orderby=Distributor_Name&order=<?php echo $Distributor_NameNextOrder; ?>" class="column- title">Distributor_Name</a></span></th>
<th width="10%"><span><a href="?orderby=Distributor_ID&order=<?php echo $Distributor_IDNextOrder; ?>" class="column-title">Distributor_ID</a></span></th>
<th width="10%"><span><a href="?orderby=Distributor_Code&order=<?php echo $Distributor_CodeNextOrder; ?>" class="column- title">Distributor_Code</a></span></th>
<th width="10%"><span><a href="?orderby=MFG_Name&order=<?php echo $MFG_NameNextOrder; ?>" class="column-title">MFG_Name</a></span></th>
<th width="10%"><span><a href="?orderby=MFG_ID&order=<?php echo $MFG_IDNextOrder; ?>" class="column-title">MFG_ID</a></span></th>
<th width="10%"><span><a href="?orderby=MFG_Code&order=<?php echo $MFG_CodeNextOrder; ?>" class="column-title">MFG_Code</a></span></th>
<th width="10%"><span><a href="?orderby=Container_Type&order=<?php echo $Container_TypeNextOrder; ?>" class="column-title">Container_Type</a></span></th>
<th width="10%"><span><a href="?orderby=Container_Capacity&order=<?php echo $Container_CapacityNextOrder; ?>" class="column- title">Container_Capacity</a></span></th>
<th width="10%"><span><a href="?orderby=Container_Capacity_Units&order=<?php echo $Container_Capacity_UnitsNextOrder; ?>" class="column- title">Container_Capacity_Units</a></span></th>
<th width="10%"><span><a href="?orderby=Approved_Vendor_Criteria_Table_ID&order=<?php echo $Approved_Vendor_Criteria_Table_IDNextOrder; ?>" class="column- title">Approved_Vendor_Criteria_Table_ID</a></span></th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["RM_Code"]; ?></td>
<td><?php echo $row["Status"]; ?></td>
<td><?php echo $row["COA_Description"]; ?></td>
<td><?php echo $row["Distributor_Name"]; ?></td>
<td><?php echo $row["Distributor_ID"]; ?></td>
<td><?php echo $row["Distributor_Code"]; ?></td>
<td><?php echo $row["MFG_Name"]; ?></td>
<td><?php echo $row["MFG_ID"]; ?></td>
<td><?php echo $row["MFG_Code"]; ?></td>
<td><?php echo $row["Container_Type"]; ?></td>
<td><?php echo $row["Container_Capacity"]; ?></td>
<td><?php echo $row["Container_Capacity_Units"]; ?></td>
<td><?php echo $row["Approved_Vendor_Criteria_Table_ID"]; ?></td>
</tr>
<?php
}
?>
<tbody>
</table>
<?php } ?>
</form>
</div>
</body></html>
【问题讨论】:
-
在您使用的查询中添加
order by可能会有所帮助...