Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/updateroster-CBS-htmlparser.php
<?php 
include ("htmlparser.inc");
 
require_once("offlconfig.php"); 
require_once($DOC_ROOT . "/lib/header.php"); 
 
$x = new OFFL_NFLTeam(); 
$teams = $x->getAllNFLTeams(); 

foreach($teams as $team) {
	$nflteamcity = $team->getNFLTeamCity(); 
	$nflteamname = $team->getNFLTeamName(); 
	$nflteamabbv = $team->getNFLTeamAbbv(); 
	$nflteamfullname = str_replace(" ", "", strtolower($nflteamcity . $nflteamname)); 

	echo $nflteamfullname . "<br/>";
}

$x = new OFFL_Position(); 
$positions = $x->getAllPositions(); 
$pos_id = Array(); 
$pos_name = Array(); 
 
foreach ($positions as $position) { 
	$pos_id[$position->getPositionAbbv()] = $position->getPositionID(); 
	$pos_name[$position->getPositionID()] = $position->getPositionAbbv(); 
} 
 
$warnings = array(); 
$playersmoved = array(); 
$sqllist = Array();
 
//do not reset defense
$sqllist[0] = "UPDATE players SET active=0 WHERE position_id<>8"; 

$num = 0;  
 
// here's the main team iterative loop 
foreach($teams as $team) {
	$player = array(); 
	// structure of the old player object: 
	// $player[$i][0] last name 
	// $player[$i][1] first name 
	// $player[$i][2] position abbv 
	// $player[$i][3] profile relative url 
	// $player[$i][4] trash, but I'll use this to store nflteam_id 

	$nflteamcity = $team->getNFLTeamCity(); 
	$nflteamname = $team->getNFLTeamName(); 
	$nflteamabbv = $team->getNFLTeamAbbv(); 
	$nflteamfullname = str_replace(" ", "", strtolower($nflteamcity . $nflteamname)); 
	 
	//$teamurl = "http://www.nfl.com/teams/" . $nflteamfullname . "/roster?team=" . $nflteamabbv; 
	$teamurl = "http://www.sportsline.com/nfl/teams/roster/" . $nflteamabbv;
	 
	$nflteam_id = $team->getNFLTeamID(); 
	 
	$teamroster = file("$teamurl"); 
	$teamstring = ""; 
 
	foreach ($teamroster as $teamline) { 
		$teamstring .= $teamline; 
	} 
	
	list($beforetable, $tableandafter) = explode("COLLEGE</a></td></tr>", $teamstring, 2); 
	unset($beforetable); 
	unset($teamstring); 
	list($table, $aftertable) = explode("<tr  id=special align=middle  class=bg1>", $tableandafter, 2); 
	unset($aftertable); 
	unset($tableandafter); 
	
	//echo $table;
	 
	$parser = new HtmlParser ($table);

    $currentPlayerNbr = '';
    $currentPlayerName = '';
    $currentPlayerPosition = '';
	$currentPlayerProfile = '';
    
    while ($parser->parse()) {
		//echo $players_raw[$i] . "<br/>";
		
		//echo $parser->iNodeType . " - " . $parser->iNodeName . " - " . $parser->iNodeValue . "<br/>";

		if (($parser->iNodeType == 1) && ($parser->iNodeName == 'tr')) {
			//echo "new player<br/>";
			$i++;
		    $currentPlayerNbr = '';
		    $currentPlayerName = '';
		    $player_lname = '';
		    $player_fname = '';
		    $currentPlayerPosition = '';
		    $currentPlayerProfile = '';
		} elseif (($parser->iNodeType == 1) && ($parser->iNodeName == 'a') && ($currentPlayerProfile == '')) {
            $attrValues = $parser->iNodeAttributes;
			$currentPlayerProfile = "http://www.sportsline.com" . $attrValues['href'];
			//$currentPlayerProfile = $attrValues['href'];
		} elseif (($parser->iNodeType == 3) && ($parser->iNodeName == 'Text') && ($currentPlayerNbr == '')) {
			$currentPlayerNbr = $parser->iNodeValue;
		} elseif (($parser->iNodeType == 3) && ($parser->iNodeName == 'Text') && ($currentPlayerName == '')) {
			$currentPlayerName = $parser->iNodeValue;

			list ($player_lname, $player_fname) = explode(", ", $currentPlayerName, 2); 
		} elseif (($parser->iNodeType == 3) && ($parser->iNodeName == 'Text') && (trim($parser->iNodeValue) != '') && ($currentPlayerPosition == '')) {
			$currentPlayerPosition = $parser->iNodeValue;

			$player[$i][0] = $player_lname;
			$player[$i][1] = $player_fname;
			$player[$i][2] = $currentPlayerPosition;
			$player[$i][3] = $currentPlayerProfile;
//			$player[$i][4] = ''; // blank this out to use later for nflteam_id 

			$player[$i][0] = mysql_escape_string($player[$i][0]); // last name 
			$player[$i][1] = mysql_escape_string($player[$i][1]); // first name 
				 
			if (strcmp($player[$i][2], "FB") == 0) 
				$player[$i][2] = "RB"; 
	
			foreach ($pos_name as $pos) { 
				if (strcmp($player[$i][2], $pos) == 0) { 
					$player[$i][2] = $pos_id[$pos]; 
					break; 
				} 
			} 

			//echo $i . ":" . $currentPlayerNbr . " - " . $player_lname . " - " . $player_fname . " - " . $currentPlayerPosition . " - " . $currentPlayerProfile . "<br/>";
		}
	} 
 
	foreach ($player AS $i=>$plr) { 
		//only handle players that are in a valid FFL position
		if(is_numeric($player[$i][2])) { 
			//$sql = "SELECT player_id, nflteam_id, fname, lname, position_id, nfl_profile FROM players WHERE lname = '" . $player[$i][0] . "' AND fname = '" . $player[$i][1] . "'"; //nflteam_id = " . $team->getNFLTeamID() . " AND 
			$sql = "SELECT player_id, nflteam_id, fname, lname, position_id, nfl_profile FROM players WHERE nfl_profile = '" . $player[$i][3] . "'"; //nflteam_id = " . $team->getNFLTeamID() . " AND 
			$result = mysql_query($sql) or die (mysql_error() . ": $sql");//,$this->_conn 
			$multi = mysql_num_rows($result); 
			if($multi > 1) { 
				$warnings[]= "<tr><td colspan=\"8\">Warning, multiple players by this name: " . $player[$i][1] . " " . $player[$i][0] . "</td></tr>"; 
			} else {  
				if(mysql_result($result,0,"player_id") != "") { 
					$sqllist[] = "UPDATE players SET active=1, lname='" . $player[$i][0] . "', fname='" . $player[$i][1] . "', nflteam_id=" . $team->getNFLTeamID() . ", nfl_profile='" . $player[$i][3] . "' WHERE player_id=" . mysql_result($result,0,"player_id"); } 
				else { 
					$sqllist[] = "INSERT INTO players (lname,fname,nflteam_id,position_id,nfl_profile) VALUES ('" . $player[$i][0] . "','" . $player[$i][1] . "'," . $team->getNFLTeamID() . "," . $player[$i][2] . ",'" . $player[$i][3] . "')"; } 
				} 
				$num++; 
		} else { 
			unset($player[$i]); 
		} 
	}
} 

if(sizeof($warnings)) { 
	echo "<table>"; 
	echo "<tr><td>Following players not updated; handle these players manually:</td></tr>"; 

	foreach($warnings AS $warning) { 
		echo $warning; 
	} 

	echo "</table>"; 
} 

$db = new OFFL_DBObject(); 
$dbconn = $db->getConn(); 
 
foreach ($sqllist as $sql) { 
	$result = mysql_query($sql, $dbconn); 
	echo "$result - $sql<br>\n"; 
} 
 
$db->closeDB(); 
 
echo "<h2>Player update complete!</h2>"; 
 
?>
Return current item: Online Fantasy Football League