Ok, so I am trying to use PHP to comb through an html file, grab the data, and put it into a mysql database.
Here is the page I am trying to have put into a database:
http://cmhl.freehostia.com/CMHL4TeamScoring.html
This is basically a page of hockey statistics. I am able to get most of the data into mysql. My problem is encountered when a player has played for more than one team. Because when the page is generated, it displays the stats that that player has earned on each team along with a Total line. I have successfully programmed something that can lift off the first line of stats for the players current team. But I don't want those, I want the stats that are on the Total line.
Here is the code I have. This was actually initially done by someone else, and whatever it is that they did does not work so now I am trying to fix it. The part that doesn't work is in between the ------ lines
<?php
include_once("lib.php");
$url = $info[scoring];#Gets URL for processing
$player_names = player_names();
$fp = fopen ($url, "r");#Opens URL, read only.
if (!$fp) {
echo "ERROR: $errstr ($errno)<br>\n";#Can't load the file - error message
} else {
while($buffer=fgets($fp,512)) {#fgets reads the file line by line and stores the info into $buffer...loop progresses as long as there is data to read.
$buffer = strip_tags($buffer);#Strips HTML and PHP tags from file
foreach($player_names as $key => $value) {#Iterative loop that goes over array. Assigns unique keys to $key for each unique $value
if(strstr($buffer,$key)) #strstr finds first occurence of a string..in this case, returns strings $buffer starting at $key
{ $buffer=str_replace($key,$value,$buffer); }#str_replace — Replace all occurrences of the search string with the replacement string. For every line (buffer) replaces key with value.
}
$buffer = preg_replace("([\s]+)", " ", $buffer); #Looks for any amount of whitespace and replaces it with one whitespace.
$buffer = str_replace(",", ".", $buffer); #Replaces all commas with points.
#----------------------------------------------------------------------
if(substr($buffer, 0, 3)=="TOT") { #If string starts with TOT
$stats = preg_replace("([\s]+)", " ", $buffer); #Looks for any amount of whitespace and replaces it with one whitespace.
$stats = addslashes($stats);# Allows any special characters like ' to be included in entry
list($foo, $gp, $g, $a, $p, $plusminus, $pim, $pp, $sh, $gw, $gt, $ht, $s, $pctg, $gs, $ps) = split(' ', $stats); #splits line into individual array components where space occurs and assigns them to variables.
#Not sure how this works or helps.
if(!strstr($a,".")) {
$sql_del = strrpos($sql, "(");
$sql = substr($sql, 0, $sql_del);
$sql .= "('', '$pos', '$num', '$fname', '$lname', '$team', '$gp', '$g', '$a', '$p', '$plusminus', '$pim', '$pp', '$sh', '$gw', '$gt', '$ht', '$s', '$pctg', '$gs', '$ps', '$rookie')";
}
}
#-----------------------------------------------------------------------
$fchar = trim(substr($buffer, 0, 2)); #trim strips whitespace from beginning and end of string
if($fchar=="F"||$fchar=="C"||$fchar=="L"||$fchar=="R"||$fchar=="D"||$fchar=="G") {
# if fchar is equal to this OR that OR this, etc.
$stats = preg_replace("([\s]+)", " ", $buffer); #See above
$stats = addslashes($stats);
list($pos, $num, $fname, $lname, $team, $gp, $g, $a, $p, $plusminus, $pim, $pp, $sh, $gw, $gt, $ht, $s, $pctg, $gs, $ps) = split(' ', $stats);
if(substr($fname, 0, 1)=='*') {#Determines if player is a rookie
$rookie = 1;#Assigns player rookie status
$fname = str_replace("*", "", $fname);#Eliminates star
} else {
$rookie = 0;
}
if($sql) $sql .= ", \n"; #add comma and line break to $sql
$sql .= "('', '$pos', '$num', '$fname', '$lname', '$team', '$gp', '$g', '$a', '$p', '$plusminus', '$pim', '$pp', '$sh', '$gw', '$gt', '$ht', '$s', '$pctg', '$gs', '$ps', '$rookie')"; #add all this on each new line
}
}
}
if($sql) $sql2 = "INSERT INTO sorter_stats (id, pos, num, fname, lname, team, gp, g, a, p, plusminus, pim, pp, sh, gw, gt, ht, s, pctg, gs, ps, rookie) VALUES ".$sql;
if($sql2) {
db_execute("DELETE FROM sorter_stats") or die("del error!");
db_execute($sql2) or die("error! $sql2 ".mysql_error());
}
?>
Any ideas about how I can fix this?