Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

php - Laravel multiple checkbox data fetch

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

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

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...