簡體   English   中英

我該怎么做這個SQL過濾結果

[英]How can I do this sql filter results

我的網站有5個類別,我只想在新帖子中僅顯示1-4個類別。如何編輯代碼來完成此工作? 謝謝

這是我的代碼,我通過使用以下代碼在sql中執行此操作:

 SELECT * FROM `ff_se` WHERE Wid in ('1','2','3','4')"

但是我不知道如何在php代碼中做到這一點。 以下是我的php代碼部分:

$this->TableSe = 'ff_se';
    $this->TableSeWord = 'ff_se_word';
    $this->TableSeSupport = 'ff_se_support';
    $this->TableSePost = 'ff_se_post';
    $this->TableSePostTableId = 'ff_se_post_tableid';

public function GetAjaxList($Get){
    $Results = array();
    $Get = $this->StrToGBK($Get);
    $Page = $Get['page'] ? intval($Get['page']):0;

    $Where = '';
    $Order = 'S.updateline';
    if($Get['type'] == 'New'){
        $Order = 'S.dateline';
    }else if($Get['type'] == 'Hot'){
        $Order = 'S.updateline';
        if($this->Config['PluginVar']['ListHotVal']){
            $Where .= ' and (S.support_count >= '.$this->Config['PluginVar']['ListHotVal'].' OR S.comment_count >= '.$this->Config['PluginVar']['ListHotVal'].')';
        }
        if($this->Config['PluginVar']['ListHotDataline']){
            $Where .= ' and S.dateline >= '.strtotime("-".$this->Config['PluginVar']['ListHotDataline']." hours",time());   
        }
    }else if($Get['type'] == 'Nearby'){//
        $Order = 'S.updateline';
        if($Get['lng'] && $Get['lat']){
            $SquarePoint = $this->GetReturnSquarePoint($Get['lng'],$Get['lat'],$this->Config['PluginVar']['Distance']);
            $Where .= ' and S.lat <> 0 and S.lat > '.$SquarePoint['right-bottom']['lat'].' and S.lat < '.$SquarePoint['left-top']['lat'].' and S.lng > '.$SquarePoint['left-top']['lng'].' and S.lng < '.$SquarePoint['right-bottom']['lng'];
        }else{
            return $Results;
        }
    }

    if($_GET['wid']){
        $Where .= ' and S.wid = '.intval($_GET['wid']);
    }
    $Where .= ' and S.display = 1 and S.fast_add_display = 1';
    $Where = preg_replace('/and/','where',$Where,1);

    $this->Config['PluginVar']['ListNum'] = $this->Config['PluginVar']['ListNum'] ? $this->Config['PluginVar']['ListNum'] : 10;
    $Limit = 'LIMIT '.($Page * $this->Config['PluginVar']['ListNum']).','.$this->Config['PluginVar']['ListNum'];

    $FetchSql = 'SELECT W.title as Ttitle,S.* FROM '.DB::table($this->Tablese).' S LEFT JOIN '.DB::table($this->TableSeWord).' W on W.id = S.wid '.$Where .' order by topdateline > '.time().' desc,'.$Order.' desc,S.dateline desc '.$Limit;
    $Results = $this->ListFormat(DB::fetch_all($FetchSql));
    return $Results;
}

我認為您應該先嘗試理解代碼,然后再對其進行修改……但是無論如何:

您只需要將條件附加到PHP代碼中生成的where子句中即可。 您可以通過在此行之后添加標記為“此行是新行”的行來做到這一點:

$Where = '';

這是代碼的相關部分:

$Where = '';
$Order = 'S.updateline';
if($Get['type'] == 'New'){
    $Where .= ' and S.wid in (1,2,3,4)'; //THIS LINE IS NEW
    $Order = 'S.dateline';
}else .......

如您所見,這里所有多余的初始“和”都被替換為“哪里”:

$Where = preg_replace('/and/','where',$Where,1);

所以這應該工作-假設$Get['type'] == 'New'意味着這是我只能猜測的新帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM