Question

I am using XMLReader to parse a large file, then using XPATH and the output buffering function to display the search results.

$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);

Here is where the output buffering starts in the while loop

ob_start();
echo "Category: " . $category . ". ";
echo "Name: " . $name . ". ";
echo "Price: " . $price . ". ";

echo "<br>";
$output = ob_get_contents();
ob_end_clean();
}
$reader->next('product');
}

Then in a different area of the page, the search results are to be displayed. But only one search result is being displayed. Any advice.

Was it helpful?

Solution

$output .= ob_get_contents();

You need a period before equals to combine the value of $output with ob_get_contents().

How you had it, you were only getting the last one because $output was being set to what ob_get_contents and not combining.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top