Home Variable not returning
Reply: 1

Variable not returning

Husbandman Published in 2017-09-12 15:29:00Z

This is a complete mystery to me. I have a function that loops through a data query. I am trying to gather the data into a variable as rows for a table. However, that variable will not return. I can echo it, but not return it. The variable is $table_rows within the 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 to display</h1>"; }

If I echo the $table_rows right outside the while loop it displays, but when I return it, it is empty.

Daren Chandisingh
Daren Chandisingh Reply to 2017-09-12 15:33:56Z

Looks like it's using the same result set twice and not resetting the cursor in between calls (i.e. the first time $result is OK, the second time it's already at the end of the results. Do this instead:

$table = show_data($result);
if ($table != "false") {
    echo $table;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.457346 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO