开发者

PHP error when reading for txt file to table

开发者 https://www.devze.com 2023-01-24 09:01 出处:网络
I am getting the following errors Notice: Undefined variable: header in C:\\wamp\\www\\test\\test1.php on line 27

I am getting the following errors

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: Undefine开发者_开发百科d 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

With the following code

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

BUT my table is showing correctly and is doing what i want it to do I dont know why iam getting these errors.


There are no errors in your Code. Tat are notice-messages. They are telling you the following problems with your code:

  1. split() is deprecated (You should use another mothod explode for example: http://php.net/manual/en/function.split.php
  2. you sould initialise variables before using them: if(!isset($var)){$var="";

You may disables these warning. but it would be a better coding-style to remove the warnings, by editing the code.


These are Notices and Warnings. It depends upon your error_reporting setting in your PHP INI file.

In your case, you have not declared $header before it's first use. Also, split() is a deprecated function, you should use explode() moving forward.

Although these should be considered and breed best coding practices, as you noted, they are not serious enough to stop your script from running.


The split errors tell me that you're running PHP 5.3 but with code written for (probably) 5.2.x. The other errors imply you are referring to undefined variables.


  • You don't have to use split() because it is deprecated, look for a similar function in the php manual.

  • You have this variables like $sortby not declared

  • You have some arrays index missings

I'll let you check by yourself becouse you didn't even tried to check your errors, you just copied and pasted over here because of your laziness. I told you what are the things that causes the errors, just fix it by yourself.


I did manage to fix the errors

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号