php - 数据未显示在表 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>
谢谢!
解决方案
问题在于
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退出了函数。
推荐阅读
- python-3.x - 为 maskRcnn 导入 Tensorboard(Matterport - Mask RCNN)
- javascript - 无法将创建帐户数据从 React 发送到 Flask 端点
- r - 折叠近乎重复的内容并将差异添加为 R 中的新列?
- r - 如何使用 R ggplot 覆盖两个地图
- scala - Java 保留字作为 Scala 中的包名
- nfc - NTAG 424 NFC 芯片的 Python CRC 计算
- react-native - 如何将通过 URL 检索到的图像的宽度调整为 React Native 中的 View 容器?
- jquery - jQuery中的变量问题(未定义)即使我检查它是否存在
- java - 删除片段时如何动态更新 ViewPager
- java - Java:使用孩子的成员变量调用父母的方法