保持双重价值

时间:2012-07-01 19:42:38

标签: php arrays

我有以下脚本可以根据标记

从数据库中获取内容
   <?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服务器版本对应的手册,以便在'GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN'第1行附近使用正确的语法

在下面的脚本中生成的查询是这样的:

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>";

0 个答案:

没有答案