【发布时间】:2018-02-21 03:35:27
【问题描述】:
这对我来说完全是个谜。我有一个循环数据查询的函数。我正在尝试将数据作为表格的行收集到一个变量中。但是,该变量不会返回。我可以回显它,但不能返回它。变量是 while 循环中的 $table_rows。
function show_data($result) {
$total_time = 0;
$table_rows = '<table style="width: 1200px;"><tr><th width="30%">Date</th><th width="10%">Hour</th><th width="10%">Business</th><th width="40%">Task</th><th width="10%">Time</th></tr>';
if ( mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$total_time += $row["Total_Time"];
$hours = floor($row["Total_Time"] / 3600);
$mins = floor($row["Total_Time"] / 60 % 60);
$table_rows .= '<tr><td>'.$row['Date'].'</td><td>'.$row["Hour_Done"].'</td><td>'.$row["Business"].'</td><td>' . $row["Task"]. '</td><td>' . $hours.':'.$mins.'</td></tr>';
}
$hours = floor($total_time / 3600);
$mins = floor($total_time / 60 % 60);
return $table_rows . '<tr><td colspan="4" align="right"><b>Total Time</b></td><td><b>'.$hours.':'.$mins.'</b></td></table>';
}
else {
return "false";
}
}
if( show_data($result) != "false") {
echo show_data($result);
}
else { echo "<h1>No data to display</h1>"; }
如果我在它显示的while循环之外回显$table_rows,但当我返回它时,它是空的。
【问题讨论】: