READ THROUGH ALL POSTS HERE BEFORE ANSWERING
I've already figured some stuff out... for your benefit check it all out!

ive been lookin over some simplexml notes/docs and i havent really found a way to search and return results

example:

<?xml ...?>
<products>
     <cat1>
          <item>
          <name>product 1</name>
          <color>blue</color>
          </item>
     </cat1>
     <cat2>
          <item>
          <name>product 2</name>
          <color>blue</color>
          </item>
     </cat2>
</products>

if the user searches "blue", then i should be able to step through and process the valid <item> structures... same for if they search for "product 1", then just process the one valid structure (including its category)

any links to snippets or ideas are welcome!

thanks guys

    it looks like it can be done with xpath() but im still not sure how the search is exactly "worded"

    <?php
    $xmlFileName = 'products/xml/products.xml';
    $xml = simplexml_load_file($xmlFileName);
    
    $search_term = 'glycolic';
    
    $result = $xml->xpath("/name=$search_term");
    
    echo "<pre>";
    print_r($result);
    echo "</pre>";
    
    while(list( , $node) = each($result)) {
        echo 'Result: ',$node,"\n<br/>";
    }
    ?>
    

    is what im working with

    i want to search the XML tree and compare the search term to any <name> tag and return the <item></item> node that contains that found name

      ok.. break through!
      (documentation here)

      this works as a pure search:

      
      $search_term = '';
      $result = $xml->xpath("/products//item[name='".$search_term."']");
      

      so in short... return ALL item nodes under PRODUCTS whose name EQUALS $search_term

      that // is powerful b/c it'll skip any further tags in between (e.g. for me its some category assignment)

      only issue here is that there is no forgiveness with the terms... b/c its equals and not like it wont return something unless u give it the full product name... if anyone know how to ramify this let me know please

      NOW, my next goal is to be able to

      return ALL item nodes whose type EQUALS $search_term... except that type looks like this:

      <type>
      <t1></t1>
      <t2></t2>
      </type>
      

      again.. any help is appreciated.. there definitely isnt enough documentation on xpath

        search bits now working 100%:

        $search = ucwords(rawurldecode($_GET['search']));
        
        $result = $xml->xpath("/products//item[contains(name,'".$search."')]");
        

        ive chosen to use ucwords b/c thats how all of my products are labeled and xPath IS CASE SENSITIVE!!!

        still toying with the type issue

        (more documentation)

        scroll down towards the bottom to see the operators

          type problem overcome:

          $type = ucwords(rawurldecode($_GET['type']));	
          $result = $xml->xpath("/products//item[contains(descendant::type,'".$type."')]");
          

          works like a charm

          ok... im off to get a stiff drink... with the amount of junk i just learned in the past 6 hours its time to RELAX

            Write a Reply...