2

I'm building something so I can parse latitudes and longitudes from an XML file. The problem is that I have a users with multiple lat and lng (used for markers on google maps) coordinates and only the first coordinate is saved in the array. I would like to have every coordinate in the array. It looks like that the foreach function isn't working properly

Here is the ajax call to the php file where I parse the xml file. And also test if the parsed data is working with json.

<html>

<head>
    <script type="text/javascript" src="jquery-2.1.0.js"></script>

    <script>


    $(function()
    {
        $.ajax(
            {
                type:"GET",
                url:"leesstudent.php",
                success:callback
            }
        );
    });


    function callback(data,status)
    {
        alert(data);
        var jsArr = JSON.parse(data);
        var coordinates = new Array();
        alert(jsArr[0]);
        for(var i=0;i<jsArr.length;i++)
        {
            $("#message").append("<p>" + jsArr[i].latitude + " " + jsArr[i].longitude + "</p>");
        }
    }

    </script>
</head>

<body>

<div id="message">
    Message..
</div>  

</body>

The php file where I parse the xml, from my opinion the foreach doesn't work

$xml = simplexml_load_file("Database.xml");
$coordinaten = array();
$teller = 0;
foreach($xml->user as $item)
{
    $coordinaten[$teller]["latitude"] = (string)$item -> latitude;
    $coordinaten[$teller]["longitude"] = (string)$item -> longitude;
    $teller++;
}
print json_encode($coordinaten);

the xml code

<root>

<user>
    <username>$2y$11$6SxUsvoDGlwm3Ji6BcnzCu/QyUWNy09Ny/.M9rXIpvImgJ1igJppy</username>
    <password>$2y$11$6SxUsvoDGlwm3Ji6BcnzCu7hIhAyNKtdlui1.oMdK4gQJnkZrL/Ky</password>
    <latitude>50.74688365485319</latitude><longitude>5.0701904296875</longitude>
    <latitude>51.09662294502995</latitude><longitude>4.9713134765625</longitude>
</user>

</root>

I only get the first latitude and longitude data, I would like have both (and in the future even more).

2
  • Do you have a sample of the XML your PHP code is attempting to parse? Commented May 22, 2014 at 13:19
  • stackoverflow.com/questions/8830599/… Commented May 22, 2014 at 13:27

1 Answer 1

1

Your foreach loop isn't correct. He will loop trough the users but never loop trough your coordinats!

    foreach($xml->user as $item){

    $teller = 0;
    foreach($item -> latitude as $test )
    {
         $coordinaten[$teller]["latitude"] = (string)$test;
         $teller++;
    }

    $teller = 0;

    foreach($item -> longitude as $test2)
    {
         $coordinaten[$teller]["longitude"] = (string)$test2;
         $teller++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.