Using select the same select more than once

Asked

Viewed 46 times

1

I need to wear the same select other times, but I’m not getting it.

It shows the result in the first while, but for the rest not.

On the same screen I will repeat by 4x, because I am referring to the tires: Right Front, Left Front, Right Rear, Left Rear.

MYSQL:

$rsMarca = $mysqli->query("SELECT * FROM pneu_marca");
$row_rsMarca = $rsMarca->fetch_assoc();

First While

<select name="marca_de">
  <?php do { ?>
  <option value="<?php echo $row_rsMarca['ID_Marca']; ?>">
    <?php echo $row_rsMarca['marca']; ?>
  </option>
  <?php } while ($row_rsMarca = $rsMarca->fetch_assoc()); ?>
</select>

Second and other while

<select name="marca_dd">
  <?php do { ?>
  <option value="<?php echo $row_rsMarca['ID_Marca']; ?>">
    <?php echo $row_rsMarca['marca']; ?>
  </option>
  <?php } while ($row_rsMarca = $rsMarca->fetch_assoc()); ?>
</select>

1 answer

1


Well, basically you are trying to run the loop "while" the execution of the query happens, in case it happens only once, and as you used the structure of the do while, it performs once, checks the while and stop condition. I would recommend you to use the foreach, where it loops for each line of the result.

In the first

<select name="marca_de">
  <?php foreach($row_rsMarca as $resultado) { ?>
  <option value="<?php echo $resultado['ID_Marca']; ?>">
    <?php echo $resultado['marca']; ?>
  </option>
  <?php } ?>
</select>

In the second

<select name="marca_dd">
  <?php foreach($row_rsMarca as $resultado){ ?>
  <option value="<?php echo $resultado['ID_Marca']; ?>">
    <?php echo $resultado['marca']; ?>
  </option>
  <?php } ?>
</select>

This will cause "for each" line of your query, it creates an option containing the line’s relative information.

  • 1

    Having a problem, see how it is displayed (https://prnt.sc/npgvjn), in mysql this way (https://prnt.sc/npgvod)

  • 1

    Find the error...rsrs. Instead of <?php foreach($row_rsMarca as $resultado) { ?> era <?php foreach($rsMarca as $resultado) { ?>.

  • If in your query you put one return before $row_rsMarca, I believe it would work without trading in foreach.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.