简体   繁体   中英

Need some help using a php variable in xpath

Thanks to your site and its helpful members I am almost completely finished with this project apart from one little problem .

Background ..

I have a form that enters data to an xml file and a page that displays the information based on 2 pieces of criteria it needs to match today's date and have the status out .

Problem is the code I am using gives me the data when it matches only status and not both I need it to match both before showing the data .

XML Structure ....

<information>
<entry>
<date></date>
<name></name>
<status></status>
<notes></notes>
<comments></comments>
</entry>
</information>

Code For Retrieving And Displaying Data ...

$lib  = simplexml_load_file("sample.xml");
$today = date('m/d/y');
$query = $lib->xpath("//entry[.//date[contains(., '   $today')]]|//entry[.//status[contains(., 'out')]]");
if( $query ) {
foreach($query as $node){
      echo "<div id='one'>$node->name</div>
<div id='two'>$node->notes</div> 
<div id='three'><div class='front'>$node->comments</div></div>";

}
}
else {
// Echo the special div.
}

So as you can see from the code I have it querying whether or not it contains today's date via the $today function (which is not working ) and querying whether or not it contains out in status but as of yet I can not get it to work to match both it only matches status.

I apologise if this has been answered somewhere before and I thank you all in advance for any help given on this matter . I also apologise if the question is too vague or I have not been forthcoming enough with details .

the | in your xpath-expression means or , try this instead:

... xpath("/information/entry[date = '01/23/2014' and status='out']")

It will work great without the contains() on

<date>01/23/2014</date>
<status>out</status>

use this

... xpath("/information/entry[date[contains(., '01/23/2014')] and status[contains(., 'out')]]")

on this:

<date>foo01/23/2014</date>
<status>barout</status>

see it working: https://eval.in/93642

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM