【发布时间】:2020-07-21 23:05:10
【问题描述】:
我正在尝试使用选择下拉框创建过滤器选项,但我不知道我在哪里失败。我有一个完美的搜索栏,但我希望能够选择我想要搜索的位置。 例如,我可以输入我正在寻找的工作的名称,但我想过滤位置以仅在一个城市中查看 这是我的代码
编辑:感谢 ADyson,我对代码进行了一些编辑,我将在最后一个代码上发布新代码
<?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>
抱歉,代码混乱,我是一般编码新手
【问题讨论】:
-
看来
'location_id' => $_GET['search']应该改为'location_id' => $_GET['location_id']。现在,您正在使用“搜索”文本作为位置 ID!您稍后还会遇到语法错误 -AND location_id = ;location_id需要将;更改为:。 -
P.S.如果您进行了一些简单的调试并确保正确设置了错误日志记录,这些应该很容易发现。你有没有花时间学习如何调试?它几乎与编程本身一样重要。 phpknowhow.com/basics/basic-debugging 有一个使用 PHP 进行调试的简单指南。可能还有其他问题,通过快速阅读代码并不能立即发现。
-
哦,
$searchText = $_GET['search'];是多余的,因为您从不在该行之后使用 $searchText 变量。它没有目的。您可以简单地删除这行代码。 -
感谢帮助,我在此处发布代码并更改后注意到语法错误和逻辑错误。运行代码后,我得到了这个
-
Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter is not defined in C:\xampp\htdocs\TemaCarantina\index.php:17 Stack trace: #0 C:\xampp\htdocs \TemaCarantina\index.php(17): PDOStatement->execute(Array) #1 {main} 在 C:\xampp\htdocs\TemaCarantina\index.php 第 17 行抛出错误