The files I am recieving are from a stat service. I dont have the option of starting with the DB I have to parse the flat file into a db.
Here is a snippet of the file I am parsing.
Code: Select all
; 1999 Week 17 99WK17QO.NFL QSS Offensive Player Stats
; |-------- Passing --------|-- Rushing --|- Receiving -|- Kicking FG Distances -|
; 2- 2- 2- 01 30 40
;Name ID # Tm/ID Pos I S P T Cmp Att Yds In TD Pt Sck/Yds Car Yds TD Pt Rec Yds TD Pt EPM EPA FGM FGA -29 -39 -49 50+ FL TDs by distance
Aguiar, Louie 3024 GB 9021 P 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Aikman, Troy 2 DAL 9014 QB 1 1 0 23 32 288 0 2 0 0 0 3 -2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 T004T090
Akers, David 1594 PHI 9016 PK 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 G046
Alexander, Derrick 1020 KC 9010 WR 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 18 0 0 0 0 0 0 0 0 0 0 0
And the code I am using to parse it into the DB
Code: Select all
<?PHP
//read.php
// Database settings
$dbhost = "localhost"; // Database host
$dbname = "nfl"; // Database name
$dbusername = "un"; // Database user name
$dbuserpw = "pw"; // Database password
$dbtable = "off_players"; // table of servers
$link_id = mysql_connect($dbhost,$dbusername,$dbuserpw)
or die("Could not connect to MySQL.");
//echo("Connection Successfull.<br>");
$selected = mysql_select_db($dbname,$link_id)
or die("Could not select database");
$offPlayers = "./nfl.txt" ;
if (!($fp = fopen($offPlayers, "r"))) die ("Cannot open file");
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
do
{
$fullname = fread($fp,25);
$fullname = trim($fullname);
// spilt each line into individual fields
$names = explode(",", $fullname);
$offset = fgetc($fp);
$id = fread($fp, 4);
$id = trim($id);
$offset = fgetc($fp);
$team = fread($fp, 3);
$team = trim($team);
$offset = fgetc($fp);
$teamid = fread($fp, 4);
$teamid = trim($teamid);
$offset = fgetc($fp);
$pos = fread($fp, 2);
$pos = trim($pos);
$offset = fgetc($fp);
$inj_stat = fread($fp, 1);
$inj_stat = trim($inj_stat);
$offset = fgetc($fp);
$start = fread($fp, 1);
$start = trim($start);
$offset = fgetc($fp);
$played = fread($fp, 1);
$played = trim($played);
$offset = fgetc($fp);
$inact = fread($fp, 1);
$inact = trim($inact);
$offset = fgetc($fp);
$pcmp = fread($fp, 3);
$pcmp = trim($pcmp);
$offset = fgetc($fp);
$patt = fread($fp, 3);
$patt = trim($patt);
$offset = fgetc($fp);
$pyds = fread($fp, 3);
$pyds = trim($pyds);
$offset = fgetc($fp);
$pint = fread($fp, 2);
$pint = trim($pint);
$offset = fgetc($fp);
$ptds = fread($fp, 2);
$ptds = trim($ptds);
$offset = fgetc($fp);
$p2pt = fread($fp, 2);
$p2pt = trim($p2pt);
$offset = fgetc($fp);
$psck = fread($fp, 3);
$psck = trim($psck);
$offset = fgetc($fp);
$psckyds = fread($fp, 3);
$psckyds = trim($psckyds);
$offset = fgetc($fp);
$car = fread($fp, 3);
$car = trim($car);
$offset = fgetc($fp);
$ryds = fread($fp, 3);
$ryds = trim($ryds);
$offset = fgetc($fp);
$rtds = fread($fp, 2);
$rtds = trim($rtds);
$offset = fgetc($fp);
$r2pt = fread($fp, 2);
$r2pt = trim($r2pt);
$offset = fgetc($fp);
$rec = fread($fp, 3);
$rec = trim($rec);
$offset = fgetc($fp);
$recyds = fread($fp, 3);
$recyds = trim($recyds);
$offset = fgetc($fp);
$rectds = fread($fp, 2);
$rectds = trim($rectds);
$offset = fgetc($fp);
$p2pts = fread($fp, 2);
$p2pts = trim($p2pts);
$offset = fgetc($fp);
$xpm = fread($fp, 3);
$xpm = trim($xpm);
$offset = fgetc($fp);
$xpa = fread($fp, 3);
$xpa = trim($xpa);
$offset = fgetc($fp);
$fgm = fread($fp, 3);
$fgm = trim($fgm);
$offset = fgetc($fp);
$fga = fread($fp, 3);
$fga = trim($fga);
$offset = fgetc($fp);
$fgm1 = fread($fp, 3);
$fgm1 = trim($fgm1);
$offset = fgetc($fp);
$fgm3 = fread($fp, 3);
$fgm3 = trim($fgm3);
$offset = fgetc($fp);
$fgm4 = fread($fp, 3);
$fgm4 = trim($fgm4);
$offset = fgetc($fp);
$fgm5 = fread($fp, 3);
$fgm5 = trim($fgm5);
$offset = fgetc($fp);
$fum = fread($fp, 2);
$fum = trim($fum);
$offset = fgetc($fp);
$td_dist = fgets($fp);
$td_dist = trim($td_dist);
echo $td_dist,"<BR>";
$query = "INSERT INTO $dbtable VALUES ('$namesї0]','$namesї1]','$id','$team','$teamid','$pos','$inj_stat','$start','$played','$inact','$pcmp','$patt','$pyds','$pint','$ptds','$p2pt','$psck','$psckyds','$car','$ryds','$rtds','$r2pt','$rec','$recyds','$rectds','$p2pts','$xpm','$xpa' ,'$fgm','$fga','$fgm1','$fgm3','$fgm4','$fgm5','$fum','$td_dist')";
$result = mysql_query($query);
$count = $count + "1";
}
while(!Feof($fp));
fclose($fp);
echo "You added ",$count," records.";
?>
Now I am trying to improve that code to parse the file by using an array and also when I pull the data back in an array, I dont want to have to so much typing to acheive the results.
Does this make any sense?