-2

i am trying to extract data from XML with xpath and php,but the xpath command is not parsing the string php var,thus am not getting back any result this is my php code

$term=$_POST["term"];
var_dump($term);
$xml = simplexml_load_file('Dbase.xml');
echo "$term";
$Movies = $xml->xpath('/Database/Movie[contains(Title,'$term')]');

foreach($Movies as $Movie) {
echo "Found {$Movie->Title}<br />";
echo "Found {$Movie->Cast}<br />";
echo "Found {$Movie->Producer}<br />";
}

i forgot to mention that a previous page containing a post form sends the data( i will use to search) in the input named "term"

MrRobot
  • 483
  • 1
  • 7
  • 18
  • 1
    pls post the XML, valid, but minimum necessary. – michi Jan 31 '16 at 15:53
  • Your xpath seems wrong, look here: http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more – bdn02 Jan 31 '16 at 15:53
  • 1
    `$Movies = $xml->xpath('//Database/Movie[contains(Title,"'.$term.'")]');` perhaps? – Professor Abronsius Jan 31 '16 at 16:00
  • @RamRaider suggestion was great it works fine thanks,could you provide why it worked that way? – MrRobot Jan 31 '16 at 16:04
  • 1
    you were not using quotes correctly ( single quotes do not allow for the inclusion of a variable unless escaped ) and the query needs to begin with a double slash - unless you provide a root node for the search to operate from – Professor Abronsius Jan 31 '16 at 16:11
  • i already have been answered by RamRaider,no need for the XML doc upload – MrRobot Jan 31 '16 at 23:38

2 Answers2

1

As @RamRaider posted this is the solution to my problem :

$Movies = $xml->xpath('//Database/Movie[contains(Title,"'.$term.'")]');

it is so because the quotes i used,were not operating correctly,so the variable was not parsed at all.

MrRobot
  • 483
  • 1
  • 7
  • 18
0

I don't think your $term will be expanded inside a single quoted string. Try:

$Movies = $xml->xpath("/Database/Movie[contains(Title,'$term')]");
TheMagicCow
  • 396
  • 1
  • 10