Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have got a serious problem about fetching data into the view after getting all the values from multiple checkboxes. I can fetch the last value checked, but no more.

Here's my controller:

public function getFilterPaytv(){

    $valore =  Input::get('opt');
    if(!empty($valore))
    foreach ($valore as $val) {

        $results = Tpaytv::where('Desc', 'LIKE', '%' . $val . '%')->get();

        echo $val . ""; // 

        // echo $results
    }

    return View::make('result')->with('results', $results);
}

and here's my view

@if($results->count())
    @foreach($results as $pa)

            <div class="col-lg-12 ">
               <div class="box">
                  <div class="row">
                      <div class="col-lg-2"><img src="img/T.png" class="timC"></div>
                  <div class="col-lg-4">
                     <ul class="boxCar">
                        <li>Pro: {{$pa->Type}}</li>
                        <li>Sca: {{ date("d/m/Y",strtotime($pa -> S))}}</li>
                        <li 
                             data-toggle="modal" 
                             data-target="#{{ $pa->id }}">
                             <a>Magg</a></li>
                     </ul>
                  </div>
                      <!--inizio modal -->

                  <!-- fine modal -->
                  <div class="col-lg-3"><h1 class="ads">{{$pa->T}}€</h1></div>
                  <div class="col-lg-3">

                   {{ 
                       link_to_route('confr.show',
                       'request ', 
                       array($paytv->id), 
                       array('class' => 'btn btn-green btn-md offButton'))
                   }}
                  </div>
               </div>
            </div>
         </div>
    @endforeach
@else
no
@endif
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are still a number of issues with your code that need to be addressed, but the reason why you're only getting the last result of the checkbox set, assuming that the checkbox values are coming from $valore = Input::get('opt');, is because when you loop through the values in your foreach, you're overwriting your results variable.

In your code:

foreach ($valore as $val) {

    $results = Tpaytv::where('Desc', 'LIKE', '%' . $val . '%')->get();

    echo $val . ""; // 

    // echo $results
}

The variable $result has not been declared until the first iteration of your loop which means that in your first loop $result is set to the result of your Tpaytv::where method call and then on the second loop the value in $result is being overwritten by the next result from the Tpaytv::where method call. This is why you're only getting the last value checked; it's the last value looped over in your foreach.

If you want to get a sack of results, you need to declare the $result variable as an empty array before the foreach loop and then push the results into the array:

// Create your empty array
$results = array();

foreach ($valore as $val) {

    // Push the results of the method call into the array.
    // This will keep them from being overwritten in your foreach loop. 
    $results[] = Tpaytv::where('Desc', 'LIKE', '%' . $val . '%')->get();

    echo $val . ""; // 

    // echo $results
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...