【问题标题】:How to create a automatic <table> if there is data in the database, php如果数据库中有数据,php如何自动创建<table>
【发布时间】:2017-03-20 12:48:08
【问题描述】:

我在 MySql 中创建了一个包含 8 个表和产品的数据库。我想自动,它在表中创建一个 tr 和 td,因为我在数据库表中有越来越多的数据。

*我已经尝试查找有关它的信息,但我无法理解它是如何工作的。

这是我目前所拥有的:

<?php
        //Connect 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("Erro na query SELECT: " . mysqli_error($link));
        $fornos = mysqli_fetch_assoc($result);
        $imagem = $fornos['image_link'];
        $preco = $fornos['preco'];
        mysqli_free_result($result);

        //Finish the Conection//
        mysqli_close($link);
?>


<?php
                        echo '<table id="tabela1">';
                    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=' echo '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>";
                    }
                        echo '</table>';

                    ?>

【问题讨论】:

  • mysqli_fetch_assoc($id) ?可能是 mysqli_fetch_assoc($result) 并检查 echo statments 中的引用
  • 共享一个表结构和你想要的输出...
  • @RakeshSharma 是的,我已经将mysqli_fetch_assoc($id) 更改为mysqli_fetch_assoc($result) 谢谢

标签: php mysql html-table


【解决方案1】:

将代码包装在条件中以检查是否有条目:

<?php

if(mysqli_num_rows($sql) > 0)
{
    echo '<table>';

    // YOUR CODE TO PRINT THE TABLE ROWS

    echo '</table>';
}

?>

【讨论】:

  • 是的,它的结果,但我尝试放置另一个“产品”(在数据库表中),它只显示 1 而不是显示其中的 2(因为我以后会和更多产品)
  • 我不明白你的意思...请澄清。
  • 我在 mySQL 中有一个包含产品的表格,我希望它们显示在我的 html/php 页面中的表格内。但是想象一下,如果我在 mySQL 表中有 2 或 3 个产品并且我想添加另一个我想自动创建表中的另一行。 (该网站将出售产品)
【解决方案2】:

试试这个代码:

<?php
......
......
$sytax = mysql_query("SELECT * FROM `TABLE`"); // anything you want
echo "<table><tr>";
while($make_this = mysql_fetch_array($sytax)){
?>
   <td><?php echo $make_this['Field_in_your_table']; ?></td>
<?php
}
echo "</tr></table>";
?>

【讨论】:

    【解决方案3】:

    试试这个....

    &lt;table&gt;

    &lt;?php

       while ($fornos=mysqli_fetch_assoc($id)){
    

    ?>

           <tr>
                  <td class='products_td'><?php echo $preco ?></td>
                  // similarly other data
    
        <?php
            }
           ?>
        </table>      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 2011-10-21
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      相关资源
      最近更新 更多