There is such a cycle:

$req = DB::run("SELECT `games`.*, " . Games::count() . " FROM `games` ORDER BY `id` DESC LIMIT " . $this->page . ", " . $this->message); while ($row = $req->fetch(PDO::FETCH_ASSOC)) { $arrayrow[] = $row; $arrayrowgenre[] = DB::run("SELECT * FROM `genre` WHERE `id` IN (" . $row['genre'] . ") ORDER BY `id` ASC")->fetchAll(); } 

It is necessary to transfer values ​​from the $arrayrowgenre to the smarty template

This option does not fit:

 {foreach $arrayrow as $row key=k} {foreach $arrayrowgenre as $genre} {$genre.$k.name|esc}<br/> {/foreach} {/foreach} 

it displays the wrong values.

  • And what does? - Alexey Shatrov
  • Displays this joxi.ru/1A5W53aSnzWepm And you need: 1. Race 2. Race, indie, arcade 3. Race, indie, casual game, arcade - vitagame
  • Give an example of what you have in the $arrayrow , $arrayrowgenre . ( print_r ). It’s probably not the fault of Smarti, but what you choose and how you build arrays. - teran
  • $ arrayrow - joxi.ru/ZrJpQa0S9bgEL2 $ arrayrowgenre - joxi.ru/YmENKW0F0BWpd2 - vitagame

1 answer 1

The problem in your code (if you do not take into account some kind of hell when fetching data from a database) is that instead of building and transmitting a single connected data model, you pass there several arrays with some kind of indices. Collect the data initially in a normal form, including using normal variable names, then it will be easier to work with them.

 $model = []; while ($game = $req->fetch(PDO::FETCH_ASSOC)) { $genres = DB::run("SELECT * FROM `genre` WHERE `id` IN (" . $game['genre'] . ") ORDER BY `id` ASC") ->fetchAll(); $game['genres'] = $genres; $model[] = $game; } // $smarty->assign('model', $model); // или иначе передать данные в шаблон 

in the template, the output of the table with games and genres will be as follows:

 {foreach $model as $idx => $game} <tr class="game" id="{$game.id}"> <td>{$game.name}</td> <td> <ul> {foreach $game.genres as $genre} <li>{$genre.name}</li> {/foreach} </ul> </td> </tr> {/foreach} 

PS: and read somewhere about working with the database in terms of data sampling, as well as about the database itself in terms of their implementation. It is not clear why you actually store many-to-many links serialized in a table field.