使用php和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.

You must add a loop function within the script. On the top it looks like you want to output each return.

But the returns must be iterated.

e.g. loop like described here https://www.w3schools.com/js/js_loop_for.asp

What you are doing is to set i to 0, then return...and done. But you must loop over and over.