【问题标题】:Variable not returning变量不返回
【发布时间】: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,但当我返回它时,它是空的。

【问题讨论】:

    标签: php variables return


    【解决方案1】:

    看起来它两次使用相同的结果集并且没有在两次调用之间重置光标(即第一次$result 可以,第二次它已经在结果的末尾。改为:

    $table = show_data($result);
    if ($table != "false") {
        echo $table;
    }
    

    【讨论】:

    • 谢谢,我从来没想过。
    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多