Foreach loop does not work properly on select

0

Good morning,

I have an article editor where one of the fields that you have to "pick up" is the game to which it belongs. I've done it with a loop because it's about 2,000 games and I'm not going to create one for each game manually, it also changes. The code I have is this:

<select class="select" name="juego">
   <option value="Ninguno" <?php if ($juego == 'Ninguno') {echo 'selected';} ?>>Ninguno</option>
   <?php foreach ($games as $game): ?>
   <?php

   $game_title = $game['titulo'];
   $titular = trim($game['titulo']);

   ?>
   <option value="<?php echo $game_title ?>" <?php if ($juego == "<?php echo $titular ?>") {echo 'selected';} ?>><?php echo $game['titulo'] ?></option>

   <?php endforeach; ?>

</select>

As you can see, I put it in manually because there are articles that are not linked to any game and for that I use the value of "none".

$games is an array that contains all the games and where I get the names of the games. What I'm realizing is that the loop does not enter more than once, the first, because the value of $ game_title is always the same, which matches the name of the first game in the array $games .

    
asked by JetLagFox 23.02.2017 в 11:00
source

1 answer

2

I think you have the error in the code, try this code to see

<select class="select" name="juego">
   <option value="Ninguno" <?php if ($juego == 'Ninguno') {echo 'selected';} ?>>Ninguno</option>
   <?php foreach ($games as $game){ ?>
   <?php

   $game_title = $game['titulo'];
   $titular = trim($game['titulo']);

   ?>
   <option value="<?php echo $game_title ?>" <?php if ($juego == $titular) {echo 'selected';} ?>><?php echo $game['titulo'] ?></option>

   <?php } ?>

</select>
    
answered by 23.02.2017 / 12:21
source