简体   繁体   中英

Echo all rows from column

I need to echo all rows from SQL column.

Here is my 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'];
}

Here is my HTML output..

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

This only displays one of the rows (the latest row) from the column.. I want it to display all rows..

You need to actually do the echoing in the loop. If you move all of the echo statements to the end of the while loop, it will work exactly as you expect. You also need <table> and </table> .

Please embed your one row HTML echos into the while loop and everything will be done

OR

You can build your html string in the loop and echo your html string elsewhere.

You are overriding your variables in your while loop, so only the last value will be stored in your variables.

Try this:

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

}

Additionally, this is not very good code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM