[英]Drop down list to select a table and an additional drop down list to filter
I want to use 2 drop down lists on the same page, one of them to select the table from the Db and the other to filter the selected table. 我想在同一页面上使用2个下拉列表,其中一个从Db中选择表,另一个用于过滤选定的表。 I think this is very basic but I am new in PHP and SQL.
我认为这是非常基本的,但是我是PHP和SQL的新手。 I have no problem to use a submit to filter the table by group but I don't know where assign to
$table
variable the $_POST['tables']
value. 我没有问题使用提交按组过滤表,但是我不知道将
$_POST['tables']
值分配给$table
变量的位置。
Here is the relevant part of my code, : 这是我的代码的相关部分:
$table=table_name;
if(isset($_POST['tables'])) {
$table=$_POST['tables'];
}
//filtering by group
if(true===isset($_POST['value'])) {
if($_POST['value'] == 'All') {
// query to get all records
$query = "SELECT * FROM $table";
} else {
// filter groups
$value=$_POST['value'];
$query = "SELECT * FROM $table WHERE Grupo='$value'";
}
} else {
// the first time the page is loaded
$query = "SELECT * FROM $table ORDER BY Grupo ASC";
}
$sql = mysqli_query($con,$query);
index.php index.php
<html>
<head>
<script>
function sortResult(str)
{
var e = document.getElementById("firstDrop");
var str1 = e.options[e.selectedIndex].value;
var e = document.getElementById("secondDrop");
var str = e.options[e.selectedIndex].value;
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","results.php?q="+str+"&t="+str1,true);
xmlhttp.send();
}
</script>
</head>
<body>
<!--database name-->
<select name="tablename" id="firstDrop">
<option selected='selected' value="yourdatabasename1">DB1</option>
<option value="yourdatabasename2">DB2</option>
</select>
<!--sort by what-->
<select name="sortby" id="secondDrop">
<option selected='selected' value="slno">slno</option>
<option value="name">name</option>
<option value="author">author</option>
</select>
<button id="sub" onclick="sortResult()">CLICK</button>
<br><br>
<div id="result"><b>Results will be listed here.</b></div>
</body>
</html>
results.php results.php
<?php
$q = $_GET['q'];
$t = $_GET['t'];
$con = mysqli_connect('localhost','yourusername','password','database');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"database");
$sql="SELECT * FROM ".$t." ORDER BY ".$q;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Author</th>
<th>ID</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['name']. "</td>";
echo "<td>" .$row['author'] ."</td>";
echo "<td>" .$row['slno']. "</td>";
echo "</tr>";
}
echo "</table>";
echo $t;
mysqli_close($con);
?>
modify accordingly. 相应地修改。
you have a typo... 你有错字...
if(isset($_POST['tables'])) {
$tabla=$_POST['tables'];
}
change to 改成
if(isset($_POST['tables'])) {
$table=$_POST['tables'];
}
Correct your query syntax-- 更正您的查询语法-
$query = "SELECT * FROM $table";
to 至
$query = "SELECT * FROM '".$table."'";
$query = "SELECT * FROM $table WHERE Grupo='$value'";
to 至
$query = "SELECT * FROM '".$table."' WHERE Grupo='".$value."'";
and 和
$query = "SELECT * FROM '".$table."' ORDER BY Grupo";
to 至
$query = "SELECT * FROM '".$table."' ORDER BY Grupo";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.