【问题标题】:PHP - Output buffering in a while loop not displaying correctlyPHP - while循环中的输出缓冲未正确显示
【发布时间】:2014-04-19 23:24:49
【问题描述】:

我正在使用 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');
}

然后在页面的不同区域,将显示搜索结果。但只显示一个搜索结果。任何建议。

【问题讨论】:

  • 所以你在每次循环迭代时都调用 ob_start() ?

标签: php xpath while-loop xmlreader output-buffering


【解决方案1】:
$output .= ob_get_contents();

在 equals 之前需要一个句点来将 $output 的值与 ob_get_contents() 结合起来。

你是怎么得到它的,你只得到最后一个,因为 $output 被设置为 ob_get_contents 而不是组合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多