Page 1 of 1

File explode into multidimensional array contained in a tabl

Posted: Wed Dec 21, 2011 6:52 pm
by TheDumbNerd
I want to open this file so that it creates a multidimensional array that will be contained in a table. I don't know why my code isn't working. Please help fix it.

File i want to open: nhl.tx

Phil,Kessel,TOR,R,22,16,14
Joffrey,Lupul,TOR,R,22,11,15
Claude,Giroux,PHI,R,20,11,15
Kris,Versteeg,FLA,R,20,11,14
Thomas,Vanek,BUF,L,20,11,13
Anze,Kopitar,LAK,C,21,10,14
Daniel,Sedin,VAN,L,20,6,18
Marian,Hossa,CHI,R,20,9,14
Jason,Pominville,BUF,R,20,8,15
Ryan,Smyth,EDM,L,21,11,11
Ryan,Nugent-Hopkins,EDM,C,21,8,14
Patrick,Kane,CHI,C,21,7,15
Henrik,Sedin,VAN,C,20,7,15
Nicklas,Backstrom,WSH,C,19,6,16
James,Neal,PIT,L,21,12,9
Tyler,Seguin,BOS,C,19,11,10
Jeff,Skinner,CAR,C,22,9,12
Stephen,Weiss,FLA,C,20,8,13
Jordan,Eberle,EDM,R,21,7,14
Steven,Stamkos,TBL,C,20,11,9
Johan,Franzen,DET,R,19,10,10
Tomas,Fleischmann,FLA,L,20,9,11
Jason,Spezza,OTT,C,21,6,14
Joe,Thornton,SJS,C,18,5,15
Tomas,Plekanec,MTL,C,21,5,15
Joe,Pavelski,SJS,R,18,11,8
Jonathan,Toews,CHI,C,21,10,9
Loui,Eriksson,DAL,L,20,9,10
Patrick,Marleau,SJS,L,18,9,10
Patrik,Elias,NJD,C,19,8,11

Code:

Code: Select all

<h1>Sports Database </h1>
<?php
$file=fopen("nhl.txt","r");
if ($file==false)
{
echo "The file does not exist";
}
else
{
echo "<table border=1>";
echo "<tr><th>Last Name</th><th>First Name</th><th>Age</th><th>Hometown</th></tr>";
$j=0;
while (feof($file)==false)
{
$record= fgets($file);
$records = explode(" ", $record);
$record_number=count($records);
$record_array[$j]=$records;
$j++;
for ($i=0;i<$record_number;$i++)
  {
  echo "<td>".$fields[$i]."</td>";
  }
 echo "</tr>";
 }
echo "</table>"
}
fclose($file);
?>

Re: File explode into multidimensional array contained in a

Posted: Wed Dec 21, 2011 7:18 pm
by califdon
To begin with, you are storing your data in an array named $record_array but you never read data from that array. Then you try reading data from an array named $fields, which you don't seem to have ever created. That, of course, won't work. Then, if what you showed was a representation of the data stored in your file, you are trying to explode the lines of data based on a space character delimiter, but it appears that your file is using comma delimiters. Then, if that's what your data looks like, the headers that you created don't correspond to the data.