I'm new to DOMs, I was developing a website where when person clicks on let's say on FORD, link would refer to cars.php?model=ford and would show all FORD car owners. I put php like this:
<?php
//retrieve xml constants
include("includes/constants.php");
//create instance of xml doc
$ldoc = new DOMDocument();
//load login fild into $ldoc
$ldoc -> load($LoginFile);
$xpath = new DOMXPath($ldoc);
$model = $_GET["model"];
$query = "//loginData/member/model[. = '".$model."']";
$queryResult = $xpath->query($query);
$queryNode = $queryResult->item(0)->parentNode;
foreach($queryNode as $parent)
{
$login = $parent->getElementsByTagName("login");
$loginValue = $login->item(0)->nodeValue;
echo ($loginValue);
}
?>
I want it to search all nodes with value FORD and then refer to its parentNode and display login in login node
First use DOMXpath::evaluate() not DOMXpath::query(). With evaluate() you can use all kind of Xpath expression, with query() only the once that return node lists.
Speaking of node lists. You expression returns an node list, with $queryNode = $queryResult->item(0)->parentNode;
you try to read the parent node of the first node in the returned node list. It seems that you node list is empty and so that fails.
Using xpath you can fetch all model ford car owners, directly.
$members = $this->evaluate("//loginData/member[model='FORD']");
This will return all member element with a child node "model" with the text content "FORD". In your description, you use the uppercase "FORD", in the URL the lowercase "ford". Which one is it?
Using the second argument of evaluate()/query(), you can provide a context node for an Xpath expression. Using it with evaluate() allows to fetch string values directly from XML nodes.
foreach ($members as $member) {
var_dump(
$xpath->evaluate('string(//model)', $member)
);
}