Page 1 of 1

Problem with list() to read CSV File

Posted: Sun Oct 31, 2010 5:47 am
by Behseini
hi guys,
I am new at PHP and doing some tutorial to learn.Here I got a problem which I am getting a Notice from php when i want to add a CSV file to an HTML table using php!
Notice: Undefined offset: 2 in C:\wamp\www\Test\Q2.php on line 40
Notice: Undefined offset: 1 in C:\wamp\www\Test\Q2.php on line 40
the line 40 of my code is:

Code: Select all

 list($x,$y,$a) = explode(",",$linedata);
and as you can see I use the explode().I also tried the split() which I got another notice or error
Deprecated: Function split() is deprecated in C:\wamp\www\Test\Q2.php on line 40

could you please let me know what i am doing wrong?

Code: Select all

echo "<TABLE border=3.5>";    
while (!feof ($fp)) 
{ 
   $linedata = fgets( $fp,200 );
   list($x,$y,$a) = explode(",",$linedata); 
   echo "<TR><TD>$x</TD><TD>$y</TD><TD>$a</TD></TR>";  
}
echo "</TABLE>";  
Best regards

Re: Problem with list() to read CSV File

Posted: Sun Oct 31, 2010 7:20 am
by josh
print_r() the row each time for debugging. I bet you have rows without commas.

Re: Problem with list() to read CSV File

Posted: Sun Oct 31, 2010 1:29 pm
by Behseini
Hi Josh,
Thanks for your reply but I really did not understand what you mean?
here is the whole of my code which is writing and reading on and from a txt file.every thing goes OK and I can write and read but I got that notice, so as u see I have comma in my file

Code: Select all

<html>
<body>
<?php
$x1 =  $_REQUEST['x1'];  
$y1 =  $_REQUEST['y1'];  
$x2 =  $_REQUEST['x2'];  
$y2 =  $_REQUEST['y2'];  
$a1 = $_REQUEST['attrival1'];
$a2 = $_REQUEST['attrival2'];

$fp = fopen( "xdata.txt" , "a" ); 
if(!$fp) 
{
  echo "Couldn't open the data file. Try again later.";
  exit;
}
fwrite( $fp, "$x1,$y1,$a1\r\n");  
fwrite( $fp, "$x2,$y2,$a2\r\n");  
fclose( $fp );   

$fp = fopen( "Test.txt" , "r" ); 
if(!$fp) 
{
  echo "Couldn't open the data file. Try again later.";
  exit;
}

echo "<TABLE border=3.5>";    
while (!feof ($fp))  
{ 
   $linedata = fgets( $fp,200 );
   list($x,$y,$a) = split(",",$linedata); 
   echo "<TR><TD>$x</TD><TD>$y</TD><TD>$a</TD></TR>";   
}
echo "</TABLE>";  
fclose( $fp ); //close the file
echo "<B> Thank you very much for your input </B>";
?>
</body>
</html>