2010-11-08 93 views
0

我正在嘗試修改這個Modx片段,以便它接受從db而不是默認值返回的多個值。SQL多個WHERE子句問題

默認情況下,tvTags僅用於設置一個變量。我對它進行了一些修改,以便將它分解爲變量列表。我想查詢數據庫中的每個變量並返回與每個變量關聯的標籤。然而,我很困難,因爲我對SQL和PHP相當陌生。

我插入$ region,它的工作原理,但我真的不知道如何爲$國家變量添加更多的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; 
    } 
} 

回答

2

你不更WHERE條款添加,您使用AND S和OR S中已經存在的where子句。我會說在$query .= " WHERE stv.name = '".$region...之後你放

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

但是我不知道你希望查詢如何工作。

+0

感謝您的回答!它似乎沒有工作,因爲$國家不是一個數組。我真的只需要查詢$ region中包含的值並返回與之關聯的標籤,然後查詢與$國家相關聯的值並返回標籤。 $ region =「地區」和$國家=「國家」抱歉的混亂。 – Vecta 2010-11-08 20:29:26

+0

哦,對不起,沒問題。沒有循環的同樣的解決方案,'$ query。=「或stv.name ='{$ countries}',」;' – 2010-11-09 07:20:08