未定义的偏移量:尝试创建排序过滤器时为1

时间:2018-03-18 13:48:11

标签: php

这是我现有的过滤器的代码:

HTML:

 <select name="sortby" style="height: 40px;background: #e6e6e6;width:100%;border: 1px solid #ccc;">
<option value="HightoLow" @if(isset($returnData['sortby']) && $returnData['sortby'] == "HightoLow") selected @endif>Price High to Low</option>
<option value="LowtoHigh" @if(isset($returnData['sortby']) && $returnData['sortby'] == "LowtoHigh") selected @endif>Price Low to High</option>
 </select>

PHP(Laravel - Controller文件):

  public function sortsearch(Request $request){

    $input = $request->all(); 

    $fees = schools::where('status','')
    ->orderBy('fees', 'asc')
    ->get()
    ->pluck('fees')
    ->toArray();


    $fees = array_unique($fees);
    $priceRanges = array();
    $minPrice = 0;
    $maxPrice = 0;
    if(count($fees) > 0){
        foreach ($fees as $key => $value) {
            $range = explode(' - ', $value);
            $priceRanges[] = $range['0'];
            $priceRanges[] = $range['1'];
        }
        $priceRanges = array_unique($priceRanges);
        $minPrice = (int) min($priceRanges) ?? 0;
        $maxPrice = (int) max($priceRanges) ?? 0;
        // dd($maxPrice);
    }

    $Ranges = array();
    if (isset($request['FeesRange']) && !empty($request['FeesRange'])) {
        $Rfees = explode(' - ',$request['FeesRange']);
    }else{
        $Ranges = explode(' - ',$request['fees']);
    }

    $returnData['Ranges'] = $Ranges;
    $returnData['Ranking'] = $Ranking;
    $returnData['sortby'] = $request['sortby'];
    $returnData['FeesRange'] = $request['FeesRange'];
    //dd($Rfees);
    $sortby=$request['sortby'];
    $search=$request['search'];
    //dd($fees);

 if (isset($request['FeesRange']) && !empty($request['FeesRange'])) {
        $result = $result->where('fees','like',$request['FeesRange']);
    } else {
        $result = $result->where(DB::raw("SUBSTRING_INDEX(fees, ' - ', 1)"), '>=', (int) $Ranges[0])
                         ->where(DB::raw("SUBSTRING_INDEX(fees, ' - ', -1)"), '<=', (int) $Ranges[1]);
    }

 if($sortby == "LowtoHigh"){
        $result = $result->orderBy('fees', 'asc');
    }else{
        $result = $result->orderBy('fees', 'desc');
    }

    $result = $result->paginate();

我正在尝试创建另一个过滤器,但不是分类费用,而是按排名排序(例如:24th,23rd,4th,5th)

我已经尝试复制我收费的代码,但我错了:未定义的偏移量:1

错误发生在此行(不知道为什么它出现在费用代码上,因为费用过滤器有效):

                         ->where(DB::raw("SUBSTRING_INDEX(fees, ' - ', -1)"), '<=', (int) $Ranges[1]);

来自这段代码:

    if (isset($request['FeesRange']) && !empty($request['FeesRange'])) {
        $result = $result->where('fees','like',$request['FeesRange']);
    } else {
        $result = $result->where(DB::raw("SUBSTRING_INDEX(fees, ' - ', 1)"), '>=', (int) $Ranges[0])
                         ->where(DB::raw("SUBSTRING_INDEX(fees, ' - ', -1)"), '<=', (int) $Ranges[1]);
    }

    if (isset($request['PhilippinesRanking']) && !empty($request['PhilippinesRanking'])) {
        $result = $result->where('ranking_philippines','like',$request['PhilippinesRanking']);
    } else {
        $result = $result->where(DB::raw("SUBSTRING_INDEX(ranking_philippines, '', 1)"), '>=', (int) $Ranking[0])
                         ->where(DB::raw("SUBSTRING_INDEX(ranking_philippines, '', -1)"), '<=', (int) $Ranking[1]);
    }

我拿出了“ - ”,因为费用过滤器需要它。费用数据库字段中的数据如下所示:10000-20000。我不需要连字符用于排名过滤器。

0 个答案:

没有答案