Php过滤器选择选项
我正在尝试使用一个选择下拉框制作一个过滤器选项,但我不知道我在哪里失败了。我有一个搜索栏,工作完美,但我想能够选择我想搜索的位置。 例如,我可以键入我正在寻找的工作的名称,但我希望筛选位置,以便仅在一个城市中查看 这是我的密码 编辑:多亏了ADyson,我对代码做了一些编辑,我将在上一次发布新代码Php过滤器选择选项,php,mysql,Php,Mysql,我正在尝试使用一个选择下拉框制作一个过滤器选项,但我不知道我在哪里失败了。我有一个搜索栏,工作完美,但我想能够选择我想搜索的位置。 例如,我可以键入我正在寻找的工作的名称,但我希望筛选位置,以便仅在一个城市中查看 这是我的密码 编辑:多亏了ADyson,我对代码做了一些编辑,我将在上一次发布新代码 <?php require 'views/header.php'; $connection = getDbConntection(); // Searc
<?php
require 'views/header.php';
$connection = getDbConntection();
// Search //
if (!empty($_GET['search'])) {
$data = [
'job_name' => '%' . $_GET['search'] . '%',
'location_id' => $_GET['location_id']
];
$searches = $connection->prepare("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id "
. "where jobs.name like :job_name"
. 'AND location_id = :location_id');
$searches->execute($data);
$searches= $query->fetchAll();
// List //
} else {
$query = $connection->query("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id ");
$searches = $query->fetchAll();
}
?>
<div class="w3-row-padding w3-padding-64 w3-container">
<div class="w3-content">
<h1 class="center"> Jobs table </h1>
<br>
<form style="text-align:center" action="index.php" method="GET">
<input type="text" name="search" value="Search jobs..." onfocus="this.value = ''" class="btn btn-danger">
<select name="location_id" class="btn btn-danger">
<?php foreach ($searches as $location): ?>
<?php $selectedText = ($location['id']) ?>
<option value= <?= $selectedText ?> > <?= $location['location_name'] ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Search" class="btn btn-danger">
<a href="index.php" class="btn btn-danger">Back to list </a>
</form>
<br>
<div class="center">
<table>
<tr>
<th>ID</th>
<th> Job Name</th>
<th> Job Location</th>
<th> Job Domain</th>
<th> Job Description</th>
<th> Job Salary</th>
<th>Actions</th>
</tr>
<?php foreach ($searches as $key => $job_name) : ?>
<tr>
<th><?= $job_name['id'] ?></th>
<th style="background-color: lightskyblue"><?= $job_name['job_name'] ?></th>
<td><?= $job_name['location_name'] ?></td>
<td><?= $job_name['domain_name'] ?></td>
<td><?= $job_name['description'] ?></td>
<td><?= $job_name['job_salary'] ?></td>
<td> <a class="btn btn-success" href="edit.php?id=<?= $job_name['id'] ?>"> Edit </a>
<a class="btn btn-danger" href="delete.php?id=<?= $job_name['id'] ?>">Delete</a> </td>
</tr>
<?php endforeach; ?>
</table>
</div>
<style>
table td, table th {
padding: 15px;
text-align: center;
}
table th {
background:#3390FF;
}
table {
width: 100%;
border: 3px solid #ccc;
border-collapse: collapse;
}
.ce
nter {
margin: auto;
width: 100%;
border: 3px solid red;
padding: 10px;
text-align: center;
}
</style>
</div>
</div>
工作表
身份证件
职务名称
工作地点
工作域
职位描述
工作工资
行动
表td,表th{
填充:15px;
文本对齐:居中;
}
表th{
背景#3390FF;
}
桌子{
宽度:100%;
边框:3px实心#ccc;
边界塌陷:塌陷;
}
.ce
内特{
保证金:自动;
宽度:100%;
边框:3倍纯红;
填充:10px;
文本对齐:居中;
}
对不起,代码太乱了,我对一般的编码还不熟悉多亏了ADysom,我解决了这个问题,这是正确的代码
<?php
require 'views/header.php';
$connection = getDbConntection();
$locations = $connection->query("select * from locations");
// Search //
if (!empty($_GET['search'])) {
$data = [
'job_name' => '%' . $_GET['search'] . '%',
'location_id' => $_GET['location_id']
];
$query = $connection->prepare("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id "
. "where jobs.name like :job_name "
. "AND location_id = :location_id ");
$query->execute($data);
$query = $query->fetchAll();
// print_r($_GET);
// List //
} else {
$query = $connection->query("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id ");
$query = $query->fetchAll();
}
?>
<div class="w3-row-padding w3-padding-64 w3-container">
<div class="w3-content">
<h1 class="center"> Jobs table </h1>
<br>
<form style="text-align:center" action="index.php" method="GET">
<input type="text" placeholder='Search jobs..' name="search" onfocus="this.value = ''" class="btn btn-danger">
<select name="location_id" class="btn btn-danger">
<?php foreach ($locations as $location): ?>
<option value="<?= $location['id'] ?>">
<?= $location['name'] ?>
</option>
<?php endforeach; ?>
</select>
<input type="submit" value="Search" class="btn btn-danger">
<a href="index.php" class="btn btn-danger">Back to list </a>
</form>
<br>
<div class="center">
<table>
<tr>
<th>ID</th>
<th> Job Name</th>
<th> Job Location</th>
<th> Job Domain</th>
<th> Job Description</th>
<th> Job Salary</th>
<th>Actions</th>
</tr>
<?php foreach ($query as $key => $job_name) : ?>
<tr>
<th><?= $job_name['id'] ?></th>
<th style="background-color: lightskyblue"><?= $job_name['job_name'] ?></th>
<td><?= $job_name['location_name'] ?></td>
<td><?= $job_name['domain_name'] ?></td>
<td><?= $job_name['description'] ?></td>
<td><?= $job_name['job_salary'] ?></td>
<td> <a class="btn btn-success" href="edit.php?id=<?= $job_name['id'] ?>"> Edit </a>
<a class="btn btn-danger" href="delete.php?id=<?= $job_name['id'] ?>">Delete</a> </td>
</tr>
<?php endforeach; ?>
</table>
</div>
<style>
table td, table th {
padding: 15px;
text-align: center;
}
table th {
background:#3390FF;
}
table {
width: 100%;
border: 3px solid #ccc;
border-collapse: collapse;
}
.center {
margin: auto;
width: 100%;
border: 3px solid red;
padding: 10px;
text-align: center;
}
::placeholder {
color: white;
opacity: 1;
}
</style>
</div>
</div>
工作表
身份证件
职务名称
工作地点
工作域
职位描述
工作工资
行动
表td,表th{
填充:15px;
文本对齐:居中;
}
表th{
背景#3390FF;
}
桌子{
宽度:100%;
边框:3px实心#ccc;
边界塌陷:塌陷;
}
.中心{
保证金:自动;
宽度:100%;
边框:3倍纯红;
填充:10px;
文本对齐:居中;
}
::占位符{
颜色:白色;
不透明度:1;
}
看起来应该将'location\u id'=>$\u GET['search']
更改为'location\u id'=>$\u GET['location\u id']
。现在,您正在使用“搜索”文本作为位置ID!此外,稍后还会出现语法错误-和location_id=;位置id
需要代码>更改为:
.P.S。如果您进行了一些简单的调试并确保正确设置了错误日志记录,那么这些错误应该很容易被发现。你有没有花时间学习如何调试?这几乎和编程本身一样重要。有一个简单的PHP调试指南。从代码的快速通读来看,可能还有其他问题不是很明显
是多余的,因为您从未在该行之后使用$searchText变量。它没有目的。您可以简单地删除这行代码。Thnaks为了帮助您,我在这里发布代码并对其进行更改后注意到了语法错误和逻辑错误。运行代码后,我得到了这个未捕获的PDOException:SQLSTATE[HY093]:无效的参数编号:C:\xampp\htdocs\TemaCarantina\index.php:17堆栈跟踪:#0 C:\xampp\htdocs\TemaCarantina\index.php(17):PDO语句->执行(数组)#1{main}在C:\xampp\htdocs\TemaCarantina\index.php的第17errorBTW行抛出,但查询仍然可以全部生成一个字符串:select jobs.id,jobs.name作为job\u name,salary作为job\u salary,description,location\u id,domain\u id,locations.name作为location\u name,domains.name作为jobs上的domain\u name左连接位置。location\u id=locations.id左连接jobs上的域。domain\u id=domains.id where jobs.name类似:job\u name AND location\u id=:location\u id“
。where子句不需要是单独的字符串。