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

时间:2012-02-21 21:26:17

标签: php html loops while-loop html-table

我在学习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;

}
?>

6 个答案:

答案 0 :(得分:2)

将那些echo移出你的循环。此外,您不应在<br />代码中直接使用<table>

答案 1 :(得分:1)

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

答案 2 :(得分:0)

IDIOT!对不起伙计....

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

[=

答案 3 :(得分:0)

只需将标题放在循环外部,所以在开始循环之前回显,但开始之后 {/ 1}}

答案 4 :(得分:0)

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

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

    }
    ?>

答案 5 :(得分:0)

<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>";
...

什么是静态,保持静止。 什么是动态的,变成PHP