Help required with grabbing specific data from a web page

Discussion in 'PHP' started by centralexpert, Nov 4, 2009.

  1. #1
    Hello,

    Could someone please help me with a simple script that grabs a specific value from a web page?

    The page contains a table with a column for player id and player points enclosed by <td></td> tags.

    There is 357 players in the table each with a player id and player points.

    The code i am using is as follows:

    
    $url = file_get_contents("http://www.myurl.com");
    
    preg_match('~<td>(.*?[^<])</td>~i',$url,$points);
    print $points[1];
    
    Code (markup):
    I can only grab the first points value.

    How can I grab the players points for each player?

    Regards

    Central
     
    centralexpert, Nov 4, 2009 IP
  2. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're going to need some kind of loop, I think. Perhaps a while loop? From your code, you're just checking one cell (<td></td>).
     
    smcblogger, Nov 4, 2009 IP
  3. emilybarbie

    emilybarbie Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $url = file_get_contents("http://www.myurl.com");

    preg_match_all('~<td>(.*?[^<])</td>~i',$url,$points);
    for ($i=0;$i<count($points[0]);$i++) {
    echo $point=$points[0][$i];
    }
     
    emilybarbie, Nov 5, 2009 IP
  4. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks guys i'll give it a try and see how i go.
     
    centralexpert, Nov 5, 2009 IP
  5. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah that works fine to display the data.

    My next mission is to update each player in the db with their latest points.

    I have the following code

    
    mysql_connect("server","user","password");
    mysql_select_db("database");
    
    $url = file_get_contents("http://www.mydomain.com");
    
    preg_match_all('~<td>(.*?[^<])</td>~i',$url,$points);
    
    for($i=0;$i<count($points[0]);$i++)
    	{
    	$point = $points[0][$i];
    	$sql = "UPDATE players_table SET points = '$point' WHERE id = '$i'";
    	$result = mysql_query($sql) or die(mysql_error());
    	if($result != false)
    	{
    	print "Updated row $i with points = $point<br>";
    	}
                 else
                 {
                 print "Error updating row $i<br>";
    	}
    mysql_close();
    
    PHP:
    When i run this script it doesn't insert anything in to the db apart from 0 (zero) yet when i print it out it shows the points....

    Any ideas why it's doing this?

    Regards
     
    centralexpert, Nov 5, 2009 IP
  6. brealmz

    brealmz Well-Known Member

    Messages:
    335
    Likes Received:
    24
    Best Answers:
    3
    Trophy Points:
    138
    #6
    hmmn.. you sure that their corresponding id was their row position (id = '$i')?
    also try to replace
    if($result != false)
    with
    if(mysql_affected_rows() != 0)
     
    brealmz, Nov 5, 2009 IP
  7. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    for($i=0;$i<count($points[0]);$i++)
    {
    $point = $points[0][$i];

    Maybe I'm wrong (only given it a couple moment's thought), but this line:
    for($i=0;$i<count($points[0]);$i++) is using $points as an array of one dimension, and this line: $point = $points[0][$i]; is using it as two (dimensioned array). Maybe you want $point = $points[$i]; ???
     
    smcblogger, Nov 5, 2009 IP
  8. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok so i figured out what was happening......

    The preg_match_all() function was returning the whole string including all html tags.

    When trying to insert these into a integer db field it was inserting a 0.

    Soooo...

    I stripped away the non numeric chars with preg_replace() and trimmed all remaining white space with trim() so all i am left with is the numeric data i require. Which gets me through that problem.

    However...

    There are some minus figures in the string which are being turned into plus figures because of the preg_replace() function is removing the hyphen (-) before the number.

    Anyone know of a way to exclude the hyphen (-) from the search for non numeric chars?

    Regards
     
    centralexpert, Nov 5, 2009 IP
  9. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Right then all figured out now.

    To get around the minus (-) being removed by the preg_replace() function i set it to remove only the html tags.

    
    mysql_connect("server","user","password");
    mysql_select_db("database");
    
    $url = file_get_contents("http://www.mydomain.com");
    
    preg_match_all('~<td>(.*?[^<])</td>~i',$url,$points);
    
    for($i=0;$i<count($points[0]);$i++)
    	{
    	$point = preg_replace('/<td>/','',$points[0][$i]);
    	$sql = "UPDATE Players_test SET points = '".trim(preg_replace('/<\/td>/','',$point))."' WHERE id = '$i'";
    	$result = mysql_query($sql) or die(mysql_error());
    	if($result != false)
    		{
    		print "Updated row $i with points = $point<br>";
    		}
    	}
    mysql_close();
    
    PHP:
    Thank you to you all.

    Regards

    Central
     
    centralexpert, Nov 5, 2009 IP