首页 > 解决方案 > 数据未显示在表 PHP 中

问题描述

我对此完全陌生,并且正在上教授绝对没有帮助的课程。我正在尝试将文件中的数据读取到表中,但由于某种原因数据没有显示出来。我已经尝试了几种不同的方法,但老实说,我有点迷路了。非常感谢任何建议

代码是:

$filename = 'data/'.'booklist.txt';
$find_title = $_POST['title'];
$ascending = $_POST['ascending'];
$descending = $_POST['descending'];
list($total_rows, $theTable) = displayTable($filename);
if ($total_rows != 'No File')
{
print $theTable;   
}
else
{
    print "No file found";
}
function displayTable($filename)
{ 
    $myTable =  "\n<table border='1'>";
    $myTable .= "<tr>";
    $myTable .= "   <th>Title</th>";
    $myTable .= "   <th>Type</th>";
    $myTable .= "   <th>Publication Date</th>";
    $myTable .= "   <th>ISBN</th>";
    $myTable .= "</tr>\n\n";
    $line_ctr = 0;
    $fp = fopen($filename, 'r');   //opens the file for reading
    if ($fp)
    {
        while(true)
        {
            $line = fgets($fp);
            if (feof($fp))
            {
                break;
            }
            $line_ctr++;
            $line_ctr_remainder = $line_ctr % 2;
            if ($line_ctr_remainder == 0)
            {
                $style = "style='background-color: #FFFFCC;'";
            } else {
                $style = "style='background-color: white;'";
            }
            list($title, $type, $publicationdate, $isbn) = explode('*', $line);
            $myTable .= "<tr $style>";
            $mytable .= "<td>".$title."</td>";
            $mytable .= "<td>".$type."</td>";
            $mytable .= "<td>".$publicationdate."</td>";
            $mytable .= "<td>".$isbn."</td>";
            $mytable .= "</tr>\n";   
        }
        fclose($fp );
        $myTable .= "</table>";
        $rtn = array($line_ctr, $myTable);
    } else {
        $rtn = array("No File", "dummy");
    }
    return $rtn;
    print $mytable;   //This prints the table rows
}
?>
</div>
</body>
</html>

谢谢!

标签: phphtml

解决方案


问题在于

print $theTable;

哪个值为“NULL”,因此它不起作用。

PHP 为您的函数返回错误:

Undefined index: title in <b>[...][...]</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined index: ascending in <b>[...][...]</b> on line <b>5</b><br />
<br />
<b>Notice</b>:  Undefined index: descending in <b>[...][...]</b> on line <b>6</b><br />

函数的执行也在返回时停止,所以:

   return $rtn;
    print $mytable;   //This prints the table rows

Print 永远不会执行,因为您使用return退出了函数。


推荐阅读