简体   繁体   中英

How can I do this sql filter results

I have an website there are 5 categories, i only want to show the 1-4 category only in the new posts..how can I edit the code to do this job? thanks

Here is my code, and I do that in my sql by using following code:

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

but i dont know how to do that in php code. following is my part of php code:

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

I think you should try and understand the code first before you modify it ... but anyway:

You would only need to append the condition to the where clause generated in the PHP code. You could do that by adding THE LINE MARKED "THIS LINE IS NEW" after this line:

$Where = '';

And this is the relevant part of the code:

$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 .......

As you can see any redundant initial "and"s are replaced with "where" here:

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

so this should work - assuming that $Get['type'] == 'New' means that this is a new post which I can only guess.

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