2013-03-20 27 views
0

我需要回顯SQL列中的所有行。回顯列中的所有行

這是我的PHP

$query2="SELECT * FROM field_activity WHERE field_id='$fetch'"; 
$result2 = mysql_query($query2); 
while($row = mysql_fetch_array($result2)){ 
    $activity = $row['activity']; 
    $cr_date = date_create($row['date']); 
    $for_date = date_format($cr_date, 'F j, Y'); 
    $amount = $row['amount']; 
    $acres_complete = $row['acres_complete']; 
    $duration = $row['duration']; 
    $status = $row['status']; 
} 

這裏是我的HTML輸出..

<?php 
{ 
    echo "<tr>"; 
    echo "<td width='16%'><strong>Date</strong></td>"; 
    echo "<td width='16%'>$for_date</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Activity</strong></td>"; 
    echo "<td width='16%'>$activity</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Amount</strong></td>"; 
    echo "<td width='16%'>$amount</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Acres Complete</strong></td>"; 
    echo "<td width='16%'>$acres_complete</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Duration</strong></td>"; 
    echo "<td width='16%'>$duration</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Status</strong></td>"; 
    echo "<td width='16%'>$status</td>"; 
    echo "</tr>"; 
} 
?> 

這只是顯示從列行(最新行)之一。我希望它顯示所有行。

+1

echo在while循環中,你每次只用$ row數組覆蓋你的字符串,所以只能看到最後一行 – 2013-03-20 02:11:56

回答

3

您需要實際做循環中的回顯。如果將所有echo語句移動到while循環的末尾,它將按照您的預期正常工作。您還需要<table></table>

0

請嵌入您的一行HTML回聲進入while循環,一切都會做

OR

你可以建立HTML字符串中的環和其他地方的呼應你的HTML字符串。

0

您正在覆蓋您的while循環中的變量,因此只有最後一個值將存儲在您的變量中。

試試這個:

$query2="SELECT * FROM field_activity WHERE field_id='$fetch'"; 
$result2 = mysql_query($query2); 
while($row = mysql_fetch_array($result2)){ 

    $activity = $row['activity']; 
    $cr_date = date_create($row['date']); 
    $for_date = date_format($cr_date, 'F j, Y'); 
    $amount = $row['amount']; 
    $acres_complete = $row['acres_complete']; 
    $duration = $row['duration']; 
    $status = $row['status']; 

    echo "<tr>"; 
    echo "<td width='16%'><strong>Date</strong></td>"; 
    echo "<td width='16%'>$for_date</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Activity</strong></td>"; 
    echo "<td width='16%'>$activity</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Amount</strong></td>"; 
    echo "<td width='16%'>$amount</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Acres Complete</strong></td>"; 
    echo "<td width='16%'>$acres_complete</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Duration</strong></td>"; 
    echo "<td width='16%'>$duration</td>"; 
    echo "</tr><tr>"; 
    echo "<td width='16%'><strong>Status</strong></td>"; 
    echo "<td width='16%'>$status</td>"; 
    echo "</tr>"; 

} 

此外,這不是很好的代碼。

+1

你可以跳過賦值並只是echo $ row ... – 2013-03-20 02:22:49

+0

正確,只是不想要混淆乍得。 – 2013-03-21 07:59:00