0

I'm programming a tourism agency website. I've hotels in database which have a column called stars which is an integer its value is between 1 to 5.

I have a form which the user can search hotels based on their stars. The field is checkbox so he can search five stars and four stars hotels together for example.

I read the stars array and redirect the visitor to the search page using this code:

$stars_param = "";
    $i = 1;
    foreach($stars as $star){
        if($i == 1){
            $stars_param .= $star; 
        }else{
            $stars_param .= "&".$star;
        }
        $i++;
    }
    $parameters = "filter=true&&stars=".$stars_param."&&price=".$price."&&area=".$area;
    return \Redirect::to('/hotels-search/?'.$parameters); 

So the url will be like this:

hotels-search?filter=true&&stars=5&1&&price=300&&area=taksim

And in the hotels-search page I read the $_GET , and explode the stars variable and coded this query to fetch data:

$stars = $_GET['stars'];
$stars_array = explode('&', $stars);
$price = $_GET['price'];
$area = $_GET['area'];
$this['hotels'] = \Lilessam\Hotels\Models\Hotel::orderBy('id', 'desc')->where(function($query) use($stars_array){
                $i = 1;
                foreach($stars_array as $star){
                    if($i == 1){
                        $query->where('stars', $star);
                    }else{
                        $query->orWhere('stars', $star);
                    }
                    $i++;
                }

                })->where('price', '<=', $price)->whereHas('area', function($query) use($area){
                                $query->where('name', 'LIKE', $area);
                                })->paginate(5);

But with this if I search hotels with 5 and 1 stars, I only get the five stars hotels!

if I search hotels with 4 and 3 stars, I only get the four stars hotels ! Other times I get nothing from database at all !!

How can I make a query so I can get hotels with 1 or 3 or 5 stars at the same time?!

2 Answers 2

1

Why don' you use this

$this['hotels'] = \Lilessam\Hotels\Models\Hotel::orderBy('id', 'desc')
                    ->whereIn('start',$stars_array)
                    ->where('price', '<=', $price)
                    ->whereHas('area', function($query) use($area){
                            $query->where('name', 'LIKE', $area);
                            })->paginate(5);
Sign up to request clarification or add additional context in comments.

Comments

0

get variables are separated by single &, not &&. every name value pair has the form:- name=value.

Try this.

$stars_param = implode(",",$stars);
$parameters = "filter=true&stars=".$stars_param."&price=".$price."&area=".$area;
    return \Redirect::to('/hotels-search/?'.$parameters);

Then your URL will be:

hotels-search?filter=true&stars=5,1&price=300&area=taksim

And in the hotel search page

$stars = $_GET['stars'];
$stars_array = explode(',', $stars);
$price = $_GET['price'];
$area = $_GET['area'];

Hope this will help you...

1 Comment

Actually I was separating them with only one & It's been resolved when I used whereIn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.