我创建了一个 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';
}
}
?>
最佳答案
你必须先测试性别值
然后在查询中使用它来自定义结果...
<?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';
}
}
?>
关于php - 如果在选择标签html php中选择了男性/女性,如何仅输出男性/女性学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26200776/