php - MySQL data into template -
i'm starting php , have code works fine:
<body> <? require("conexaobd.php"); $sql = "select * tb_materia_prima"; $limite = mysql_query("$sql"); while ($sql = mysql_fetch_array($limite) ) { $data = $sql["data"]; $lote = $sql["lote"]; $material = $sql["material"]; $cor = $sql["cor"]; $quantidade = $sql["quantidade"]; $prazo = $sql["prazo"]; echo "<div id=\"testeid\"> data: $data<br> lote: $lote<br> material: $material<br> cor: $cor<br> quantidade: $quantidade kilos<br> previsão de entrega: $prazo<br><a href=\"#\">editar</a> </div>"; } ?> </body>
but, wanna put echo data html this:
<div> data: <p><?php echo $data ?></p> <br /><br /> lote: <p><?php echo $lote ?></p> <br /><br /> material: <p><?php echo $material ?></p> <br /><br /> cor: <p><?php echo $cor ?></p> <br /><br /> quantidade: <p><?php echo $quantidade ?></p> <br /><br /> prazo: <p><?php echo $prazo ?></p> </div>
it works fine too, return last mysql entry. how show entries?
get data first, , start html template
<?php require("conexaobd.php"); $sql = "select * tb_materia_prima"; $limite = mysql_query($sql); $data = array(); while ($sql = mysql_fetch_array($limite) ) { $data[] = $sql; } ?> <body> <?php foreach ($data $row): ?> <div id="testeid"> data: <?=$row['data']?><br> lote: <?=$row['lote']?><br> material: <?=$row['material']?><br> cor: <?=$row['cor']?><br> quantidade: <?=$row['quantidade']?> kilos<br> previsão de entrega: <?=$row['prazo']?><br> <a href="#">editar</a> </div> <?php endforeach ?> </body>
however, it's better data using pdo:
<?php require("conexaobd.php"); $sql = "select * tb_materia_prima"; $stm = $pdo->prepare($sql); $stm->execute(); $data = $stm->fetchall(); ?>
of course, template part remain same. that's point of templates: while changing program logic, template remain same. opposite true well
Comments
Post a Comment