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

这仅显示列中的一行(最新行)..我希望它显示所有行..

4

3 回答 3

3

您实际上需要在循环中进行回显。如果将所有echo语句移到while循环的末尾,它将完全按照您的预期工作。您还需要<table></table>

于 2013-03-20T02:11:50.973 回答
0

请将您的一行 HTML 回显嵌入到 while 循环中,一切都会完成

或者

您可以在循环中构建您的 html 字符串并在其他地方回显您的 html 字符串。

于 2013-03-20T02:12:06.117 回答
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>";

}

此外,这不是很好的代码。

于 2013-03-20T02:12:11.060 回答