显示表中最后一行值的PHP数组


PHP array displaying last row values from table

我想做的是拉出下表中的所有行。

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

如果用户id为8,则$withComma的返回值应为"5 7 34"

问题是循环内的线echo $withComma;输出上述值,但当echo $withComma;在循环外时,仅返回最后一个值,因此当我通过$results4时,仅显示最后一行(即34)。

我该如何解决这个问题,使循环外的echo $withComma;显示所有结果,而不是最后一行?

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

在循环中每次都覆盖$arraywithComma变量。

您需要在循环内的一个数组中累积值,然后在循环完成时内爆整个数组。

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