如果在select标签html php中选择男/女,如何只输出男/女学生

时间:2014-10-05 08:03:41

标签: php html mysql

我创建了一个php程序,用户将选择10月1日到11月1日期。选择后,该程序将生成在这些给定日期之间生病的学生的结果。此外,我希望用户选择他/她是否只想显示男性或女性结果。如果用户在下拉列表中选择男性,则仅显示男性学生,否则仅显示女性学生。我还希望用户能够同时显示两种性别。

这是我的程序,我唯一的问题是,如果我选择男性或女性,它将显示两者。如果选择了这两个选项,我只希望显示两种性别。

<form action="gen.php" method="POST"> 
        <input type="date" name="date1"> 
        <input type="date" name="date2"> 
        <select name="gender">
            <option value="male">Male</option>
            <option value="female">Female</option>
            <option value="both">Both</option>
        </select>
        <input type="submit" name="generate" value="Generate">
    </form>

<?php

$con = mysqli_connect("localhost", "root", "", "sickstuds") or die("amasarry can't connect...");

if(isset($_POST['date1']) && isset($_POST['date2']) && isset($_POST['gender'])){
    $date1 = $_POST['date1'];
    $date2 = $_POST['date2'];
    $gender = $_POST['gender'];



    $query = "SELECT * FROM sickstud";

    $result = mysqli_query($con, $query);

    $found = mysqli_num_rows($result);

        if($found > 0 ){

            while($row = mysqli_fetch_array($result)){
                if($row['date'] >= $date1 && $row['date'] <= $date2){
                    if($row['Gender'] == $gender){

                        echo '

                        <table border="1" cellpadding="20" cellspacing="5">
                            <tr>
                                <td>'.$row['id'].'</td>
                                <td>'.$row['fn'].'</td>
                                <td>'.$row['mn'].'</td>
                                <td>'.$row['ln'].'</td>
                                <td>'.$row['date'].'</td>
                                <td>'.$row['program'].'</td>
                                <td>'.$row['Gender'].'</td>
                            </tr>
                        </table>

                    ';

                    }
                    else if($row['Gender'] == $gender){

                            echo '

                        <table border="1" cellpadding="20">
                            <tr>
                                <td>'.$row['id'].'</td>
                                <td>'.$row['fn'].'</td>
                                <td>'.$row['mn'].'</td>
                                <td>'.$row['ln'].'</td>
                                <td>'.$row['date'].'</td>
                                <td>'.$row['program'].'</td>
                                <td>'.$row['Gender'].'</td>
                            </tr>
                        </table>

                    ';

                    }

                    else{

                            echo '

                        <table border="1" cellpadding="20">
                            <tr>
                                <td>'.$row['id'].'</td>
                                <td>'.$row['fn'].'</td>
                                <td>'.$row['mn'].'</td>
                                <td>'.$row['ln'].'</td>
                                <td>'.$row['date'].'</td>
                                <td>'.$row['program'].'</td>
                                <td>'.$row['Gender'].'</td>
                            </tr>
                        </table>

                    ';

                    }

                }
                else{
                    echo '';
                }
            }

        }
        else{
            echo 'no';
        }
}

?>

3 个答案:

答案 0 :(得分:0)

您需要在查询中添加Where 更改您的查询

这个

$query = "SELECT * FROM sickstud";

$query = "SELECT * FROM sickstud where gender='". $gender."'";
下拉菜单中的

更改了这两个选项

的这一行
<option value="both">Both</option>

到此

<option value="">Both</option>

答案 1 :(得分:0)

首先,您可以根据当前选择从数据库中选择所需的结果,例如:

$query = "SELECT * FROM sickstud WHERE Gender='" . $_POST['gender'] . "'";

回答你的问题。你有三个条款打印出来:

if($row['Gender'] == $gender){
...
} else if($row['Gender'] == $gender){
...
} else {
...
}

你可能想要像:

if($row['Gender'] == $gender || $gender == 'both'){
...
}

希望它有所帮助。

答案 2 :(得分:0)

您必须首先测试性别值

然后在您的查询中使用它来自定义结果...

<?php

$con = mysqli_connect("localhost", "root", "", "sickstuds") or die("amasarry can't connect...");

if(isset($_POST['date1']) && isset($_POST['date2']) && isset($_POST['gender'])){
    $date1 = $_POST['date1'];
    $date2 = $_POST['date2'];
    $gender = $_POST['gender'];


    if($gender == 'male' || $gender == 'female') // 1 here is the value of the gender it might be 1 or male depends on your form 
    {
      $query = mysqli_query($con, "SELECT * FROM sickstud WHERE gender = '$gender' AND date >= '$date1' AND date <= '$date2'");
      $rows = mysqli_num_rows($query);
      if($rows > 0)
      {
        while($i = mysqli_fetch_assoc($query))
        {
          // the rest of what you want to do and then echo
        }
      }
      else
      {
        echo 'No results';
      }
    }
    elseif($gender == 'both')
    {
      $query = mysqli_query($con, "SELECT * FROM sickstud WHERE date >= '$date1' AND date <= '$date2'");
      $rows = mysqli_num_rows($query);
      if($rows > 0)
      {
        while($i = mysqli_fetch_assoc($query))
        {
          // the rest of what you want to do and then echo
        }
      }
      else
      {
        echo 'No results';
      }
    }
?>