项目背景
我首先要说的是我不是最好的PHP并且是所有帐户的初学者,但我已经尝试为我的大学项目创建基于php的搜索。
这是一个冰沙配方网站,用户首先会通过输入关键字进行搜索,这将生成搜索结果。这部分我已经很好了。然而,当涉及过滤结果时,我真的很挣扎。用户应该能够通过水果/蔬菜/冰沙类型以及关键字本身进行过滤。例如:
Keyword = apple Filters =芒果(水果过滤器),菠菜(蔬菜过滤器)排毒(类型过滤器)
然而,目前发生的是,我会得到苹果,芒果,菠菜和排毒冰沙的每个实例。除此之外,如果冰沙同时包含苹果和芒果,它将产生两次冰沙。我知道我需要在这里包含array_unique(),但我真的不知道在哪里。
PHP
到目前为止,这是我的php:`
$dbhost = '*';
$dbuser = '*';
$dbpass = '*';
$dbname = '*';
$k = $_GET['k'];
$terms = explode(" ", $k);
//START THE QUERY HERE
$query = "SELECT * FROM search";
//FROM THE VIEW FIND OUT IF THE USER HAS CLICKED ON ANY FRUITS.
//IF SO THEN JOIN THE SEARCH_METADATA TABLE TO THE QUERY SO YOU CAN FIND ALL THE POSSIBLE FRUITS
if (!empty($_GET['fl-Fruit'])) {
$query .= ' INNER JOIN search_metadata ON search_id = search.id';
}
//START THE WHERE CLAUSE HERE
$query .= ' WHERE ';
//FIRST LOOK FOR THE SEARCH TERM THE USER ENTERED
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= " keywords LIKE '%$each%' ";
else
$query .= " OR keywords LIKE '%$each%' ";
}
//ONCE THE SEARCH TERM HAS BEEN FOUND
//TODO ==== RUN CHECKS FOR VEG AND TYPE OPTIONS
if (isset($_GET['fl-Fruit'])) {
//SET ALL THE GET VARIABLES TO NEW VARIABLES JUST FOR MAKING IT EASIER TO READ
$fruits = $_GET['fl-Fruit'];
//FOREACH FRUITS SELECTED FROM THE FACET SEARCH
//RUN THROUGH EACH ONE IN THE WHERE CLAUSE.
foreach ($fruits as $fruit) {
$query .= " XOR value = '$fruit' ";
}
}
if (isset($_GET['fl-veg'])) {
//SET ALL THE GET VARIABLES TO NEW VARIABLES JUST FOR MAKING IT EASIER TO READ
$vegetables = $_GET['fl-veg'];
//FOREACH FRUITS SELECTED FROM THE FACET SEARCH
//RUN THROUGH EACH ONE IN THE WHERE CLAUSE.
foreach ($vegetables as $veg) {
$query .= " XOR value = '$veg' ";
}
}
if (isset($_GET['fl-s-type'])) {
//SET ALL THE GET VARIABLES TO NEW VARIABLES JUST FOR MAKING IT EASIER TO READ
$types = $_GET['fl-s-type'];
//FOREACH FRUITS SELECTED FROM THE FACET SEARCH
//RUN THROUGH EACH ONE IN THE WHERE CLAUSE.
foreach ($types as $type) {
$query .= " XOR value = '$type' ";
}
}
var_dump($query);
echo '<h1>' ."Your search results for $k". '</h1>';
//CONNECT TO DATABASE
mysql_connect("$dbhost", "$dbuser", "$dbpass");
mysql_select_db("$dbname");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row ['id'];
$title = $row ['title'];
$description = $row ['description'];
$keywords = $row ['keywords'];
$link = $row ['link'];
echo "<div class='result'><img src='images/$id.jpg'><div class='result-text'><h2><a href='$link'>$title</a></h2>
<p>$description</p></div></div>";
}
}
else
echo "No results for <b>$k</b>";
?>`
HTML表单
<form class='form-wrapper' action='./search.php' method='get'>
<input id='search' type='text' name='k' value='<?php echo $_GET['k']; ?>' />
<input type='submit' id="submit" value='search'>
<div class="menu">
<strong><p class="menu-toggle"> Filter your search </p></strong>
</div>
<section class="filters">
<h2>Filter smoothies by Fruit:</h2>
<p>
<input type="checkbox" name="fl-Fruit[]" value="apple" id="apple">
<label for="apple">Apple</label>
</p>
...
<p>
<input type="checkbox" name="fl-Fruit[]" value="strawberries" id="strawberries">
<label for="strawberries">Strawberries</label>
</p>
<h2>Filter smoothies by veg:</h2>
<p>
<input type="checkbox" name="fl-veg[]" value="beetroot" id="beetroot">
<label for="beetroot">Beetroot</label>
</p>
...
<p>
<input type="checkbox" name="fl-veg[]" value="spinach" id="spinach">
<label for="spinach">Spinach</label><br>
</p>
<h2>Filter by smoothie type:</h2>
<p>
<input type="checkbox" name="fl-s-type[]" value="detox" id="detox">
<label for="detox">Supet Detox</label>
</p>
...
<p>
<input type="checkbox" name="fl-s-type[]" value="muscle" id="muscle">
<label for="muscle">Muscle Building</label>
</p>
</form>
表格
我目前有两个表“search”和“search_metadata”。 “搜索”表包含关键字搜索的关键字,“search_metadata”表包含过滤器的属性,并具有以下列“search_id”(外键),“类型”(水果,蔬菜,类型),“值” “(苹果,胡萝卜,排毒)。
重申我的要求:
我希望有足够的信息/没有太多的信息,如果我可以改进这个问题请告诉我,因为这是我第一次在这里发帖。
由于
答案 0 :(得分:0)
我会在SQL查询本身中处理搜索逻辑,而不是获取所有搜索&#39;行并在PHP中过滤它们。用户是否搜索&#34; apple&#34;并选择&#34;芒果&#34;的示例查询和&#34; passionfruit&#34;过滤器将是:
SELECT
*
FROM
Search AS s JOIN Search_Metadata AS sm USING( search_id )
WHERE
search_id = "apple" AND
sm.value IN
(
"mango", "passionFruit"
);
请注意,是MySQL风格的语法。
有几种方法可以构建这样的查询。一种是在php中简单地使用字符串操作。另一个是使用&#34;查询构建器&#34;库,如FluentPDO
我觉得你的数据库设计需要重新考虑。