我有这段代码,虽然可以正常工作,但是我却无法正常工作。我有一个包含两个选择框的表单,当用户更改每个选择框的值时,将进行ajax调用,并且页面将更新与所选选项匹配的记录。
此位工作正常。
但是,当用户通过更改选择选项来过滤记录,但又想通过在两个选择框中都选择“全部”来返回以显示所有记录(因此重置选项)时,没有显示任何内容吗?我有一个if查询,我认为可以根据$ query是否为空来设置$ sql的值。
这是我的html / php页面
<html>
<head>
<script type="text/javascript">
function showUser()
{
var users = document.getElementById('instructor').value;
var sex = document.getElementById('club').value;
if (users=="" && sex=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get-user.php?student_instructor="+users+"&club="+sex,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="student_instructor" id="instructor" onChange="showUser()">
<option value="">Select a person:</option>
<option value="Clive Double">Clive Double</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
<select name="club" id="club" onchange="showUser()">
<option value="">Male/Female:</option>
<option value="">All</option>
<option value="sennen">Sennen</option>
<option value="marazion">Marazion</option>
</select>
</form>
<br />
<div id="txtHint">
<b>Person info will be listed here.</b>
</div>
</body>
</html>
这是我获取的user.php代码
<?php
include_once("includes.php");
$count = "";
$query = "";
#print_r($_GET);
foreach($_GET as $field => $value)
{
#echo "field = ".$field."<br />value = ".$value."<br />";
if($value != "")
{
if($count > 0)
{
$query .= " AND ";
}
$query .= $field." = '".$value."'";
$count++;
}
}
//ORDER BY SQL TO GO HERE
$where = '';
if( !empty ( $query ) )
{
$where = ' WHERE ' . $query;
$sql = "SELECT * FROM tbl_register".$where;
}
else
{
$sql = "SELECT * FROM tbl_register";
}
#echo "<p>".$sql."</p>";
$result = mysqli_query($conn,$sql);
$numrows = mysqli_num_rows($result); ?>
<div class='col-sm-12 day-heading'>
<div class="row">
<div class="col-sm-2">Forename</div>
<div class="col-sm-2">Surname</div>
<div class="col-sm-2">Club</div>
<div class="col-sm-3">Instructor</div>
<div class="col-sm-2">Date</div>
<div class="col-sm-1">Age</div>
</div>
</div>
<?php
while($row = $result->fetch_assoc())
{
$id = $row['id'];
$firstname = $row['student_forename'];
$surname = $row['student_surname'];
$age = $row['student_age'];
$club = $row['club'];
$instructor = $row['student_instructor'];
$date = $row['date_awarded'];
$grade = $row['student_grade'];
if($i % 2 == 1)
{
$math = "odd-row";
}
else
{
$math = "even-row";
}?>
<div class="col-sm-12 <?php echo $math?>">
<div class="row">
<div class="col-sm-2"><?php echo $firstname?></div>
<div class="col-sm-2"><?php echo $surname?></div>
<div class="col-sm-2"><?php echo $club?></div>
<div class="col-sm-3"><?php echo $instructor?></div>
<div class="col-sm-2"><?php echo $date?></div>
<div class="col-sm-1"><?php echo $age?></div>
</div>
</div><?php
$i++;
}?>
当两个选择框都设置为全部,然后显示所有记录时,我希望基本上具有一个重置选项。
这是我认为可以工作的一点代码,但可悲的是
if( !empty ( $query ) )
{
$where = ' WHERE ' . $query;
$sql = "SELECT * FROM tbl_register".$where;
}
else
{
$sql = "SELECT * FROM tbl_register";
}
有人可以看到我做错了吗?认识我可能很简单:D
答案 0 :(得分:1)
您每次都会在for循环中将$query
变量重置为''。
include_once("includes.php");
$count = "";
$query = "";
print_r($_GET);
foreach($_GET as $field => $value)
{
echo "field = ".$field."<br />value = ".$value."<br />";
//$query = " WHERE "; //Commented this.
if($value != "")
{
if($count > 0)
{
$query .= " AND ";
}
$query .= $field." = ".$value;
$count++;
}
}
//Added a New Variable
$where = '';
if( !empty ( $query ) ) {
$where = ' WHERE ' . $query;
}
$sql = "SELECT * FROM tbl_register".$where;
echo "<p>".$sql."</p>";