使用AJAX进行多重过滤结果

时间:2015-04-28 07:42:25

标签: javascript jquery ajax laravel filter

我目前正在使用Laravel对数据库进行AJAX调用来检索数据库中的内容。以前我正在做语言选择器来过滤视频的语言。但现在我打算将它改为多语言选择器。我谷歌进行多重结果过滤,但它们都不适合我。当用户选择一种语言时,我正在使用AJAX调用。对于当前情况,我们有多个具有多个选择选项的AJAX调用。

Javascript AJAX (当您选择新语言时)

var language = $(this).text();
var channel = "{{ $channel }}";

$.ajax({
            url:'/directory/updateLanguage/live/'+language+'/'+channel,
            type: 'GET',
            success: function(data) {
                $('#live').html(data);
            }
        });

        $.ajax({
            url:'/directory/updateLanguage/past/'+language+'/'+channel,
            type: 'GET',
            success: function(data) {
                $('#past').html(data);
            }
        });

URL将是Controller下面的执行方法:

public function updateLanguage($type,$language,$channel)
{
$categoryId = Channel::where('name','=',strtolower($channel))->pluck('category_id');

    if ($language == "All Languages" && $categoryId == 0)
    {
        var_dump("IF All languages and category all");
        $past = Videos::orderBy('created_at','desc')->where('active','=',1);
        $live = Videos::where('stream_active','=','1')->where('active','=',1)->orderBy('created_at','desc');    
    }else if ($categoryId != 0) {
        var_dump("Any languages and Not in 'all' ");
        $past = Videos::where('category_id','=', $categoryId)->where('stream_active','=','0')->where('active','=',1)->where('language','=',$language)->orderBy('created_at','DESC');
        $live = Videos::where('category_id','=', $categoryId)->where('stream_active','=','1')->where('active','=',1)->where('language','=',$language)->orderBy('created_at','DESC');
    }else {
        var_dump("ELSE");
    }
...
}

我目前想要过滤多种语言视频。我有一个视频列表div,我只知道如何过滤一个语言视频而不是数据库中的多语言选择器。

1 个答案:

答案 0 :(得分:0)

您可以设置langage变量,如:fr.en.nl

然后,在PHP中,您将字符串拆分为

$str = 'fr.en.nl';
$langages= preg_split('.', $str);

最后,使用ORWHERE(Laravel

设置SQL请求
->where('language','=',$languages[0])->orWhere('language','=',$language[1])