if语句代码是动态构建的(基于提交的用户值)到下面的$ if_statement变量中,如下所示:
$keyword = trim($_GET["Keyword"]);
if (!empty($keyword)) {
$if_statement = ($keyword == $xpath->evaluate('string(title)', $node));
}
$shopByStore = $_GET["store"];
if (!empty($shopByStore)) {
$if_statement = ($if_statement && $shopByStore == $xpath->evaluate('string(store)', $node));
}
// plus 8 more GET methods retrieving potential user input for the $if_statement variable.
然后将使用XMLReader解析一个非常大的XML文件..
$reader = new XMLReader;
$reader->open('products.xml');
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
while ($reader->read() && $reader->name !== 'product') {
continue;
}
while ($reader->name === 'product') {
$node = $dom->importNode($reader->expand(), TRUE);
..然后这个if语句代码将测试XML循环中显示哪些值。由于在if语句中可以测试1到10个条件(取决于有多少GET方法从用户输入产生空变量),因此在系统上只测试一次空变量(在此之前)似乎不那么费劲循环执行)比在while循环的每个循环期间测试空变量。当(在某些情况下)仅需要测试1或2个条件时,测试10个条件是浪费。
有没有办法在下面的while循环中使用if_statement变量(上面动态创建)?下面没有显示任何内容。
if ($if_statement) {
$name = $xpath->evaluate('string(name)', $node);
$store = $xpath->evaluate('string(store)', $node);
echo "Name: " . $name . ". ";
echo "Store: " . $store . ". ";
}}
答案 0 :(得分:0)
试试这个:
$relations = array('keyword'=>'title', 'store'=>'store'); // your others conditions relations in there.
while ($reader->read()) {
if($reader->name !== 'product'){continue;}
$node = $dom->importNode($reader->expand(), TRUE);
foreach($relations as $key => $val){
if(trim($_GET["$key"]) != $xpath->evaluate("string($val)")){
continue 2;
}
}
$name = $xpath->evaluate('string(name)', $node);
$store = $xpath->evaluate('string(store)', $node);
echo "Name: " . $name . ". ";
echo "Store: " . $store . ". ";
}
无法测试代码,但这可能会有些修补。
希望它有所帮助。