[英]Create a while loop, for tables, in php
I'm constructing a site for sell products, i have a table to show products that's like a "square" type of table with price , image , information and buy button . 我正在建立一个销售产品的网站,我有一张桌子来展示产品,例如带有价格 , 图像 , 信息和购买按钮的“方形”桌子。
I'm new to this type of code, and i couldn't understand how to do it works. 我是这种类型的代码的新手,我不明白该如何工作。 i have here an example of what i want.
我在这里有我想要的例子 。 I know that it uses AJAX, but i just want a code/explanation to be able to make that type os "squares" table with the products that i have in my MySQL DataBase , so that as i add more pruducts it automatacly and another "square".
我知道它使用AJAX,但是我只希望一个代码/解释能够使用我在MySQL数据库中拥有的产品来使该类型的os“ squares”表成为可能,这样当我添加更多产品时,它会自动生成,而另一个“广场”。
Here is my PHP code: 这是我的PHP代码:
<?php
//Conection to DataBase//
$link=mysqli_connect('localhost','root','','produtos');
if(mysqli_connect_errno())
exit("falhou a conexão ao mysql:".mysqli_connect_error());
//Codification Type//
mysqli_query($link,"set names utf8");
//Select from DataBase//
$query="Select * FROM fornos";
$result = mysqli_query($link, $query);
if (!$result)
exit("Error in query SELECT: " . mysqli_error($link));
$fornos = mysqli_fetch_assoc($result);
$imagem = $fornos['file'];
$preco = $fornos['preco'];
// Termina a ligação à Base de Dados
mysqli_close($link);
?>
Here is my table location: 这是我的桌子位置:
<table id="tabela1">
<?php
while ($fornos = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td class='products_td'> $preco </td>";
echo"</tr>";
echo"<tr>";
echo"<td class='products_td'><img class='img_product' src='images/fornos/$imagem'></td>";
echo"</tr>";
echo"<tr>";
echo"<td class='products_td'>Informações</td>";
echo"</tr>";
echo"<tr>";
echo"<td class='products_td_buy'>Comprar</td>";
echo"</tr>";
}
?>
</table>
tr means table row. tr表示表格行。 You are adding a new row for every detail of each item.
您正在为每个项目的每个细节添加新行。 You want to use a basic modulus operator to create a new row every 4th item.
您要使用基本模运算符每第4个项目创建一个新行。 See here:
看这里:
<?php
$query="Select * FROM fornos";
$result = mysqli_query($link, $query);
if (!$result)
exit("Error in query SELECT: " . mysqli_error($link));
?>
<table id="tabela1">
<tr>
<?php
$i = 0;
while ($fornos = mysqli_fetch_assoc($result)):
if($i > 0 && $i % 4 == 0):
/* this line above is what creates a new row every 4th. */
?>
</tr><tr>
<?php endif; ?>
<td class='products_td'>
<?php echo $fornos['preco']; ?><br>
<img class='img_product' src='images/fornos/<?php echo $fornos['file']; ?>'><br>
Informações<br>
Comprar
</td>
$i ++; endwhile; ?>
</tr>
</table>
This is a basic idea. 这是一个基本的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.