mercredi 27 juin 2018

Looping through all rows with php and js

Within a button click I want to loop through all rows of the table, my code:

@foreach( $questions as $q)


<script>


    var js_array = [<?php echo '"'.implode('","', $q).'"' ?>];



    var nextQuestion = (function() {
        var questionArray = js_array;
        var i = 0;
        return function() {
            $('#results').html(questionArray[i%questionArray.length]);
            i++;
        }
    })();


</script>

Controller logic in case:

$questions = DB::table('questions')->orderBy(DB::raw('RAND()'))->get();


$questions= json_decode( json_encode($questions), true);

return view('test', ['questions' => $questions]);

Problem is it only loops through the last row and not all the rows, can't see why.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire