How an array comes from the database in an input

Asked

Viewed 76 times

2

<input type="text" class="form-control" name="lanches" id="lanches" value="<?php echo $resultado['lanches']; ?>" >

I have that above input where it receives that array that is inside the database inserir a descrição da imagem aqui

How do I do...

3 answers

2


You can use the foreach, in your case I understood that you would be showing the value in your correct input?

The structure of the foreach is

foreach($array as $linha){

}

It will perform an iteration (just like the structure for), for each row of your Array, and in each iteration you will perform the manipulation with the $line variable.

For example, if you had this array: $array = ["pos1", "pos2", "pos3"], the foreach would run 3 times, and in each iteration the value currently being accessed would be used by the variable after the "as" in the case of the example above the variable $line.

In case your code would look something like this:

foreach($array as $valor){
    <input type="text" class="form-control" name="lanches" id="lanches" value=" <?php echo $valor; ?>" >
}
  • The code will not work. html is loose in the middle of php. One option is to put in a echo or close php (?>) before html and reopen after

1

You can use a foreach. It would look something like this:

    foreach($resultado['lanche'] as $id){ 
echo "<input type='text' class='form-control' name='lanches[$id]' id='lanches_$id' value='$id'>";
}

0

  • As I would with foreach, I have some example of how ...

Browser other questions tagged

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