如何让我的 SQL 结果出现在 PHP/HTML 中的一张表而不是两张表中

我有一个到数据库的 PDO 连接,该连接返回所搜索的项目编号的任何结果。它当前在自己的表中返回每个实例,但我希望在一个表中查看所有实例。这是我的代码。


if (count($resultssample) > 0) {

        echo "<br>";

        echo "Number in the business: ".count($resultssample)."<br>";

        echo "<br>";

        foreach ($resultssample as $r) {

            echo "<table>";

            echo "<thead>";

            echo  "<tr>";

            echo    "<th>site name</th>";

            echo    "<th>location name</th>";

            echo    "<th>creation time</th>";

            echo    "<th>creation date</th>";

            echo    "<th>damage status</th>";

            echo    "<th>colour</th>";

            echo    "<th>inventory status</th>";

            echo    "<th>notes</th>";

            echo  "</tr>";

            echo "</thead>";

            echo "<tbody>";

            echo  "<tr>";

            echo    "<td>". $r['site_name'] ."</td>";

            echo    "<td>". $r['location_name'] ."</td>";

            echo    "<td>". $r['CreationTime'] ."</td>";

            echo    "<td>". $r['CreationDate'] ."</td>";

            echo    "<td>" . $r['damage_status'] . "</td>";

            echo    "<td>" . $r['colour'] . "</td>";

            echo    "<td>" . $r['inventory_status'] . "</td>";

            echo    "<td>" . $r['Notes'] . "</td>";

            echo  "</tr>";

            echo "</tbody>";

            echo "</table>";

        }

      } else {

        echo "No results found";

      }

如何才能实现这一点,我目前无法解决这个问题。


任何帮助将不胜感激。


缥缈止盈
浏览 62回答 1
1回答

皈依舞

您所要做的就是将表头移出 foreach 循环:if (count($resultssample) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; &nbsp; &nbsp; echo "Number in the business: ".count($resultssample)."<br>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<br>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo "<table>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<thead>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>site name</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>location name</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>creation time</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>creation date</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>damage status</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>colour</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>inventory status</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<th>notes</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</thead>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<tbody>";&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($resultssample as $r) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>". $r['site_name'] ."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>". $r['location_name'] ."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>". $r['CreationTime'] ."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>". $r['CreationDate'] ."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>" . $r['damage_status'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>" . $r['colour'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>" . $r['inventory_status'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; &nbsp; "<td>" . $r['Notes'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo "</tbody>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "No results found";&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP