我目前正在使用PDO从MySQL数据库中检索数据。我正在使用foreach将数据显示到页面上,并希望用户能够在输入字段中输入搜索词,点击提交按钮,然后只返回标题包含搜索词的结果。
这是我目前的代码:
file_functions.php - SQL查询功能的位置
function getbycategory($category, $limit){
global $db;
if (isset($category) AND $category != "all") {
$sm = $db->prepare ("SELECT * FROM parts WHERE main_category = :category");
$sm->bindParam(':category', $category, PDO::PARAM_INT);
} else {
$sm = $db->prepare ("SELECT * FROM parts");
}
$sm->execute();
return $sm->fetchAll();
}
files.php - 显示结果的位置
$files = getbycategory($_GET['filter']);
foreach($files as $file){
echo'<div class="col-lg-" id="file-'.$file['id'].'">
<div class="file-list-item first" id="">
<img class="file-image" height="120px" width="180px" src="'.$file['image_url'].'" />
<div class="file-text">
<h3><strong>'.$file['name'].'</strong></h3>
Submitted by: '.$file['submitter'].'<br/>
Author: '.$file['author'].'<br />
Category: '.ucfirst($file['subcategory']).'<br />
Description: '.substr($file['description'],0,45).'...
</div>
<div class="download">
<a target="_blank" href="'.$file['download_url'].'" class="btn-success btn btn-default">Download</a>
<a href="'.baseurl.'/broken.php?id='.$file['id'].'" class="btn btn-default">Report as Broken</a><br /><br />';
if($file['is_broken']){
echo '<span class="broken"><i data-toggle="tooltip" data-placement="left" id="broken" title="This file has been reported as broken and is awaiting review." class="fa fa-warning fa-2x"></i></span>';
}
echo '
</div>
</div>
</div>';
};
?>
以下是用于优化结果的表单。目前,过滤器下拉菜单适用于过滤器,但搜索项不适用。这是我希望实施的内容
<form method="get">
<select name="filter">
<option <?php if($_GET['filter'] == "all" OR !isset($_GET['filter'])){echo 'selected';} ?> value="all">View All Files</option>
<option <?php if($_GET['filter'] == "1") {echo 'selected';} ?> value="1">View Vehicles Only</option>
<option <?php if($_GET['filter'] == "2") {echo 'selected';} ?> value="2">View Lighting Equiptment</option>
</select>
<input type="submit" value="Filter Results"/><br /><br />
<input type="text" name="search" placeholder="Enter a search term" />
<input type="submit" value="Search Results"/>
</form>
总而言之,我希望使用底部代码片段中的文本字段,通过将其标题与搜索词进行比较来优化files.php中显示的结果。
我希望将搜索字词与$ file ['name']进行比较。
非常感谢。
答案 0 :(得分:0)
好的,让我通过一个简单的LIKE
比较来引导您完成它...
首先,方法签名。 Globals是一个坏主意,因此我们将包含PDO
实例
function getbycategory(PDO $db, $category = null, $search = null, $limit = null)
现在,构建查询并收集参数。
$params = [];
$where = [];
if ($category !== null) {
$where[] = 'main_category = :category';
$params[':category'] = [
'val' => (int) $category,
'type' => PDO::PARAM_INT
];
}
if ($search !== null) {
$where[] = "LOWER(`name`) LIKE CONCAT('%', LOWER(:search), '%')";
$params[':search'] = [
'val' => (string) $search,
'type' => PDO::PARAM_STR
];
}
$query = sprintf('SELECT * FROM `parts` %s', implode(' AND ', $where));
if ($limit !== null) {
$query .= ' LIMIT :limit';
$params[':limit'] = [
'val' => (int) $limit,
'type' => PDO::PARAM_INT
];
}
$stmt = $db->prepare($query);
foreach ($params as $key => $param) {
// use bindValue to avoid problems with variable references in the loop
// see http://stackoverflow.com/questions/4174524/binding-params-for-pdo-statement-inside-a-loop
$stmt->bindValue($key, $param['val'], $param['type']);
}
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);