SirMattBusby Posted July 3, 2014 Share Posted July 3, 2014 (edited) Hi there everyone... I've just joined this forum to get a little :help: with something I'm working on, and also to try to learn as much as I can from others on this forum I play an online football management game called Footstar, I'm trying to develop app's for the game... The game's development network uses .xml feeds to supply its developers with all sorts of database information such as player names, team names, nationalities, and so on.... I'm absolutely brand new to PHP, It's only because of this game I am now learning it, I'm man utd manager and president of the English football association in the game, so im trying to help out as much as I can... I have a decent knowledge of HTML and am familiar with a lot of web design aspects... but like I said, Im new to PHP :oops: This is what i need help with if possible... I have extensivley looked through tutorials on w3schools and other similar sites but still cant get my head around this problem... <?php $manutd = simplexml_load_file('manutd.asp'); $con=mysqli_connect("****","****","****","****"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } foreach ($manutd->xpath('//player') as $player) { $sql="INSERT INTO Players (ID, Name, Age) VALUES ('$player->player_id', '$player->player_name', '$player->age')"; } if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "Records Updated"; mysqli_close($con); ?> ^ this code is supposed to draw certain data from the manutd.asp feed and insert them into a table in my mysql database.... this works, but... the script is only taking the first set of data... for example, this is a snippet from my manutd.asp file: <player> <player_id>54101</player_id> <player_name>Jardel Leal</player_name> <age>24</age> </player> <player> <player_id>62643</player_id> <player_name>Olexandr Shelestenko</player_name> <age>28</age> </player> My script will only 'pick up'/'read' the first player on the list, ignoring the 50+ entries afterwards. If there is anybody who can put me on the right track / show me where Im going wrong, I'd be very grateful Thanks in advance Matt Edited July 3, 2014 by SirMattBusby Quote Link to comment https://forums.phpfreaks.com/topic/289407-need-help-parsing-data-from-xml-in-php/ Share on other sites More sharing options...
Raz3rt Posted July 3, 2014 Share Posted July 3, 2014 (edited) As i found searching over the internet : "You do not use relative paths in a loop body. Of course this always produces the same (i.e. the absolute) result." What you can try is put your path of your XML values into an variable first and then INSERT INTO your sql with these variables i.e something like that (maybe you will find allot on the internet (xpath absolute value xml). $player_id = $player->xpath("./player_id/value"); Ik also ran into this : http://php.net/manual/en/domxpath.query.php It looks like this is a better (finer way) to do these xml gets! Edited July 3, 2014 by Raz3rt Quote Link to comment https://forums.phpfreaks.com/topic/289407-need-help-parsing-data-from-xml-in-php/#findComment-1483669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.