在php中为表创建while循环

在php中为表创建while循环,php,mysql,html-table,Php,Mysql,Html Table,我正在构建一个销售产品的网站,我有一个表来显示类似于“方形”表的产品,其中包含价格、图像、信息和购买按钮 我对这种类型的代码还不熟悉,我不知道如何使用它。我这里有我想要的东西。我知道它使用AJAX,但我只希望有一个代码/解释,能够使用我的MySQL数据库中的产品,使这种类型的os“squares”表,这样,当我添加更多的Prudes时,它会自动运行,并生成另一个“squares” 以下是我的PHP代码: <?php //Conection to DataBase// $li

我正在构建一个销售产品的网站,我有一个表来显示类似于“方形”表的产品,其中包含价格图像信息购买按钮

我对这种类型的代码还不熟悉,我不知道如何使用它。我这里有我想要的东西。我知道它使用AJAX,但我只希望有一个代码/解释,能够使用我的MySQL数据库中的产品,使这种类型的os“squares”表,这样,当我添加更多的Prudes时,它会自动运行,并生成另一个“squares”

以下是我的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);

?>

这是我的桌子位置:

<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表示表行。您正在为每个项目的每个细节添加新行。您希望使用基本模数运算符每四个项目创建一个新行。请看这里:

<?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>


'>
信息
购买 $i++;endwhile;?>

这是一个基本的想法

$fornos
是“fornos”(我的表名)的一个变量
$preco
是价格变量好的,所以用$fornos['preco']替换你所拥有的,并且图像被同样对待,这应该会让你接近你需要的位置。