2012-03-17 99 views
0

我已經將下面的代碼放在一起,該代碼使用下拉菜單進行選擇時創建表格。PHP表格格式

echo "<table>"; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 
echo "<tr>"; 
echo "<td>".$rows['findname']."</td>"; 
echo "<td>".$rows['finddescription']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

我得到的問題是,對於每個返回的記錄'頭'重複。實時頁面可以找到here。我只是想知道是否有人可以看看這個,並告訴我我哪裏出了問題。

道歉的真正簡單的問題,但我一直在看這一段時間,我只是無法找到答案。我認爲這隻需要一雙新的眼睛來看待它。

+0

非常感謝您的幫助。親切的問候。 – IRHM 2012-03-17 14:05:08

回答

3

後,這應該工作:

echo "<table>"; 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 
    echo "<tr>"; 
    echo "<td>".$rows['findname']."</td>"; 
    echo "<td>".$rows['finddescription']."</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 
3

您的標題正在重複,因爲您正在將它們寫入循環中,對於查詢返回的每一行。你只需要移動外循環的頭所以他們只能寫一次,由查詢印字開始返回行之前:

echo "<table>"; 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 

$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 
    echo "<tr>"; 
    echo "<td>".$rows['findname']."</td>"; 
    echo "<td>".$rows['finddescription']."</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
4

試試這個,你只需要得到頭了while循環

echo "<table>"; 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 

$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 

echo "<tr>"; 
echo "<td>".$rows['findname']."</td>"; 
echo "<td>".$rows['finddescription']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
3

頭被重複becasue他們在while循環,它應該工作以及

echo "<table>"; 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 

echo "<tr>"; 
echo "<td>".$rows['findname']."</td>"; 
echo "<td>".$rows['finddescription']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
4

答案是顯而易見的,你是在重複循環中的標頭的輸出。移動

while($rows=mysql_fetch_array($result)){ 

第一

echo "</tr>"; 
2

更改爲:

echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 
echo "<tr>"; 
while($rows=mysql_fetch_array($result)){ 
echo "<td>".$rows['findname']."</td>"; 
echo "<td>".$rows['finddescription']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
4

你需要把報頭中的同時,外循環:

echo "<table>"; 
echo "<tr>"; 
echo "<th>Find Name:</th>"; 
echo "<th>Find Description:</th>"; 
echo "</tr>"; 

$result = mysql_query($query); 

while ($rows = mysql_fetch_array($result)) { 
    echo "<tr>"; 
    echo "<td>" . $rows['findname'] . "</td>"; 
    echo "<td>" . $rows['finddescription'] . "</td>"; 
    echo "</tr>"; 
} 

echo "</table>";