我有以下脚本可以根据标签从数据库中获取内容
<?php
header("Content-type: text/html; charset=UTF-8");
include 'config.php';
include 'lib.php';
error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
$query = $_GET['query'];
$db = dbConnect();
$tags = explode(";", $query);
$entries = Array();
foreach($tags as $tag) {
$tag = trim($tag);
echo "<br/>".$tag;
$query = "SELECT * FROM nv_entries entries JOIN nv_tags tags on (entries.id = tags.entrie_id) join nv_images images on (tags.entrie_id = images.entrie_id) WHERE tags.tag = '$tag'";
$entrie = execSelect($query);
array_push($entries, $entrie);
}
echo "<pre>";
print_r($entries);
echo "</pre>";
// keep the ones that are there as many times as there are diferent tags
// ...
dbClose($db);
?>
例如,如果我使用标签树和绿色,那么我会得到这个:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[band] => Green
[album] =>
[label] => ATCO
[year] => 1966
[text] => text about green.
[entrie_id] => 1
[tag] => tree
[source] => img01_4u8y5.png
)
[1] => Array
(
[id] => 2
[band] => Kids for Cash
[album] => No More Walls E.P.
[label] =>
[year] => 1986
[text] => Text about album kids for cash.
[entrie_id] => 2
[tag] => tree
[source] => img02_9lch1.png
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[band] => Green
[album] =>
[label] => ATCO
[year] => 1966
[text] => text about green.
[entrie_id] => 1
[tag] => green
[source] => img01_4u8y5.png
)
)
)
id = 1 的那个出现了两次。这等于标签的总数,所以我想保留那个。id = 2 的那个只出现一次,所以 1 个标签为此失败,因此我不需要它。
我怎样才能摆脱它?在那之后,我怎样才能清理双数组将消失的数组?
编辑:
这应该接近我正在寻找的,我只得到一个错误:
查询失败:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN”附近使用正确的语法
在下面的脚本中生成的查询如下所示:
home
GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN ('tree','green') HAVING COUNT(DISTINCT nv_tags.tag) = 2
这是脚本(部分):
for ($i = 0; $i < count($tags); $i++) {
$tags[$i] = trim($tags[$i]);
}
$query = "GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN (";
// add with following comma
for ($i = 0; $i < count($tags)-1; $i++) {
$query .= "'".$tags[$i]."',";
}
// add last without a comma
$query .= "'".$tags[count($tags)-1]."'";
$query .= ") HAVING COUNT(DISTINCT nv_tags.tag) = ".count($tags);
echo $query;
$entries = execSelect($query);
echo "<pre>";
print_r($entries);
echo "</pre>";