gpt4 book ai didi

php - 按 2 件事对电影进行排序

转载 作者:行者123 更新时间:2023-12-01 00:49:12 26 4
gpt4 key购买 nike

当谈到 php 时,每个人都需要一些帮助,我是菜鸟 :(

在我的数据库中,我有一个名为 movies 的表,其中有 12 列,但我使用了 2 个列来对我的电影进行排序,其中一个名为 year,另一个为 imdb_rating

我是这样分类的

 public function getRealMovies($lang=null, $p=null, $l=null, $sortby=null){
$movies = array();

if (($p) && ($l)){
$start = ($p-1)*$l;
$limit = " LIMIT $start,$l";
} else {
$limit = '';
}

if (!$sortby || $sortby=='abc'){
$order = "ORDER BY title ASC";
} elseif ($sortby=='date'){
$order = "ORDER BY date_added DESC";
} elseif ($sortby=='imdb_rating'){
$order = "ORDER BY imdb_rating DESC";
} elseif ($sortby=='year'){
$order = "ORDER BY year DESC";
}

$e = mysql_query("SELECT * FROM movies WHERE id IN (SELECT movie_id FROM movie_embeds) $order $limit") or die(mysql_error());
if (mysql_num_rows($e)>0){

$ids = array();

while($s = mysql_fetch_array($e)){
$movies[$s['id']] = $this->formatMovieData($s, $lang);
$ids[] = $s['id'];
}

if (count($ids)){

$flags = $this->getFlags($ids);

if (count($flags)){
foreach($movies as $movie_id => $val){
if (array_key_exists($movie_id,$flags)){
$movies[$movie_id]['languages'] = $flags[$movie_id];
} else {
$movies[$movie_id]['languages'] = array();
}
}
}
}

}
return $movies;
}

是否可以按 2 个事物排序

     } elseif ($sortby=='imdb_rating and year'){
$order = "ORDER BY imdb_rating and year DESC";

希望有人能帮上忙

谢谢

最佳答案

只需用逗号分隔列:

elseif($sortby == 'imdb_rating and year')
{
$order = 'ORDER BY `imbd_rating`, `year` DESC';
}

或者,如果您想以不同方式对它们进行排序(例如按年份然后按评级),请使用:

ORDER BY `year` DESC, `imdb_rating` ASC

还值得注意的是,mysql_* 函数集现已弃用。最好用MySQLiPDO .

关于php - 按 2 件事对电影进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17920068/

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