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

时间:2014-11-07 07:28:29

标签: php html sql drop-down-menu mysqli

此代码是我的搜索表单的演示部分。实际搜索表单包含3个下拉列表,从下拉列表中选择的值充当执行搜索的关键字。 这个下拉列表包含水果,如芒果,苹果,葡萄等。搜索代码工作正常。但问题是,下拉列表中显示的第一个选项是Select(没有值),低于该选项,实际的水果列表开始,但是当第一个水果开始加载时,第一个水果的值仍然被选中时间 。我想要做的是当页面第一次加载时,即下拉列表中的值为Select,应该显示任何内容,之后用户从下拉列表中选择值并点击提交按钮然后只有结果应该显示

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?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);
                echo "<label for='fruits'>Treatment Type: </label>";
                echo "<select name='fruits' id='fruits' class='form-control'><option value=''>--Select--</option>";
                while($row = $result->fetch_assoc()) {
                echo "<option value='" . $row['fruits'] . "'>" . $row['fruits'] . "</option>";
                }
                echo "</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']);

        $sql1 = "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
        $result = mysqli_query($con, $sql1);
        echo "<table class='table table-striped table-bordered responsive'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Fruits</th>
                </tr>
            </thead>";

            if (mysqli_num_rows($result) > 0) 
                {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            echo "<tbody data-link='row' class='rowlink'>";
                                echo "<tr>";
                                    echo "<td><a href='#'>" . $row['name'] . "</a></td>";
                                    echo "<td>" . $row['type'] . "</td>";       
                                    echo "<td>" . $row['fruits'] . "</td>";

                                echo "</tr>";
                            echo "</tbody>";    
                        }

                }
            else 
                {
                    echo "0 results";
                }
            echo "</table>";
            mysqli_close($con);
?>

如果有人可以指导我,我将不胜感激

P.S(编辑部分)

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?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); ?>
                echo "<label for="fruits">Treatment Type: </label>";
                echo "<select name="fruits" id="fruits" class="form-control">
                <option value="" <?php if(!isset($_POST['fruits'])) { ?>selected<?php } ?>>--Select--</option>";
                <?php 
                while($row = $result->fetch_assoc()) { ?>

                echo "<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>

1 个答案:

答案 0 :(得分:2)

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

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?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" class="form-control">
                <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 class='table table-striped table-bordered responsive'>
            <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' class='rowlink'>
                <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); ?>