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
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>).
$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]; }
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
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)
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]; ???
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
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