具有一个或多个参数的搜索表单我已经完成了基础工作,我创建了两个文件,一个用户输入搜索参数的搜索表单,以及一个运行输入项的结果文件。为了简单起见,我们将搜索表单文件指定为search.php,将结果页面指定为Resul.php。编辑:清理标签礼仪,以方便阅读。search.php<?php if(!empty($_POST['id']) && isset($_POST['id'])) {
header("Location: ?m=search.results&id=".$_POST['id']."");
} elseif(!empty($_POST['major']) && isset($_POST['major'])) {
header("Location: ?m=search.results&major=".$_POST['major']."");
} elseif(!empty($_POST['college']) && isset($_POST['major'])) {
header("Location: ?m=search.results&college=".$_POST['college']."");
} elseif (!empty($_POST['name']) && isset($_POST['name'])) {
header("Location: ?m=search.results&name=".$_POST['name']."");
} elseif (!empty($_POST['id']) && !empty($_POST['college']) && !empty($_POST['major'])
&& isset($_POST['submit']) && !empty($_POST['name'])) {
echo "<div class='alert alert-danger'>No students found. Please try different parameters.</div>";
}
?>
<h4>Search</h4>
<form method="POST">
<table width="100%">
<tr><td>ID:</td><td> <input type="text" name="id" class="form-control"></textarea></td></tr>
<tr><td>Name:</td><td> <input type="text" name="name" class="form-control"></textarea></td></tr>
<tr><td>Major:</td><td><select name="major" class="form-control"><option></option><?php echo majorSelect(); ?></select></td></tr>
<tr><td>College:</td><td><select name="college" class="form-control"><option></option><?php echo collegeSelect(); ?></select></td>
</tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Search" class="btn btn-lrg btn-primary" style="margin-top:10px;"></td>
</tr>
</table>
</form>因此,本质上,我想重写上面的内容,而用户可以输入一个或多个参数,然后返回所需的结果(例如,名称和学院名称=x&College=y或如果需要的话所有项目)。我想我需要重新思考这两个文件中的逻辑,但我愿意接受您的任何想法或建议,这个美妙的社区可能会有!如有任何建议/指导,将不胜感激。
3 回答
![?](http://img1.sycdn.imooc.com/54584d560001571a02200220-100-100.jpg)
守候你守候我
TA贡献1802条经验 获得超10个赞
WHERE
implode()
AND
OR
$wheres = array();$params = array();if (!empty($_GET['id'])) { $wheres[] = 'a.uid = :uid'; $params[':uid'] = $_GET['id'];}if (!empty($_GET['major'])) { $wheres[] = 'a.major = :major'; $params[':major'] = $_GET['major'];}if (!empty($_GET['name'])) { $wheres[] = 'b.name LIKE :name'; $params[':name'] = '%'.$_GET['name'].'%';}// And so on for all parameters$sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id";if (!empty($wheres)) { $sql .= " WHERE " . implode(' AND ', $wheres);}$stmt = $db->prepare($sql);$stmt->execute($params);
while ($student = $stmt->fetch()) { ...}
![?](http://img1.sycdn.imooc.com/545868b60001587202200220-100-100.jpg)
慕丝7291255
TA贡献1859条经验 获得超6个赞
<form action="<?= $_SERVER["REQUEST_URI"]; ?>" method="GET"> <input name="major" value="<?= $_GET["major"]; ?>" /> <select name="college"> <option value="1" <?PHP if( $_GET["college"] == 1 ) echo 'selected="true"'; ?>>Business</option> </select></form><?PHPif( ! empty( $_GET ) ){ if (isset($_GET['major'])) { $wheres[] = 'a.major = :major'; $params[':major'] = $_GET['major']; } if (isset($_GET['name'])) { $wheres[] = 'b.name LIKE :name'; $params[':name'] = '%'.$_GET['name'].'%'; } // And so on for all parameters $sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id"; if (!empty($wheres)) { $sql .= " WHERE " . implode(' AND ', $wheres); } $stmt = $db->prepare($sql); $stmt->execute($params);}?>
- 3 回答
- 0 关注
- 520 浏览
添加回答
举报
0/150
提交
取消