PHP 循环 - 使用循环创建表并为其提供标题

发布于 2025-01-07 23:44:10 字数 1421 浏览 0 评论 0原文

我在学习 PHP 的过程中又前进了几个阶段,但我又遇到了另一个令人讨厌的停顿点。我有一段非常简单的代码,可以从数据库中检索图书项目。我将它们显示在 html 表中,但是因为它是一个循环,如果我使用 th 标签作为表头,我会在每个数据项上方得到一个标头!

这是我的代码摘录:(如您所见,我已将我的 th 标签作为注释,因为这不起作用)

<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
    $row = mysql_fetch_array($results);
    extract($row);

    echo "<tr>";

    //echo "<tr>";
    //echo "<th>";
    //echo "Book Title";
    //echo "</th>";
    //echo "<th>";
    //echo "Book Author";
    //echo "</th>";
    //echo "<th>";
    //echo "Book Publisher";
    //echo "</th>";
    //echo "<th>";
    //echo "Book ISBN";
    //echo "</th>";
    //echo "</tr>";

    echo "<td>";
    echo "<a href='addtolist.php?     bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";

    echo "</td>";

    echo "<td>";
    echo $bookname;
    echo "</td>";

    echo "<td>";
    echo $bookauthor;
    echo "</td>";

    echo "<td>";
    echo $bookpub;
    echo "</td>";

    echo "<td>";
    echo $bookisbn;
    echo "</td>";

    echo "<td>";
    echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
        echo "</td>";

    echo "</tr>";

    $count = $count + 1;

}
?>

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!

Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)

<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
    $row = mysql_fetch_array($results);
    extract($row);

    echo "<tr>";

    //echo "<tr>";
    //echo "<th>";
    //echo "Book Title";
    //echo "</th>";
    //echo "<th>";
    //echo "Book Author";
    //echo "</th>";
    //echo "<th>";
    //echo "Book Publisher";
    //echo "</th>";
    //echo "<th>";
    //echo "Book ISBN";
    //echo "</th>";
    //echo "</tr>";

    echo "<td>";
    echo "<a href='addtolist.php?     bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";

    echo "</td>";

    echo "<td>";
    echo $bookname;
    echo "</td>";

    echo "<td>";
    echo $bookauthor;
    echo "</td>";

    echo "<td>";
    echo $bookpub;
    echo "</td>";

    echo "<td>";
    echo $bookisbn;
    echo "</td>";

    echo "<td>";
    echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
        echo "</td>";

    echo "</tr>";

    $count = $count + 1;

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奶茶白久 2025-01-14 23:44:10

将那些 echo 移出循环。另外,您不应将
直接放在 标记内。

Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.

一笔一画续写前缘 2025-01-14 23:44:10

将表头代码移到循环之外。

Move your table header code outside of the loop.

煮酒 2025-01-14 23:44:10

笨蛋!抱歉,伙计们……

需要将 th 标签放在循环之外……我知道很简单,但在学习时很容易错过!

[=

IDIOT! Sorry guys....

Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!

[=

永言不败 2025-01-14 23:44:10

只需将标题放在循环之外,因此在开始循环之前但在开始之后进行回显

Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>

我一向站在原地 2025-01-14 23:44:10

您必须将标题移动到循环上方:

<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>

    <?php
    $count = 0;
    while ($count < $numrow)
    {
        $row = mysql_fetch_array($results);
        extract($row);

    echo "<tr>"

        echo "<td>";
        echo "<a href='addtolist.php?     bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";

        echo "</td>";

        echo "<td>";
        echo $bookname;
        echo "</td>";

        echo "<td>";
        echo $bookauthor;
        echo "</td>";

        echo "<td>";
        echo $bookpub;
        echo "</td>";

        echo "<td>";
        echo $bookisbn;
        echo "</td>";

        echo "<td>";
        echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
            echo "</td>";

        echo "</tr>";

        $count = $count + 1;

    }
    ?>

You have to move the headers above the loop:

<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>

    <?php
    $count = 0;
    while ($count < $numrow)
    {
        $row = mysql_fetch_array($results);
        extract($row);

    echo "<tr>"

        echo "<td>";
        echo "<a href='addtolist.php?     bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";

        echo "</td>";

        echo "<td>";
        echo $bookname;
        echo "</td>";

        echo "<td>";
        echo $bookauthor;
        echo "</td>";

        echo "<td>";
        echo $bookpub;
        echo "</td>";

        echo "<td>";
        echo $bookisbn;
        echo "</td>";

        echo "<td>";
        echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
            echo "</td>";

        echo "</tr>";

        $count = $count + 1;

    }
    ?>
述情 2025-01-14 23:44:10
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
    $row = mysql_fetch_array($results);
    extract($row);

    echo "<tr>";


    echo "<td>";
...

静态的东西就保持静态。
Wjat 是动态的,变成了 PHP

<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
    $row = mysql_fetch_array($results);
    extract($row);

    echo "<tr>";


    echo "<td>";
...

What is static, stays static.
Wjat is dynamic, becomes PHP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文