按两件事对电影进行排序 [英] Sorting Movies By 2 Things

查看:65
本文介绍了按两件事对电影进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hay everyone need abit of help i a noob when it come to php :(

在我的数据库中,我有一个称为电影的表,并且在那里有12列,但是我用2列对电影进行排序,称为年,另外1个是imdb_rating

In my database i have a table called movies and i have 12 columns in there but i used 2 collumns for sorting my movies and there are called year and the other 1 is imdb_rating

这是我对它们进行排序的方式

this is how i sort them

 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件事进行排序

Would it be possible to sort by 2 things for example

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

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

Alternatively, if you'd like to order them differently (for example by year and then rating), use:

ORDER BY `year` DESC, `imdb_rating` ASC

还值得注意的是,现在不建议使用mysql_*函数集.最好使用 MySQLi

It's also worth noting that the mysql_* set of function is now deprecated. It'd be better to use MySQLi or PDO.

这篇关于按两件事对电影进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆