2012-02-21 65 views
0

我是進一步學習PHP的幾個階段,但我已經到了另一個惱人的進站點。我有一個非常簡單的代碼從我的數據庫中檢索書籍項目。我將它們顯示在html表中,但是因爲它是一個循環,所以如果我使用表標題的第th標記,我會在每個數據項上獲得一個標題!PHP循環 - 使用循環創建表格併爲其添加標題

這裏是我的代碼片段:(你可以看到我已經把我的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; 

} 
?> 

回答

2

將這些echo s移出循環。此外,您不應該在<table>標記內直接使用<br />

+0

只要我發佈問題,我注意到了,會刪除br ..謝謝你的迴應,只要它允許我也會打勾你的回答!非常感謝[= – Phil 2012-02-21 21:30:36

+0

這就是它通常發生的情況! ;) – jprofitt 2012-02-21 22:09:24

0

白癡!很抱歉的傢伙....

需要把th標籤放在循環之外....簡單我知道但很容易錯過你的學習!

[=

+1

我想知道「你」和「你」之間的區別對我們中的一些人也有幫助。 – 2012-02-21 21:30:55

1

將您的表格標題代碼移到循環之外。

0

簡單地採取外循環的頭,所以呼應你開始循環,但開幕前後<table>

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; 

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

什麼是靜態的,保持靜態。 Wjat是動態的,變成PHP