Jump to content

Archived

This topic is now archived and is closed to further replies.

animus

Filter XML using a PHP variable

Recommended Posts

Hello,

I have an issue that I cannot seem to resolve using XML and PHP.

[b]Given:[/b]

The XML
[a href=\"http://www.aagrating.com/19space.xml\" target=\"_blank\"]19space.xml[/a]

The XSL
[a href=\"http://www.aagrating.com/format.xsl\" target=\"_blank\"]http://www.aagrating.com/format.xsl[/a]

The CSS
[a href=\"http://www..agrating.com/table.css\" target=\"_blank\"]http://www..agrating.com/table.css[/a]


Given this setup I would like to be able to filter my data given a PHP variable (i.e. form submits, loads XML and filters based off of user input). I can use xpath to output the format to a table correctly, but when I attempt to set a variable for XPath it locks up.

the php script:[a href=\"http://www.aagrating.com/xmlTest.txt\" target=\"_blank\"]xmlTest.php[/a]

I have attempted to use "$myVar", '$myVar' " '+myVar+' ", but I cannot find the correct syntax. Is this eveb possible? Any thoughts?

Thanks in advance!

-thom

Share this post


Link to post
Share on other sites
As far as I know you cannot use variables in Xpath expressions (or at least I have never gotten it to work). A nice tutorial on expath expressions: [a href=\"http://www.zvon.org/xxl/XPathTutorial/General/examples.html\" target=\"_blank\"]http://www.zvon.org/xxl/XPathTutorial/General/examples.html[/a]

What you can do is look for all elements with the "type" tag, and go through them all and only show those that have the value of "$myVar".

I don't have any experience with simpleXml stuff, but I hope you get the idea if the syntax is not really correct...

Hope it helps....

[code]<?php
  $s = simplexml_load_file('19space.xml');
  $myVar="19W4";  
   $products = $s->xpath('//product'); //search for all "product" elements
  
   foreach($products as $product) //go through list of all products
  {
           //if the type of $product matches user input (ie $myVar)
       if ($product->type == $myVar)
       {
           echo "Found {$product->type} <br />";
    }
   }
?>[/code]

Share this post


Link to post
Share on other sites

×

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.