gpt4 book ai didi

php - 无论 codeigniter 中的排序如何,我都希望在顶部有一组特定的记录

转载 作者:行者123 更新时间:2023-11-29 03:16:36 26 4
gpt4 key购买 nike

以下函数以二维数组格式返回所有玩家。下面是函数:-

public function get_football_players($type = FOOTBALL_TEXT, $filter = '', $start = '', $limit = '', $player = '') {
$this->db->select('tblmarkets_football.*, tblmarkets.symbol, tblmarkets.type');
$this->db->from('tblmarkets_football');
$this->db->join('tblmarkets','tblmarkets.id = tblmarkets_football.market_id','left');
if($type != ''){
$this->db->where('type', $type);
}
$this->db->where('active', 1);
switch ($filter) {
case 'forward':
$this->db->where('position', 'Forward');
break;
case 'midfielders':
$this->db->where('position', 'Midfielder');
break;
case 'defenders':
$this->db->where('position', 'Defender');
break;
case 'goalkeepers':
$this->db->where('position', 'Goalkeeper');
break;
case 'highest_price':
case 'lowest_price':
$tmp = $this->external_api_model->get_filtered_football_pairs($limit, $start, $filter);
$players = json_decode($tmp, true);
$this->db->where_in('symbol', $players);
break;
}
if($player != ''){
$this->db->like('tblmarkets_football.name', $player);
}
if($filter != 'highest_price' && $filter != 'lowest_price'){
$this->db->limit($limit, $start);
}
$result = $this->db->get()->result();
return $result;
}

当有人应用最高价格过滤器时,get_filtered_football_pairs API 会以数组格式返回所有球员。我希望所有这些玩家都按照我收到的顺序显示在顶部。 $players 包含所有玩家的名字。目前它们是随机显示的。

任何帮助将不胜感激。提前致谢。

编辑

$players 将如下所示:-

Array
(
[0] => paul-pogba
[1] => neymar
[2] => kylian-mbappé
[3] => lionel-messi
[4] => jadon-sancho
[5] => eden-hazard
[6] => marcus-rashford
[7] => mohamed-salah
[8] => harry-kane
[9] => vinicius-junior
[10] => callum-hudson-odoi
[11] => raheem-sterling
[12] => ousmane-dembele
[13] => anthony-martial
[14] => paulo-dybala
[15] => gareth-bale
[16] => joshua-kimmich
[17] => cristiano-ronaldo
[18] => kai-havertz
[19] => leroy-sané
)

我想要的顺序是首先显示 paul-pogba,然后是 neymar 等等。

最佳答案

这是未经测试的,但逻辑是您转义 $player 数组中的元素,然后将优先处理应用于符合条件的 symbol 值,然后提供次要排序顺序为 symbol 升序(否则在将玩家与非玩家分开后让事情随机。

case 'highest_price':
case 'lowest_price':
$tmp = $this->external_api_model->get_filtered_football_pairs($limit, $start, $filter);
$players = json_decode($tmp, true);
$this->db->where_in('symbol', $players);
foreach ($players as &$p) {
$p = "'" . $this->db->escape_str($p) . "'";
}
$this->db->order_by("IF(FIELD(symbol," . implode(",", $players) . ")=0,1,0),
FIELD(symbol," . implode(",", $players) . ")");
break;

这是一个 db-fiddle 演示,展示了如何执行排序:https://www.db-fiddle.com/f/9yxTo3nEHCu9vLPW6MELy3/1

帮助理解我的技术:https://dba.stackexchange.com/a/109126/157408

解释子句的0,1部分...

它将$players 分隔在非$players 之前。所有 $players 都将在 0 组中,非 $players 将在 1 中团体。然后二次排序会将 $players 置于您的首选顺序(打破 0 并列)。


附注我认为将 $start$limit 的默认值设置为空字符串是不明智的——数字值似乎更适合。

我通常尽量远离开关 block ,因为我觉得它太冗长了。另一种样式可能如下所示(根据您的预期输入和逻辑要求,您可以进一步简化它):

if (in_array($filter, ['forward', 'midfielders', 'defenders', 'goalkeepers'])) {
$this->db->where('position', ucfirst(rtrim($filter, 's')));
}

if (in_array($filter, ['highest_price', 'lowest_price'])) {
$tmp = $this->external_api_model->get_filtered_football_pairs($limit, $start, $filter);
$players = json_decode($tmp, true);
$this->db->where_in('symbol', $players);
foreach ($players as &$p) {
$p = "'" . $this->db->escape_str($p) . "'";
}
$this->db->order_by("IF(FIELD(symbol," . implode(",", $players) . ")=0,1,0),
FIELD(symbol," . implode(",", $players) . ")");
} else {
$this->db->limit($limit, $start);
}

if ($player != '') {
$this->db->like('tblmarkets_football.name', $player);
}

关于php - 无论 codeigniter 中的排序如何,我都希望在顶部有一组特定的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55120450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com