Jump to content

Need Help: Parsing data from xml in php


SirMattBusby

Recommended Posts

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 :D
 
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   :)
Link to comment
https://forums.phpfreaks.com/topic/289407-need-help-parsing-data-from-xml-in-php/
Share on other sites

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.