Generate button in DIV?

Asked

Viewed 37 times

1

I have a DIV call ( meusBotoes ) and I would like every loop of while generates a button inside the DIV with the value of the variable $nome and with the onclick calling function display().

Man while

<?php
$host = "";
$bd = "";
$usr = "";
$psw = "";

$conn = new mysqli($host, $usr, $psw, $bd);
if ($conn->connect_error) 
{
die("A conexão falhou, consulte o suporte: " . $conn->connect_error);
} 
$sql = "SELECT id, nome, tipo, imagem FROM produtos";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{

while($row = $result->fetch_assoc())
{
$id = $row['id'];
$nome = $row['nome'];
$tipo = $row['tipo'];
$imagem = $row['imagem']; 
}

}
?>

There is an easy way to do this in PHP same, without Javascript?

  • What have you tried? It talks about writing inside a DIV, but it doesn’t show you in the code where it is. If your PHP returns an HTML, it’s just a matter of doing an echo '<input type="button" ...' with each iteration, or incrementing a variable with these inputs to give an echo in DIV later in the code...

1 answer

1


That’s basically it, playing the while within the div:

connect_query.php

<?php
$host = "";
$bd = "";
$usr = "";
$psw = "";

$conn = new mysqli($host, $usr, $psw, $bd);
if($conn->connect_error) 
{    die("A conexão falhou, consulte o suporte: " . $conn->connect_error);
} 
$sql = "SELECT id, nome, tipo, imagem FROM produtos";
$result = $conn->query($sql);  
?>

pag_btn.php

<?php include 'connect_query.php';?>
<div id="meusBotoes ">
    <?php     
    while($row = $result->fetch_assoc())
    {    $id = $row['id'];
         $nome = $row['nome'];
         $tipo = $row['tipo'];
         $imagem = $row['imagem']; 
         echo"<button id=".$nome." onclick='exibirAlerta()'>".$nome."</button>";
    }
    ?>
</div>
  • Ball show. That’s just what I wanted. Thank you very much.

Browser other questions tagged

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