Updating a player ranking automatically

Discussion in 'PHP' started by mark_s, Jun 18, 2007.

  1. #1
    Would someone be kind enough to tell me the sort of PHP code required for my tennis website to use the official rankings on ATP.com so that it updates the ranking of a particular player on my own website automatically?

    EDIT: http://www.atptennis.com/3/en/rankin...em/default.asp
     
    mark_s, Jun 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    I looked at the site, and couldn't find what you're talking about. Can you post a link to the site where you want to extract the content from?
     
    nico_swd, Jun 18, 2007 IP
  3. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    mark_s, Jun 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    <?php
    
    $data = file_get_contents('http://www.atptennis.com/3/en/rankings/entrysystem/default.asp');
    
    preg_match_all('/<div class="entrylisttext">(.*?)<\/div>/s', $data, $matches);
    $int = 0;
    
    // Free some memory
    unset($matches[0]);
    
    foreach ($matches[1] AS $match)
    {
    	preg_match('/^([^\(]+)\(([^\)]+)\)/', @strip_tags($matches[1][++$int]), $playerinfo);
    	
    	$players[trim($playerinfo[1])] = array(
    		'points'  => @strip_tags($matches[1][++$int]),
    		'pos'     => @strip_tags($matches[1][++$int]),
    		'tourns'  => @strip_tags($matches[1][++$int]),
    		'country' => $playerinfo[2]
    	);
    	
    	$int++;
    }
    
    
    // Usage example for $players array:
    foreach ($players AS $player => $playerinfo)
    {
    	echo "<strong>{$player}:</strong><br />\n";
    	
    	echo "Points: {$playerinfo['points']}<br />\n";
    	echo "Position: {$playerinfo['pos']}<br />\n";
    	echo "Tournaments: {$playerinfo['tourns']}<br />\n";
    	echo "Country: {$playerinfo['country']}<br /><br />\n";
    }
    
    
    // Or get the info for a specific player. (The name has to be exact)
    $info = $players['Murray, Andy'];
    
    echo '<pre>' . print_r($info, true) . '</pre>';
    
    
    ?>
    PHP:
     
    nico_swd, Jun 18, 2007 IP
  5. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Wow thank you, that should come in real handy!

    Can you tell me how to do the same for his profile page, I realise now that his profile page is better to use just for ranking.

    I tried using his profile page instead, using this code:

    <?php
    $data = file_get_contents('http://www.atptennis.com/3/en/players/playerprofiles/?playernumber=MC10');
    $regex = '/<div class="data">Current ATP Ranking - Singles:</div></td><td valign="middle"><span class="lines"> (.+?) </span>/';
    preg_match($regex,$data,$match);
    var_dump($match);
    echo $match[1];
    ?>
    Code (markup):
    But that simply displayed 'array(0) { }'
     
    mark_s, Jun 18, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Lol, you could have thought of that earlier. :(


    Anyway... Change the regex to:
    
    $regex = '/<div class="data">Current ATP Ranking - Singles:<\/div>\s*<\/td>\s*<td valign="middle"><span class="lines">\s*(\d+)\s*<\/span>/';
    
    PHP:
     
    nico_swd, Jun 18, 2007 IP
  7. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm so sorry but I will keep your code and it will probably come in useful in the near future.

    Thanks for the revised regex code although it displays too much information. I only want the number '8' displayed.

    This is what is displayed:

    array(2) { [0]=> string(125) "
    Current ATP Ranking - Singles:
    8 " [1]=> string(1) "8" } 8
     
    mark_s, Jun 18, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Take out the var_dump().

    $match[1] holds the 8.
     
    nico_swd, Jun 18, 2007 IP
  9. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This code displays nothing,

    <?php
    $data = file_get_contents('http://www.atptennis.com/3/en/players/playerprofiles/?playernumber=MC10');
    $regex = '/<div class="data">Current ATP Ranking - Singles:<\/div>\s*<\/td>\s*<td valign="middle"><span class="lines">\s*(\d+)\s*<\/span>/';
    echo $match[1];
    ?>
    
    Code (markup):
     
    mark_s, Jun 18, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Because you took the preg_match() function out. JUST take out the var_dump() of the piece of code where it displayed too much.
     
    nico_swd, Jun 18, 2007 IP
  11. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Fantastic, it works!!! Thank you so much.

    May I ask why you used (\d+) instead of (.+?)?
     
    mark_s, Jun 18, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    It would work the same probably, but it's a little bit more specific the way I did, because \d only matches numbers, while .+? matches any character except new lines. (Unless the s modifier has been specified)
     
    nico_swd, Jun 18, 2007 IP
  13. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #13
    oh right, fair enough :)
     
    mark_s, Jun 18, 2007 IP
  14. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #14
    How can I make it so that 'new lines' are not parsed?

    Also, is there a way to make it look in the source code bottom to top?
     
    mark_s, Jun 18, 2007 IP