我的代码已经填充了选择下拉列表中的两个不同的值来自mysql列和硬编码。选择tester
时,它会正确显示该tester
的记录。
现在我想在用户选择其中一个硬编码选项(在mysql列中为列名)时在表中显示一些用户定义的列。我是php的新手,所以请你帮忙。
<form name="frmdropdown" method="post" action="emp_dd_display.php">
<h2 align="center">Records</h2>
<strong> Select Option: </strong>
<select id="tester" name="tester" class="tester">
<option value=""> -----------ALL-----------</option>
<option value="machine_details"> Machine Details </option>
<option value="tester"> Tester </option>
<option value="test_event"> Event </option>
<?php
$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<table border="1">
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["tester"];
if($des=="") // if ALL is selected in Dropdown box, show all columns
{
$res=mysqli_query($conn, "Select * from workflow1");
//$res=mysqli_query($conn, "Select date1, tester from workflow1");
echo "<tr align=center>";
echo "<th>Sr. No. </th>";
echo "<th>Date</th>";
echo "<th>Tester</th>";
echo "<th>Machine Details</th>";
echo "<th>Event</th>";
echo "</tr>";
while($r=mysqli_fetch_row($res))
{
echo "<tr>";
echo "<td align=center>$r[0]</td>";
echo "<td width=200>$r[1]</td>";
echo "<td alig=center width=40> $r[3]</td>";
echo "<td align=center width=200>$r[4]</td>";
echo "<td width=100 align=center>$r[5]</td>";
echo "</tr>";
}
}
else
{
$res=mysqli_query($conn, "Select date1, tester from workflow1 where tester='".$des."'");
echo "<tr align=center>";
echo "<th>Emp_Id </th>";
echo "<th>Emp_name </th>";
echo "</tr>";
}
while($r=mysqli_fetch_row($res))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td align='center'>$r[1]</td>";
echo "</tr>";
}
}
?>
</table>
</center>
</form>
期望的输出:
----------------------------------------------------------
| Sr. NO | Date | Tester | Machine Details| Event |
|10-05-2017 | testing1| Ganesh | dgrgr | event1|
|10-05-2017 | testing1| Manoj | rewtw | event2|
----------------------------------------------------------
tester
(Ganesh), --------------------
| Date | Tester |
|10-05-2017 | Ganesh |
--------------------
test_event
, ----------------------------------------
| Date | Event | Machine Details|
|10-05-2017 | testing1| abcd |
|10-05-2017 | testing1| ddfe |
----------------------------------------
SQL Fiddle中的示例表数据:http://sqlfiddle.com/#!9/b8bd9/1