将txt文件读取到表时出现PHP错误
我得到以下错误将txt文件读取到表时出现PHP错误,php,Php,我得到以下错误 Notice: Undefined variable: header in C:\wamp\www\test\test1.php on line 27 Notice: Undefined variable: row in C:\wamp\www\test\test1.php on line 41 Deprecated: Function split() is deprecated in C:\wamp\www\test\test1.php on line 42 Notice: U
Notice: Undefined variable: header in C:\wamp\www\test\test1.php on line 27
Notice: Undefined variable: row in C:\wamp\www\test\test1.php on line 41
Deprecated: Function split() is deprecated in C:\wamp\www\test\test1.php on line 42
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 43
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 44
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 45
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 46
Notice: Undefined variable: sortkey in C:\wamp\www\test\test1.php on line 47
Deprecated: Function split() is deprecated in C:\wamp\www\test\test1.php on line 42
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 43
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 44
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 45
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 46
Notice: Undefined variable: sortkey in C:\wamp\www\test\test1.php on line 47
Deprecated: Function split() is deprecated in C:\wamp\www\test\test1.php on line 42
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 43
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 44
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 45
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 46
Notice: Undefined variable: sortkey in C:\wamp\www\test\test1.php on line 47
Deprecated: Function split() is deprecated in C:\wamp\www\test\test1.php on line 42
Notice: Undefined offset: 3 in C:\wamp\www\test\test1.php on line 42
Notice: Undefined offset: 2 in C:\wamp\www\test\test1.php on line 42
Notice: Undefined offset: 1 in C:\wamp\www\test\test1.php on line 42
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 43
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 44
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 45
Notice: Undefined variable: sortby in C:\wamp\www\test\test1.php on line 46
Notice: Undefined variable: sortkey in C:\wamp\www\test\test1.php on line 47
使用以下代码
<span class="style2">
<style type="text/css">
<!--
body, th, td, p, small {
font-family:'Times New Roman',Times,serif;
font-size:100%;
color:#757675;
}
small {font-size:90%;}
td, th {
background-color:#FFFFFF;
border:1px solid #CCCCCC;
padding:7px 20px 7px 20px;
}
th {background-color:#a5a5a5; color:#FFFFFF;}
h1 {font-size:120%; color:#558;}
h1 .sortby {color:#855;}
-->
</style>
</span>
<?php
echo '<h1><span class="sortby">'.$header.'</span></h1>
<table cellspacing="5" summary="List of demo fields">
<tr>
<th>Date & Time Added</th>
<th>Products</th>
<th>Keys</th>
<th>Computer</th>
</tr>';
$fp = fopen('key.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
while (!feof($fp)) {
$line = fgets($fp,1024); //use 2048 if very long lines
$row++;
list ($date, $products, $keys, $computer) = split ('\|', $line);
if ($sortby == 'Date Added') $sortkey = strtolower($date);
if ($sortby == 'Products') $sortkey = strtolower($products);
if ($sortby == 'Keys') $sortkey = strtolower($keys);
if ($sortby == 'Computer') $sortkey = strtolower($computer);
$col[$row] = array($sortkey, $date, $products, $keys, $computer);
}
fclose($fp);
$arrays = count($col) - 1;
$loop = 0;
while ($loop < $arrays) {
$loop++;
echo '
<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
<td>'.$col[$loop][4].'</td>
</tr>';
}
echo '
</table>
'
?>
但是我的表显示正确,并且正在做我希望它做的事情,我不知道为什么我会出现这些错误 这些是通知和警告。这取决于您在PHP INI文件中的设置
在您的情况下,在首次使用$header之前,您没有声明它。此外,split是一个不推荐使用的函数,您应该使用explode moving forward
正如您所指出的,尽管应该考虑并培养最佳的编码实践,但它们不足以阻止脚本运行。您的代码中没有错误。Tat是通知消息。他们告诉您代码存在以下问题: split不推荐使用。您应该使用另一种方法explode,例如: 在使用变量之前,您应该初始化变量: 如果isset$var{$var=; 您可以禁用这些警告。但是,通过编辑代码来删除警告是一种更好的编码方式。拆分错误告诉我,您正在运行PHP 5.3,但代码可能是为5.2.x编写的。其他错误意味着您引用的是未定义的变量 您不必使用split,因为它已被弃用,请在php手册中查找类似的函数 您有$sortby之类的变量未声明 你有一些数组索引缺失 我会让你自己检查的,因为你甚至没有尝试检查你的错误,你只是复制和粘贴在这里,因为你的懒惰。
我告诉过你是什么原因导致了这些错误,你自己解决吧。我确实设法解决了这些错误
<?php
error_reporting(E_ALL ^ E_NOTICE);
$row = 0;
$sortby ='';
$sortkey='';
echo '<h1><span class="sortby"></span></h1>
<table cellspacing="5" summary="List of demo fields">
<tr>
<th>Products</th>
<th>Keys</th>
<th>Date & Time Added</th>
</tr>';
$fp = fopen('key_QA_N1.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
while (!feof($fp)) {
$line = fgets($fp,1024); //use 2048 if very long lines
$row++;
list ($products, $keys, $date) = explode('|', $line);
if ($sortby == 'Products') $sortkey = strtolower($products);
if ($sortby == 'Keys') $sortkey = strtolower($keys);
if ($sortby == 'Date & Time Added') $sortkey = strtolower($date);
$col[$row] = array($sortkey, $products, $keys, $date);
}
fclose($fp);
$arrays = count($col) - 1;
$loop = 0;
while ($loop < $arrays) {
$loop++;
echo '
<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
</tr>';
}
echo '
</table>
'
?>
$header似乎不包含值。是否要包含存储在$header变量中的文件?这个问题是因为您的懒惰。检查每个错误,清楚地解释问题所在。检查行和其他内容。感谢您的帮助,我了解了问题所在,并将尝试解决问题