如何使用下拉菜单

问题描述:

在PHP表从数据库中获取的项目进行排序我有一个从数据库不同的标题取像timenamesenderstateState Count等由降time订购物品的表。我希望客户端通过从下拉菜单中选择一个选项并在页面加载时使用默认选项按升序或降序动态地对项目进行排序。如何使用下拉菜单

Time | Name | State | State Count | District | District Count 
    -----|------|-------|---------------------------------------- 
    Val1 | Val2 | Val3 | Val4  | Val5  | Val6  
    -----|------|-------|-------------|----------|-------------- 
    Val7 | Val8 | Val9 | Val10  | Val11 | Val12  

当我试图做到这一点时,它没有给出正确的结果。这是否是因为期权价值的空间?我不希望使用Ajax,jQuery和我新的PHP

这是我编辑的代码 -

$sql="SELECT s.time,s.code,p.name,p.state,p.district,p.assembly from Table1 
AS s INNER JOIN Table2 AS p ON s.sender=p.sender"; 

$result = mysqli_query($con,$sql); 

while($row = mysqli_fetch_array($result)) 
{ 

$opcodey1= ($row['state']); 
$opcodey2= ($row['district']); 

$sql01="SELECT state from Table2 AS r INNER JOIN Table1 AS s ON 
s.sender=r.sender and r.state='$opcodey1'"; 

$result01 = mysqli_query($con,$sql01); 
$num01=mysqli_num_rows($result01); 

$sql02="SELECT state from Table2 AS r INNER JOIN Table1 AS s ON 
s.sender=r.sender and r.district='$opcodey2'"; 

$result02 = mysqli_query($con,$sql02); 
$num02=mysqli_num_rows($result02); 
} 

echo '<form action="" method="post">'; 
echo '<select id="category" name="category">'; 
echo '<option value="s.time desc">-S.TIME DESC-</option>'; 

echo '<option value="s.time asc">-S.TIME asc-</option>'; 
echo '<option value="r.state asc">-State asc-</option>'; 
echo '<option value="$num01 asc">-State Count ASC-</option>'; 
echo '<option value="r.district asc">-District asc-</option>'; 
echo '<option value="$num02 asc">-District Count ASC-</option>'; 
echo '<br>'; 
echo '</select>'; 

echo '<input type="submit" name="submit" value="submit" >'; 
echo '<input type="button" value="Reset"  
onClick="window.location.href=window.location.href">'; 

echo '</form>'; 
echo '<br>'; 
if(isset($_POST['category'])) 
{ 
    $issue_opn=$_POST['category']; 
} 
echo "<table> 
<tr> 
<th>Time</th> 
<th>Name</th> 
<th>State</th> 
<th>State Count</th> 
<th>District</th> 
<th>District Count</th> 
</tr>"; 
    $sqla="SELECT s.time,p.name,p.state,p.district  
      from Table1 AS s 
      INNER JOIN Table2 AS p ON s.sender=p.sender 
      order by '.$issue_opn.'"; 
    $resulta = mysqli_query($con,$sqla); 

while($row = mysqli_fetch_array($resulta)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['time'] . "</td>"; 
echo "<td>" . $row['name'] . "</td>"; 
echo "<td>" . $row['state'] . "</td>"; 
echo '<td>.$num01."</td>"; 
echo "<td>" . $row['district'] . " </td>"; 
echo '<td>.$num02."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
+0

第一个问题'秩序 “;'应该是'ORDER BY $ issue_opn”;'用单引号 – RiggsFolly

+1

以供将来参考你不会换列名 '$ issue_opn。' ** **调试回应查询并尝试通过phpMyAdmin或类似的工具运行它。或者,检查所有对数据库API的调用的错误状态。我希望你使用'mysqli_''或PDO而不是'mysql_' API – RiggsFolly

+0

感谢您的回应。请尝试给出答案@RiggsFolly – user20152015

你需要如下AJAX使用。按照以下方式尝试。

<select id="category" name="category" onChange="showUser(this.value)"> 
<script> 
function showUser(str) 
{ 
    if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 

if (window.XMLHttpRequest) 
{ 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 

xmlhttp.onreadystatechange = function() 
{ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
} 
    xmlhttp.open("GET","get_data.php?q="+str,true); 
    xmlhttp.send(); 
} 
</script> 

//get_data.php 
<?php 
include('conection.php'); 
$q=$_GET["q"]; 
$sql="SELECT s.time,p.name,p.state,p.district  
      from Table1 AS s 
      INNER JOIN Table2 AS p ON s.sender=p.sender 
      order by '.$q.'"; 

$result = mysql_query($sql); 

echo "Your data <select>"; 
while($row = mysql_fetch_array($result)) 
{ 
    echo '<option value="s.time asc">-S.TIME asc-</option>'; 
} 
echo "</select>"; 
?> 
通过
+0

你没看过这个问题_我不想使用Ajax,jQuery和我是新来的php_ – RiggsFolly