简体   繁体   中英

SQL Multiple WHERE Clause Problem

I'm attempting the modify this Modx Snippet so that it will accept multiple values being returned from the db instead of the default one.

tvTags, by default, was only meant to be set to one variable. I modified it a bit so that it's exploded into a list of variables. I'd like to query the database for each of these variables and return the tags associated with each. However, I'm having difficulty as I'm fairly new to SQL and PHP.

I plugged in $region and it works, but I'm not really sure how to add in more WHERE clauses for the $countries variable.

Thanks for your help!

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

You don't add in more WHERE clauses, you use AND s and OR s in the already existing where clause. I would say after the line $query .= " WHERE stv.name = '".$region... you put in

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

but I don't know how you want the query to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM