[英]PHP - Output buffering in a while loop not displaying correctly
我正在使用XMLReader解析一个大文件,然后使用XPATH和输出缓冲功能来显示搜索结果。
$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 ($xpath->evaluate('number(price)', $node) > $price_submitted) {
$nameArray[] = $name;
$category = $xpath->evaluate('string(@category)', $node);
$name = $xpath->evaluate('string(name)', $node);
$price = $xpath->evaluate('number(price)', $node);
这是while循环中输出缓冲开始的地方
ob_start();
echo "Category: " . $category . ". ";
echo "Name: " . $name . ". ";
echo "Price: " . $price . ". ";
echo "<br>";
$output = ob_get_contents();
ob_end_clean();
}
$reader->next('product');
}
然后,在页面的其他区域中将显示搜索结果。 但是仅显示一个搜索结果。 任何建议。
$output .= ob_get_contents();
您需要在等于之前加上句点,以将$ output的值与ob_get_contents()合并。
怎么得到的,您只得到最后一个,因为$ output被设置为ob_get_contents而不合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.