搜索了许多选项以找到一些简单的解决方案,我可以对结果进行排序,但却找不到任何结果。 我想按选定的下拉值排序显示的结果。我不想使用“表格”。想要另一种方式对其进行排序。
<div class="col-lg-2">
<label class="margin-bottom:25px;" style="margin-left:75px;"> Sort by: <label>
</div>
<div class="col-lg-2">
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
<option value="Location">Location</option>
</select>
</div>
以上是我的下拉列表。 以下是我们的$ sql初始查询:
$condition = implode(' AND ', $query);
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition;
现在我们已经尝试过这个,但似乎有些不对劲。
if($_POST['sortby']=="ID")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition." order by candidate.cand_number asc";
}
if($_POST['sortby']=="Name")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition." order by candidate.cand_fname asc";
}
if($_POST['sortby']=="Source")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_source asc";
}
if($_POST['sortby']=="Location")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_source FROM candidate_contact ".$join.' where '.$condition." order by candidate.cand_location asc";
}
asc-按升序排序;
我的jquery / ajax脚本 -
<script>
$(document).ready(function(){
// Each time you change your sort list, send AJAX request
$("#sortby").change(function(){
$.ajax({
method: "POST",
url: "viewcandidate.php",
data: { sortby:$("#sortby").val() }
})
// Copy the AJAX response in the table
.done(function( msg ) {
$("#list").html(msg);
});
});
});
</script>
//even tried with $(window).load(function){.....but no result.
这有什么不对,因为这不适合我?我不想使用表格。建议我一些简单的解决方案。
答案 0 :(得分:2)
首先出现问题...
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Relevance">Name</option><!-- Value ???-->
<option value="Name">Source</option><!-- Value ???-->
</select>
就像
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
</select>
当您比较$ _POST [&#39; sortby&#39;] ==&#34;姓名&#34;和$ _POST [&#39; sortby&#39;] ==&#34;来源&#34;
答案 1 :(得分:0)
尝试在订单前添加空格。
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source
FROM candidate ".$join.' where '.$condition." order by candidate.cand_number" ;
答案 2 :(得分:0)
您的下拉列表是:
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Relevance">Name</option>
<option value="Name">Source</option>
</select>
当您发布下拉列表时,该值将为值=&#34; ...&#34;所选选项的属性,而不是显示的文本。
所以你的排序&#39;条件不好,必须是:
if($_POST['sortby']=="ID")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_number" ;
}
if($_POST['sortby']=="Relevance")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_fname" ;
}
if($_POST['sortby']=="Name")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_Source" ;
}
答案 3 :(得分:0)
@ CodeSniper..try检查一些空格。我相信你会得到解决方案。
答案 4 :(得分:0)
我做了一个小例子,向您展示如何使用jQuery动态重新加载数据。当然,你必须根据你的情况进行调整。
对于这个例子,我使用的是localhost,数据库='test'和一个名为'candidate'的表,有三列:ID,Name和Source。
有两个文件:index.php和request.php(在同一文件夹中)。
index.php文件:
<?php
// Database connection
$db = mysqli_connect('localhost', 'root', '', 'test');
// First request, on load
$sql = "SELECT ID, Name, Source FROM candidate";
$exe = $db->query($sql);
?>
<html>
<head>
<!-- Including jQuery -->
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div>
<!-- Generate table on load -->
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Source</th>
</tr>
</thead>
<tbody id="list">
<?php
while($row = mysqli_fetch_assoc($exe)) {
echo "
<tr>
<td>".$row['ID']."</td>
<td>".$row['Name']."</td>
<td>".$row['Source']."</td>
</tr>";
}
?>
</tbody>
</table>
Sort by :
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
</select>
</div>
<script>
$(document).ready(function(){
// Each time you change your sort list, send AJAX request
$("#sortby").change(function(){
$.ajax({
method: "POST",
url: "request.php",
data: { sortby:$("#sortby").val() }
})
// Copy the AJAX response in the table
.done(function( msg ) {
$("#list").html(msg);
});
});
});
</script>
</body>
</html>
request.php文件:
<?php
$db = mysqli_connect('localhost', 'root', '', 'test');
if(isset($_POST['sortby'])) {
if($_POST['sortby'] == "ID") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY ID";
} else if($_POST['sortby'] == "Name") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY Name";
} else if($_POST['sortby'] == "Source") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY Source";
}
} else {
$sql = "SELECT ID, Name, Source FROM candidate";
}
$exe = $db->query($sql);
while($row = mysqli_fetch_assoc($exe)) {
echo "
<tr>
<td>".$row['ID']."</td>
<td>".$row['Name']."</td>
<td>".$row['Source']."</td>
</tr>";
}
?>
演示:http://maxime-mettey.com/projects/dropdownajax/
下载文件:http://maxime-mettey.com/projects/dropdownajax/dropdownajax.zip