Web Analytics Made Easy -
StatCounter While or Foreach help - CodingForum

Announcement

Collapse
No announcement yet.

While or Foreach help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • While or Foreach help

    hi all,

    Need some help with a loop.
    I'm working on a simple php script that extracts some information from an xml style dataset. The problem I get is I do not know how to loop round the data. For example the data is:

    PHP Code:
    $data "<START><DATA1>some data</DATA1><DATA2>more</DATA2></START>
    <START><DATA1>some other data</DATA1><DATA2>more still</DATA2></START>"

    Now, I can extract the first lot of data in <START></START> easy enough however don't know how to write a loop to extract the data within data1 and data2 for every <start></start>.

    The structured english would be something like:
    Code:
    FOREACH <START></START>{
    EXTRACT <DATA1></DATA1> AND <DATA2></DATA2>
    }
    The extract bit is no problem as I said, just need help making it loop round all of the <start></starts> in the data.

    Any help would be very much apperciated.
    Kind Regards,
    Sir P
    Reseller Web Hosting - Unlimited websites from $5.49
    cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

    Freelance Magento Job Classifieds

  • #2
    you should probably be using well-formed XML (which your example is not) and using the xml methods here:
    http://us3.php.net/manual/en/ref.domxml.php

    Your XML should be:
    Code:
    <xmldoc>
      <start><data1></data1><data2></data2></start>
      <start><data1></data1><data2></data2></start>
    </xmldoc>
    Then you getElementsByTagName('start') and do your loop over the array that gets returned.

    Comment


    • #3
      Thank you for your reply.

      Unfortunetaly I have no control over the quality of the data. It is not actually an XML file, just data in a similar structure. I cannot alter the XML format in anyway, this is why I'm not using the php parsing tools, and trying to get my code to simply loop

      any thoughts?
      cheers
      p
      Reseller Web Hosting - Unlimited websites from $5.49
      cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

      Freelance Magento Job Classifieds

      Comment


      • #4
        I will go into a bit more detail:

        I currently have this:

        PHP Code:
        $xmlRaw "<START><DATA1>some data</DATA1><DATA2>more</DATA2><DATA3>more again</DATA3></START> 
        <START><DATA1>some other data</DATA1><DATA2>more still</DATA2><DATA3>more</DATA3></START>"
        ;


        $xmlFieldNames=array("DATA1""DATA2""DATA3");
        foreach (
        $xmlFieldNames as $xmlField) {
           if(
        strpos($xmlRaw,$xmlField)!==false){
               
        $parsedXML[$xmlField]=substr($xmlRaw,
               
        strpos($xmlRaw,"<$xmlField>")+strlen("<$xmlField>"),
               
        strpos($xmlRaw,"</$xmlField>")-strlen("<$xmlField>")
               -
        strpos($xmlRaw,"<$xmlField>"));
           }
        }
        print_r($parsedXML); 
        This returns:
        Array
        (
        [DATA1] => some data
        [DATA2] => more
        [DATA3] => more again
        )
        So it is only getting the first lot of data in <START></START>. It should display this:
        Array
        (
        [DATA1] => some data
        [DATA2] => more
        [DATA3] => more again
        )
        Array2
        (
        [DATA1] => some other data
        [DATA2] => more still
        [DATA3] => more
        )
        Or similar.

        Real stuck. Any thoughts?

        Cheers,
        P
        Reseller Web Hosting - Unlimited websites from $5.49
        cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

        Freelance Magento Job Classifieds

        Comment


        • #5
          Your loop is only going through the $xmlFieldNames array once, so you are only going to get one result set. You'll need to enclose your current loop in another loop that cycles through each occurance of <START> tags.

          Comment


          • #6
            yeh exactly what i need to do.. but i dont understand how to widen the loop around it you see .. probably a thick moment :$

            any pointers?
            Reseller Web Hosting - Unlimited websites from $5.49
            cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

            Freelance Magento Job Classifieds

            Comment


            • #7
              Well it really depends on how complex that danged raw string is going to get. If it stays simple like you've shown, you can use a regex to build an array of the stuff inbetween each <START> tag. But... it's risky building your code to be dependant on data that you have no control over. And if you start making it more robust to handle more situations, then you'll find yourself writing an XML parser! Which you don't want to do I wouldn't imagine.

              Have you tried using the XML parsing functions?

              Comment


              • #8
                i seee, thanks for your help.

                although i canot change the current data, I can for sure say that it will always have the <start></start> aroundeach block of data.

                So what you mentioned about using regex to find everything inbetween <start></start> each time, sounds perfect ... I had a look at it on php.net but still dont understand the foreach bit ..


                How would it would, foreach occurance of <start></start> strip out middle...

                hmm bit lost on this...
                Reseller Web Hosting - Unlimited websites from $5.49
                cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

                Freelance Magento Job Classifieds

                Comment


                • #9
                  CHeers for all our help

                  got it sorted, got it all in an array.. which I then got stuck with ..
                  http://codingforum.net/showthread.ph...504#post493504

                  this thread can be ignored now
                  cheers
                  Reseller Web Hosting - Unlimited websites from $5.49
                  cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

                  Freelance Magento Job Classifieds

                  Comment

                  Working...
                  X