Search code examples
phphelper

Undefined variable in foreach


I'm doing a website using MVC method. I'm having a little problem trying to create some html . I use database queries. I have a page with stats (best goalscorers,assist and player with most cards). For the best goalscorers it works fine, but for the other two i get the famous

Notice: Undefined variable: assists in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\app\views\show_stats.view.php on line 23

    public function show_stats()
    {

     $players = Player::fetchByGoal();
     $cards = Player::fetchByCards();
     $assists = Player::fetchByAssists();
      return Helper::view("show_stats",[
                'players' => $players],[
                'cards' => $cards],[
                'assists' => $assists]);
    }

And I post my 3 foreach loops, the first one works, the other two don't.

<?php foreach ($players as $player) {
        echo $player->asHTMLTableGoals();
    }?>
<?php foreach ((array)$assists as $assist) {
       echo $assist->asHTMLTableAssists();
    }?>
<?php foreach ((array)$cards as $card) {
       echo $card->asHTMLTableCards();
    }?>

Any idea of why I got the error to the variable assists and cards ? Thank you in advance !


Solution

  • I think you need to put all of the data into one array and not separate arrays...

    return Helper::view("show_stats",[
                    'players' => $players],[
                    'cards' => $cards],[
                    'assists' => $assists]);
    

    should be

    return Helper::view("show_stats",[
                    'players' => $players,
                    'cards' => $cards,
                    'assists' => $assists]);