SQL多个WHERE子句问题

时间:2010-11-08 19:59:11

标签: php sql

我正在尝试修改此Modx代码段,以便它接受从db而不是默认值返回的多个值。

默认情况下,tvTags只能设置为一个变量。我对它进行了一些修改,以便将其分解为变量列表。我想查询数据库中的每个变量,并返回与每个变量关联的标记。但是,我遇到了困难,因为我对SQL和PHP很新。

我插入$ region并且它有效,但我不确定如何为$ countries变量添加更多WHERE子句。

感谢您的帮助!

if (!function_exists('getTags')) {

    function getTags($cIDs, $tvTags, $days) {

        global $modx, $parent;

        $docTags = array ();

        $baspath= $modx->config["base_path"] . "manager/includes";
        include_once $baspath . "/tmplvars.format.inc.php";
        include_once $baspath . "/tmplvars.commands.inc.php";

        if ($days > 0) {
            $pub_date = mktime() - $days*24*60*60;
        } else {
            $pub_date = 0;
        }

        list($region, $countries) = explode(",", $tvTags);

        $tb1 = $modx->getFullTableName("site_tmplvar_contentvalues");
        $tb2 = $modx->getFullTableName("site_tmplvars");
        $tb_content = $modx->getFullTableName("site_content");
        $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
        $query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
        $query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
        $query .= " WHERE stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").") ";
        $query .= " AND tb_content.pub_date >= '$pub_date' ";
        $query .= " AND tb_content.published = 1 ";
        $query .= " ORDER BY stc.contentid ASC;";

        $rs = $modx->db->query($query);
        $tot = $modx->db->getRecordCount($rs);
        $resourceArray = array();
        for($i=0;$i<$tot;$i++)  {
            $row = @$modx->fetchRow($rs);
            $docTags[$row['contentid']]['tags'] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);   
        }
            if ($tot != count($cIDs)) {
            $query = "SELECT name,type,display,display_params,default_text";
            $query .= " FROM $tb2";
            $query .= " WHERE name='".$region."' LIMIT 1";
            $rs = $modx->db->query($query);
            $row = @$modx->fetchRow($rs);
            $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);
            foreach ($cIDs as $id) {
                if (!isset($docTags[$id]['tags'])) {
                    $docTags[$id]['tags'] = $defaultOutput;
                }
            }
        }
            return $docTags;
    }
}

1 个答案:

答案 0 :(得分:2)

您不添加更多WHERE子句,在已存在的where子句中使用ANDOR。我会在你输入的行$query .= " WHERE stv.name = '".$region...之后说

foreach ($countries as $country)
{
    $query .= "OR stv.name = '{$country}', ";
}

但我不知道您希望查询如何工作。