Jump to content

PHP XML question


eje

Recommended Posts

When I run my php script I get only the results for the first Driver listed in the XML doc, the foreach loop will not grab the other two Drivers.  I feel I'm missing something very simple here but I just can't seem to figure it out.  The XML doc and my script are as follows:

 

<statsXML>

  <RaceResults>

    <Race>

      <Driver>

        <Name>Driver1</Name>

        <Lap num="1" p="16" et="41.3956" s1="29.7811" s2="30.4392" s3="25.9101" fuel="0.953">86.1305</Lap>

        <Lap num="2" p="17" et="127.5261" s1="19.6057" s2="26.1722" s3="26.0336" fuel="0.906">71.8115</Lap>

        <Lap num="3" p="15" et="199.3376" s1="20.0280" s2="26.9223" s3="23.2915" fuel="0.855">70.2418</Lap>

        <Lap num="4" p="14" et="269.5793" s1="19.5144" s2="26.6192" s3="24.0424" fuel="0.808">70.1760</Lap>

        <Lap num="5" p="13" et="339.7553" s1="18.7761" s2="25.9043" s3="23.5552" fuel="0.761">68.2355</Lap>

        <Lap num="6" p="13" et="407.9908" s1="18.8694" s2="25.7919" s3="23.6949" fuel="0.714">68.3562</Lap>

        <Lap num="7" p="13" et="476.3470" s1="19.0703" s2="25.9529" s3="23.7024" fuel="0.663">68.7257</Lap>

        <Lap num="8" p="13" et="545.0727" s1="18.6340" s2="31.7123" s3="65.3759" fuel="0.635" pit="1">115.7222</Lap>

        <Lap num="9" p="13" et="660.7949" s1="24.3446" s2="25.8754" s3="23.7406" fuel="0.945">73.9606</Lap>

        <Lap num="10" p="12" et="734.7556" s1="19.0858" s2="25.5025" s3="23.4923" fuel="0.898">68.0806</Lap>

        <Lap num="11" p="12" et="802.8361" s1="20.0309" s2="25.4569" s3="22.9788" fuel="0.851">68.4666</Lap>

        <Lap num="12" p="12" et="871.3027" s1="18.7256" s2="25.6147" s3="32.8634" fuel="0.804">77.2037</Lap>

        <Lap num="13" p="11" et="948.5063" s1="21.1912" s2="30.6428" s3="24.3612" fuel="0.765">76.1952</Lap>

        <Lap num="14" p="9" et="1024.7015" s1="19.1867" s2="25.0103" s3="23.6392" fuel="0.718">67.8363</Lap>

      </Driver>

      <Driver>

        <Name>Driver2</Name>

        <Lap num="1" p="13" et="41.3956" s1="26.9926" s2="29.7078" s3="24.3721" fuel="0.953">81.0724</Lap>

        <Lap num="2" p="15" et="122.4680" s1="19.8542" s2="25.8926" s3="28.8351" fuel="0.906">74.5819</Lap>

        <Lap num="3" p="13" et="197.0500" s1="19.2349" s2="25.9964" s3="23.6990" fuel="0.859">68.9303</Lap>

        <Lap num="4" p="12" et="265.9803" s1="19.4375" s2="25.1173" s3="23.6292" fuel="0.812">68.1840</Lap>

        <Lap num="5" p="11" et="334.1643" s1="18.8468" s2="24.8001" s3="23.2518" fuel="0.765">66.8987</Lap>

        <Lap num="6" p="11" et="401.0631" s1="18.8374" s2="26.1268" s3="22.7785" fuel="0.718">67.7427</Lap>

        <Lap num="7" p="11" et="468.8058" s1="18.7792" s2="25.3028" s3="23.9204" fuel="0.671">68.0023</Lap>

        <Lap num="8" p="10" et="536.8081" s1="19.0460" s2="25.0647" s3="24.5304" fuel="0.624">68.6411</Lap>

        <Lap num="9" p="11" et="605.4492" s1="19.1918" s2="32.6066" s3="37.3007" fuel="0.580" pit="1">89.0990</Lap>

        <Lap num="10" p="13" et="694.5482" s1="92.2460" s2="25.7184" s3="24.6580" fuel="0.953">142.6224</Lap>

        <Lap num="11" p="13" et="837.1706" s1="22.8768" s2="26.0296" s3="25.1890" fuel="0.910">74.0955</Lap>

      </Driver>

      <Driver>

        <Name>Driver3</Name>

        <Lap num="1" p="14" et="41.3956" s1="28.5505" s2="28.7433" s3="24.6022" fuel="0.953">81.8961</Lap>

        <Lap num="2" p="12" et="123.2917" s1="19.2948" s2="26.7753" s3="23.6191" fuel="0.914">69.6892</Lap>

      </Driver>

    </Race>

  </RaceResults>

</statsXML>

 

<?php

 

$id = 0;

$xml = simplexml_load_file('statsxml.xml');

 

foreach($xml->RaceResults->Race->Driver[$id]->Lap as $item)

{

print $item->attributes()->num;

print $item->attributes()->s1;

print $item->attributes()->s2;

print $item->attributes()->s3;

print $item->attributes()->fuel;

print $item;  //prints lap time

print '<hr>';

$id = $id++;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/196023-php-xml-question/
Share on other sites

This did the job:

 

<?php

 

$xml = simplexml_load_file('statsxml.xml');

 

foreach ($xml->RaceResults->Race->Driver as $driver){

 

foreach ($driver->Lap as $results){

print $results->attributes()->num;

print $results->attributes()->s1;

print $results->attributes()->s2;

print $results->attributes()->s3;

print $results->attributes()->fuel;

print $results;  //prints lap time

      print '<hr>';

      }

}

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/196023-php-xml-question/#findComment-1031427
Share on other sites

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.