vendredi 31 mars 2017

Laravel - method merge

I did a method to filter the results, I want for example looking for a hotel in a city and whose price and between 0 and 49, I have 4 checkbox 0-49, 50-99,100-149,150-200 I want If I click on the first it gives me the names of the hotels whose city given in query and the price that is in the value of checkbox

For example:

If I look for the city Casablanca, it gives me the name of the hotels that have in Casablanca and when I click on the checkbox 0-49 it gives me the hotels that have in Casablanca and whose price is between 0 and 49 and so on For other checkboxes when I click on the first checkbox that contains the value 0-49 and the second one that contains the value 50-99, it gives me the hotels that have in Casablanca and whose price is between 0 and 49 and 50 -99 and so on

The method works very well, the problem is in pagination, when I click on a single checkbox it gives the exact page number and the number of element to display in the pages is 25

$ Perpage = 25;

I have made $ product to display the names of hotels and $ afica to calculate the total number of results, because the method "totalHits ()" only works with the get () method,

$ Books = $ product [0]; // this line stores the first results that is to say when I have a single checkbox but when I click on several checkbox it enters the loop for:

for($i=1;$i<count($product);$i++){

            $collection  = new Collection();
            $collection2 = new Collection();

            $collection= $product[$i];

            $nbPages = floor($res/$perpage);

            $books = $collection2->merge($books)->merge($collection);

            }

My problem is:

When I did the merge method to combine the two results it is to say:

$ Books = $ product [0]; And $ collection = $ product [$ i]; $ Books = $ collection2-> merge ($ books) -> merge ($ collection); It is the only method that worked with me but the problem is that I want to show 25 elements per page, it shows me the 25 elements of the first results and 25 of the second is you say Example $ books = $ product [0]; And $ books = $ product [1]; And that he gives me another page in the pagination that is empty, because the results that had to be displayed in the empty page it has to display in the first page.

For example, if the total number of the first results is $ books = $ product [0]; It gives me 42 element, and $ collection = $ product [$ i = 1]; Gives me 87 so the page number is 129/25 it gives me 5 page, it shows me in a first page 50 element and so follows what it does in the 5th page is empty because it displayed in The other page

How to solve this problem?

I use Elasticsearch and Laravel.

My function :

public function pricefilter(Request $request)
{
    $query = Input::get('query', false);
    $brans = request()->priceFilter;
    $perpage = 25;

    if ($query = Input::get('query', false))  {  

        foreach ($brans as $data) 
        {

            $minMax    = explode("-",$data);
                $product[] = Afica::search()
                                 ->multiMatch(['name','city_hotel'], $query, ['fuzziness' => 'AUTO'])
                                 ->filter()
                                 ->range('minrate',['from'=>$minMax[0],'to'=>$minMax[1]])
                                 ->paginate($perpage)
                                 ->appends('priceFilter' , request('priceFilter'));

                 $afica[] = Afica::search()
                                 ->multiMatch(['name','city_hotel'], $query, ['fuzziness' => 'AUTO'])
                                 ->filter()
                                 ->range('minrate',['from'=>$minMax[0],'to'=>$minMax[1]])
                                 ->get();     

        }


        $books = $product[0];
        $af = $afica[0];
        $res = $af->totalHits();
        $nbPages = ceil($res/$perpage);
        $kaa = new Collection();

        for($i=0;$i<count($afica);$i++)
        {  

            $cole =  $afica[$i];
            $cole = $cole->totalHits();
            $ka = $kaa->push($cole);
        }                    

        $res = $ka->sum();

        for($i=1;$i<count($product);$i++){

            $collection  = new Collection();
            $collection2 = new Collection();

            $collection= $product[$i];

            $nbPages = floor($res/$perpage);

            $books = $collection2->merge($books)->merge($collection);

            }

   } else {
        $books = Afica::search()
                 ->paginate(26);
   }

   if ($request->ajax()) {
       return view('presult2', compact('books','nbPages','pageActuelle','brans'));
   }

   return view('welcome2',compact('books','nbPages','pageActuelle','brans')); 
}

my view :

@foreach($books as $key => $value)

            <article class="one-third">
              <figure><a href="#" title=""><img src="" alt="" style="height: 215px!important;" /></a></figure>

              <div class="details">

                <h3>
                  <span class="stars"> 
                    <i class="material-icons">&#xE838;</i>
                    <i class="material-icons">&#xE838;</i>
                    <i class="material-icons">&#xE838;</i>
                    <i class="material-icons">&#xE838;</i>
                  </span>
                </h3>
                <span class="address">,  <a href="hotel?query=">Show on map</a></span>
                <span class="rating"></span>
                <span class="price">Max rate  <em>$ </em> </span>
                <span class="price">Min rate  <em>$ </em> </span>
                <div class="description">
                  <p><a href="hotel.html">More info</a></p>
                </div>   
                <a href="" title="Book now" class="gradient-button">Book now</a>
              </div>
            </article>

            <!--//deal-->
            @endforeach

<!--//bottom navigation-->
 <input type="hidden" id="nbPages" value="<?php echo $nbPages;?>">
            <div class="bottom-nav">
            <a href="#" class="scroll-to-top" title="Back up">Back up</a> 
            <div id="pager">
            <?php $query=Input::get('query', '');?>
            @if($query == "")
          <span><a href="?page=1">First Page</a></span>
            @else
         <span><a href="?query=<?php echo $query?>&page=1"=>FirstPage</a></span>
         @endif

<?php


$query = Input::get('query', '');

for($i=1; $i<=$nbPages; $i++) //On fait notre boucle
{
    echo '<span><a href="&page='.$i.'">'.$i.'</a></span>';    
}

?>


       @if($query == "")
          <span><a href="">Last Page</a></span>
            @else
         <span><a href="">Last Page</a></span>
         @endif
            <!--bottom navigation-->
              </div>
         </div>    
            <!--//bottom navigation-->



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ogEdbt
via IFTTT

Aucun commentaire:

Enregistrer un commentaire