Search code examples
phpmysqltwiginner-join

Get twig value inner join sql


So i'd like to get the values from my sql query into my twig. Easy, but now i have this INNER JOIN query and i just can't get the values... Here's the code :

  public function getList() {
        $game= array();
        $req = "SELECT * FROM JEU INNER JOIN CATEGORIE ON CATEGORIE.IDCATEGORIE = JEU.IDCATEGORIE";
        $stmt = $this->_db->prepare($req);
        $stmt->execute();
        while ($donnees = $stmt->fetch())
        {
            $game[] = new Game($donnees);
        }
        return $game;
    }

The index.php

    if (isset($_GET["action"]) && $_GET["action"]=="liste")
{ $game= $gameManager->getList();

  echo $twig->render('game_list.html.twig',array('game'=>$game)); 
}

And the view

    {% extends "index.html.twig" %}

{% block section %}
<table class="table table-hover table-condensed"><thead>
<tr><th>Name</th><th>Logo</th><th>Cat</th><th>Time</th><th>Players</th></tr>
</thead><tbody>
{% for game in game%}


      <tr><td>{{game.name}}</td><td>{{game.logo}}</td><td>{{game.cat}}</td><td>{{game.time}}</td><td>{{game.player}}</td><td>
</tr>
{% endfor %}
</tbody></table>
{% endblock %}

Dump NULL everytime... Thanks for you help


Solution

  • You're using the game variable twice:

    {% for game in game%}
    

    I would name the array 'games', since it can contain >1 game.

    So in index.php:

    if (isset($_GET["action"]) && $_GET["action"]=="liste")
    { 
      $games= $gameManager->getList();  
      echo $twig->render('game_list.html.twig',array('games'=>$games)); 
    }
    

    and in the twig:

    {% for game in games%}   
      <tr>
        <td>{{game.name}}</td><td>{{game.logo}}</td><td>{{game.cat}}</td><td>{{game.time}}</td><td>{{game.player}}</td>
      </tr>
    {% endfor %}
    

    [You were also opening more <td> tags than you closed in the twig, so I fixed that too]