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   :)
Edited by SirMattBusby
Link to comment
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!

Edited by Raz3rt
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.