PHP/MYSQL:将数据从 MySQL 显示到 PHP 表

我有一个关于我当前项目的问题要问。从上面的问题,我想显示数据到我的 PHP 表是这样的:


-------  ------  -------

   1       2        3

-------  ------  -------

   4       5        6      

-------  ------  -------

   7       8             

-------  ------  -------

目前,我只有 8 个数据。问题是,<td>尽管数据不存在,但仍然出现 9 号。


下面是我目前的代码。


$sql = "SELECT * FROM factory ORDER BY Fac_ID ASC";

                if($result = mysqli_query($link, $sql)){

                    if(mysqli_num_rows($result) > 0){

                        echo "<table class='table table-bordered' bgcolor='#ffffff' width='50%'>";

                        $td=0;

                            while($row = mysqli_fetch_array($result)){

                              if ($td==0){

                                echo "<tr>"; 

                              }

                              echo "<td>" . $row['Fac_ID'] . "</td>";

                              $td++;

                              if ($td==3){

                                echo "</tr>";

                                $td=0;

                              }


                            }

                            echo "</tbody>";                            

                        echo "</table>";

                        // Free result set

                        mysqli_free_result($result);

                    } else{

                        echo "<p class='lead'><em>No records were found.</em></p>";

                    }

                } else{

                    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

                }

如何确保<td>当有现有数据时会出现?


函数式编程
浏览 150回答 2
2回答

隔江千里

关于您的代码风格的两个方面的评论:1) 尽量避免使用echo静态 HTML 内容 - 禁用 PHP。2) 尝试将您的数据库逻辑与 HTML 分开 - 代码会更干净。$rows = [];if ($result = mysqli_query($link, "SELECT * FROM factory ORDER BY Fac_ID ASC")){&nbsp; &nbsp; while ($row = mysqli_fetch_array($result))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $rows[] = $row;&nbsp; &nbsp; }}$rows_chunked = array_chunk($rows, 3);?><table class="table table-bordered" bgcolor="#ffffff" width="50%"><?phpforeach ($rows_chunked as $rows){&nbsp; &nbsp; ?><tr><?php&nbsp; &nbsp; foreach ($rows as $row)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ?><td><?php echo $row['Fac_ID'] ?></td><?php&nbsp; &nbsp; }&nbsp; &nbsp; echo str_repeat('<td></td>', 3 - count($rows));&nbsp; &nbsp; ?></tr><?php}?></table>

吃鸡游戏

如果您的数据不会自然地填满表格的所有单元格,您应该质疑表格是否是显示它的正确方式。CSS 可以稍微调整一下就可以显示出来,而不必担心将结果拆分成行;运行纯 html 表示的示例:.lined {&nbsp; &nbsp; border-top-style: dashed;&nbsp; &nbsp; border-bottom-style: dashed;&nbsp; &nbsp; margin-top: -3px;&nbsp; }<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>&nbsp; &nbsp;&nbsp;<div class="row">&nbsp; <div class="col-sm-3">Page Column 1</div>&nbsp; <div class="col-sm-3">Page Column 2</div>&nbsp; <div class="col-sm-3">Page Column 3</div>&nbsp; <div class="col-sm-3">Page Column 4</div></div><div class="row">&nbsp; <div class="col-sm-3"></div>&nbsp; <div class="col-sm-3">&nbsp; &nbsp; <div class="row">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">1</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">2</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">3</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">4</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">5</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">6</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">7</div>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined">8</div>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="col-sm-3"></div>&nbsp; <div class="col-sm-3"></div></div>您需要做的就是将您的数据合并到 html 中:<?php$sql = "SELECT * FROM factory ORDER BY Fac_ID ASC";if(!$result = mysqli_query($link, $sql)){ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }$rows = array();while($row = mysqli_fetch_array($result)){&nbsp; $rows[] = $row;}mysqli_free_result($result);?><div class="row">&nbsp; <div class="col-sm-3">Page Column 1</div>&nbsp; <div class="col-sm-3">Page Column 2</div>&nbsp; <div class="col-sm-3">Page Column 3</div>&nbsp; <div class="col-sm-3">Page Column 4</div></div><div class="row">&nbsp; <div class="col-sm-3"></div>&nbsp; <div class="col-sm-3">&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; <?php foreach($rows as $r): ?>&nbsp; &nbsp; &nbsp; <div class="col-sm-4 lined"><?= $r['Fac_ID'] ?></div>&nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; <?php if(sizeof($rows) == 0): ?>&nbsp; &nbsp; &nbsp; <p class='lead'><em>No records were found.</em></p>&nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="col-sm-3"></div>&nbsp; <div class="col-sm-3"></div></div>
打开App,查看更多内容
随时随地看视频慕课网APP