未定义的偏移量:从下拉列表中选择时出现1个错误问题

时间:2018-10-29 13:06:37

标签: php laravel

我写了一个如下函数:奇怪为什么会出现这个错误?当。。。的时候 cat_id和选择的价格很好。 选择的价格也很好。只是当我只选择类别时,它没有任何作用,并显示错误 Undefined offset:1

$image = $_POST['image'];

$target = $_SERVER['DOCUMENT_ROOT'].'/images/';
$count = 0;

foreach ($_FILES['image']['name'] as $filename) {

  $tmp = $_FILES['image']['tmp_name'][$count];
  $count = $count + 1;
  $temp = $target.basename($filename);
  move_uploaded_file($tmp, $temp);
  $temp = '';
  $tmp = '';

}

$image_0 = $_FILES['image']['name'][0];
$image_1 = $_FILES['image']['name'][1];
$image_2 = $_FILES['image']['name'][2];
$image_3 = $_FILES['image']['name'][3];
$image_4 = $_FILES['image']['name'][4];

$db->Query("INSERT INTO user_location (image_0, image_1, image_2, image_3, image_4) VALUES ('$image_0', '$image_1', '$image_2', '$image_3', '$image_4')");

}

1 个答案:

答案 0 :(得分:0)

使用此代码,它将为您服务

public function productsCat(Request $request){
    $cat_id = $request->cat_id;
    $price = explode("-",$request->price);

    $start = $price[0];
    $end = $price[1];


    $data = DB::table('products');
    if (!empty($cat_id)) {
        $data->join('cats','cats.id','products.cat_id')
            ->where('products.cat_id',$cat_id);
    }
    if(!empty($start) && !empty($end)){
        $data->where('products.pro_price', ">=", $start)
            ->where('products.pro_price', "<=", $end)
    }
    $data->get();
    if(empty($cat_id) && empty($request->price))
    {
        return "<h1 align='center'>Please select at least one filter from drop down list</h1>";

    }
    if(count($data)==0){
        echo "<h1 align='center'>no products found under this Category</h1>";
    }
    else
    {
        return view('front.productsPage',[
            'data' => $data, 'catByUser' => $data[0]->cat_name
        ]);
    }
}