Table_sup |
+-------------------+
| supid=>int |
| scompany=>varchar |
| sstate=>varchar |
| scity=>varchar |
| scat=>varchar |
PHP代码
<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["getData"];
if($des=="") // if ALL is selected in Dropdown box
{
$res=mysqli_query($con,"Select * from sup");
}
else
{
$res=mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</center>
</form>
默认情况下,表格中没有填充数据。 当我按&#34;选择&#34;时,表格中会填充所有数据。
我想要的是:默认情况下,该表填充了所有数据
运行代码后我有一个默认的表格图像。如果需要我可以上传图片
答案 0 :(得分:1)
<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
$res=mysqli_query($con,"Select * from sup");
// If select was clicked, then only where condition applies, and so $res will be replaced.
if(isset($_POST['getData'] ) || !empty($des))
{
$des=$_POST["getData"];
$res = mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</form>