栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

仅在从下拉菜单中选择值时才显示结果

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

仅在从下拉菜单中选择值时才显示结果

在各

<option>
部分中,只需修改一下即可。添加
selected
条件:

<div >    <div >        <div > <?php     $servername = "localhost";     $username = "root";     $password = "";     $dbname = "db";     // Create connection     $con = mysqli_connect($servername, $username, $password, $dbname);     // Check connection     if (!$con) {         die("Connection failed: " . mysqli_connect_error());     }     $sql = "SELECT fruits FROM fruits";     $result = $con->query($sql); ?> <label for="fruits">Treatment Type: </label> <select name="fruits" id="fruits" >     <option value="" <?php if(!isset($_POST['fruits']) || (isset($_POST['fruits']) && empty($_POST['fruits']))) { ?>selected<?php } ?>>--Select--</option>     <?php      while($row = $result->fetch_assoc()) {     ?>     <option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>     <?php } ?> </select>        </div>    </div></div>

结果页:

<?php    $con    =   mysqli_connect("","","","");// Check connection    if(mysqli_connect_errno())         echo "Failed to connect to MySQL: " . mysqli_connect_error();        $fruits =   mysqli_real_escape_string($con, $_POST['fruits']);        if(!empty($fruits)) { $sql1   =   "SELECT * FROM treatment WHERe fruits LIKE '%$fruits%'"; $result =   mysqli_query($con, $sql1); $count  =   mysqli_num_rows($result);        }        else $count  =   0; ?>        <table > <thead>     <tr>         <th>Name</th>         <th>Type</th>         <th>Fruits</th>     </tr> </thead>        <?php if ($count > 0)  {     while($row = mysqli_fetch_assoc($result)) {        ?> <tbody data-link='row' >     <tr>         <td><a href='#'><?php echo $row['name']; ?></a></td>         <td><?php echo $row['type']; ?></td>         <td><?php echo $row['fruits']; ?></td>     </tr> </tbody>        <?php     } } else     echo "0 results";        ?>        </table>        <?php mysqli_close($con); ?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号