读取txt文件到表时出现PHP错误

时间:2010-11-11 16:31:53

标签: 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: 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>
 '
?>

但是我的桌子显示正确并正在做我想要它做的事我不知道为什么我得到这些错误。

5 个答案:

答案 0 :(得分:3)

您的代码中没有错误。 Tat是通知消息。他们告诉您代码存在以下问题:

  1. split()已弃用(您应该使用其他方法explode,例如:http://php.net/manual/en/function.split.php
  2. 您可以在使用变量之前初始化变量: if(!isset($var)){$var="";
  3. 您可以禁用这些警告。但是通过编辑代码来删除警告将是一种更好的编码风格。

答案 1 :(得分:0)

这些是通知警告。这取决于PHP INI文件中的error_reporting设置。

在您的情况下,您在首次使用之前尚未声明$header。此外,split()是不推荐使用的功能,您应该使用explode()前进。

虽然应该考虑这些并培养出最好的编码实践,但正如您所指出的那样,它们并不足以阻止您的脚本运行。

答案 2 :(得分:0)

split错误告诉我你正在运行PHP 5.3,但代码是为(可能)5.2.x编写的。其他错误意味着您指的是未定义的变量。

答案 3 :(得分:0)

  • 您不必使用split(),因为它已弃用,请在php手册中查找类似的功能。

  • 您有$sortby未声明的变量

  • 您有一些数组索引缺失

我会让你自己检查因为你甚至没有试图检查你的错误,你只是因为你的懒惰而复制并粘贴在这里。 我告诉你导致错误的是什么,只需自己解决。

答案 4 :(得分:0)

我确实设法解决了错误

<?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>
 '
?>