php - Variable not returning -
this complete mystery me. have function loops through data query. trying gather data variable rows table. however, variable not return. can echo it, not return it. variable $table_rows within while loop.
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 display</h1>"; } if echo $table_rows right outside while loop displays, when return it, empty.
looks it's using same result set twice , not resetting cursor in between calls (i.e. first time $result ok, second time it's @ end of results. instead:
$table = show_data($result); if ($table != "false") { echo $table; }
Comments
Post a Comment