Jump to content

Combing HTML file; Uploading data to mysql --- Need Help!


habsfan93

Recommended Posts

Hi there everybody!

I am having a problem with php and I was referred here by a friend who uses this forum often so hopefully someone can help me fix my problem.

 

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 work was actually initially done by someone else and I have now taken it over, and whatever it is that they did to account for the Total line does not work so now I am trying to fix it. The part that doesn't work is in between the ------ lines. When I first received the file, there were absolutely no comments, so whatever comments you see are things I put in to help myself remember what certain lines do.

 

<?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());
}

?>

 

 

I basically need a way to check whether or not a TOT line exists for the player, and then if it does, I need to replace the data from the first line with the data from the TOT line for inputting into the database.

 

Any help would be greatly appreciated as I have been working on this for a while now and can't seem to figure out how to achieve what I want to achieve.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.