Sandy
Sandy

Reputation: 312

PHP array displaying last row values from table

What i want to do is pull out all of the rows in the below table.

$timeapp_id (this is the users id stored from session)

If the users id is 8 then the returned values of $withComma should be "5 7 34"

What the problem is the line echo $withComma; which is inside the loop outputs the above value, but when echo $withComma; is outside the loop only the last value is returned so when i pass this through $results4 only the last row (which is 34) is displayed.

How do i go about fixing this so that echo $withComma; outside of the loop displays all the results instead of the last row?

$data2b = "select * from user_info where timesheet_approver_1 = $timeapp_id";

$result2b = mysql_query($data2b);

while ($row2b = mysql_fetch_assoc($result2b)) {

    $timesheet_approver_1 = $row2b['timesheet_approver_1'];
    $timesheet_approver_2 = $row2b['timesheet_approver_2'];
    $user_id2 = $row2b['user_id'];

    $array = array(" ",$user_id2);

    $withComma = implode(" ",$array);

    echo $withComma;

}

echo "<br><br>($withComma)";

$results4 = mysql_query("select * from time_data where user_id in ( $withComma ) and status = 'Submitted' order by data_date desc limit $time_entry_display_rows;");

Upvotes: 0

Views: 219

Answers (1)

Barmar
Barmar

Reputation: 782148

You're overwriting the $array and withComma variables each time through the loop.

You need to accumulate your values in an array inside the loop, and then implode the whole array when the loop is done.

$array = array();

while ($row2b = mysql_fetch_assoc($result2b)) {

    $timesheet_approver_1 = $row2b['timesheet_approver_1'];
    $timesheet_approver_2 = $row2b['timesheet_approver_2'];

    $array[] = $row2b['user_id'];
}

$withComma = implode(", ", $array);

Upvotes: 1

Related Questions