旧版主题筛选函数

C 11天前 189

要做新的了,把老的放出来晒晒……

性能上没太大问题,但是数据结构是写死的,灵活性太差。

正在研究新函数,要换一种计算方式了。

function get_b($b,$k,$v,$t,$a){
$pdo=init();
$needn=$a+1;$needl=$a;$bq=count($b);
if(!$v){$samen=array();$samel=array();}
else{
$sql='';
if($bq>1){$sql.='SELECT * FROM (';}
$crc=$bq;
foreach($b as $row){
$crc--;
$sql.='(SELECT `c`,`d`,`t` FROM `b` WHERE `b`="'.$row.'" AND `'.$k.'`='.$v.' '.($t?'AND `t`<='.$t:'').' ORDER BY `'.$k.'` DESC, `t` DESC LIMIT '.($a+1).')';
if($crc){$sql.=' UNION ALL ';}
}
if($bq>1){
$sql.=') AS `tempn` ORDER BY `'.$k.'` DESC, `t` DESC LIMIT '.($a+1);
}
$sql.=' ; ';
if($t){
if($bq>1){$sql.='SELECT * FROM (';}
$crc=$bq;
foreach($b as $row){
$crc--;$sql.='(SELECT `c`,`d`,`t` FROM `b` WHERE `b`="'.$row.'" AND `'.$k.'`='.$v.' AND `t`>'.$t.' ORDER BY `'.$k.'` ASC, `t` ASC LIMIT '.$a.')';
if($crc){$sql.=' UNION ALL ';}
}
if($bq>1){$sql.=') AS `templ` ORDER BY `'.$k.'` ASC, `t` ASC LIMIT '.$a;}
}
$sameq=$pdo->query($sql);
$samen=$sameq->fetchAll(PDO::FETCH_ASSOC)?:array();
if($t){$sameq->nextRowset();$samel=$sameq->fetchAll(PDO::FETCH_ASSOC)?:array();}
else{$samel=array();}
}
$needn-=count($samen);$needl-=count($samel);
if(!$needn && !$needl){
$diffn=array();$diffl=array();
}
else{
$sql='';
if($needn){
if($bq>1){$sql.='SELECT * FROM (';}
$crc=$bq;
foreach($b as $row){
$crc--;
$sql.='(SELECT `c`,`d`,`t` FROM `b` WHERE `b`="'.$row.'" '.($v?'AND `'.$k.'`<'.$v:'').' ORDER BY `'.$k.'` DESC, `t` DESC LIMIT '.$needn.')';
if($crc){$sql.=' UNION ALL ';}
}
if($bq>1){
$sql.=') AS `tempn` ORDER BY `'.$k.'` DESC, `t` DESC LIMIT '.$needn;
}
$sql.=' ; ';
}
if($needl && $v){
if($bq>1){$sql.='SELECT * FROM (';}
$crc=$bq;
foreach($b as $row){
$crc--;$sql.='(SELECT `c`,`d`,`t` FROM `b` WHERE `b`="'.$row.'" AND `'.$k.'`>'.$v.' ORDER BY `'.$k.'` ASC, `t` ASC LIMIT '.$needl.')';
if($crc){$sql.=' UNION ALL ';}
}
if($bq>1){$sql.=') AS `tempn` ORDER BY `'.$k.'` ASC, `t` ASC LIMIT '.$needl;
}
}
$diffq=$pdo->query($sql);
$diffn=$needn?($diffq->fetchAll(PDO::FETCH_ASSOC)?:array()):array();
if(!$needl || !$v){$diffl=array();}
else{if($needn){$diffq->nextRowset();}$diffl=$diffq->fetchAll(PDO::FETCH_ASSOC)?:array();}
}
$needn-=count($diffn);$needl-=count($diffl);
$res=array();
$res['t']=array_merge($samen,$diffn);
$res['b_next']=$needn?null:array_pop($res['t']);
$res['b_last']=($needl==$a)?null:end(array_merge($samel,$diffl));
$res['t']=array_column($res['t'],'t');
return $res;
}
最新回复 (0)
    • 屌丝论坛
      2