2 users online. Create an account or sign in to join them.Users
Using XMLElement to manipulate datasource $result
This is an open discussion with 9 replies, filed under Troubleshooting.
Search
Your first line assigns a copy of the result's children to the $entries variable. When you unset($entries[$key]), all you do is remove the entry from the copy and not from the actual result.
I haven’t done this before, but a quick look at the API documentation suggests you will need to use the removeChildAt() method. Try replacing your call to unset() with something like:
$result->removeChildAt($key);
If that doesn't work, try removing the conditional statements you have in there to determine if one of them is the culprit.
Ben's on the money :)
Well spotted, I tried passing it to the variable by reference, but that didn't work either.
I'll give that a go, thanks.
That worked perfectly thanks. (RTFM John, RTFM)
Ok, sorry to bump this...
Now I have the results and I'm manipulating stuff with XMLElement, is there any way to re-order the results based on the value of a node?
You can use setChildren() to replace all the child nodes of an XMLElement object with an array of new children:
$entries = $result->getChildren(); // Do stuff to $entries here to re-order its elements. $result->setChildren($entries); // Children of $result are now reordered.
Yeah, I know that bit, it's how to do the reordring based on a child-node's value that I hope I can do.
There's the getValue method, which you could use to make an array while iterating through the XMLElement(s), so that you can sort it.
I don't know about the structure of your XML, and I haven't tested it (it almost certainly won't work straight off), but maybe something along these lines?
foreach($result->getChildren() as $entries) {
foreach($entries->getChildren() as $entry) {
$values[] = $entry->getValue();
}
sort($values);
$entries_reordered = new XMLElement('entries-reordered');
foreach($values as $value) {
$entry_node = new XMLElement('entry', $value);
$entries_reordered->appendChild($entry_node);
}
$entries->setChildren($entries_reordered);
}
This is what I thought, I was hoping there would be some magical way of doing it.
Create an account or sign in to comment.
I'm trying to manipulate a datasource
$resultset usingXMLElementfunctions, and it just ain't doing anything. My code is as below, just before thereturn $result;statement in the datasourcegrabfunction:$entries = $result->getChildren('entry'); foreach($entries as $key => $entry) { $keep = 0; $children = $entry->getChildren(); foreach($children as $field) { if($field->getName() == 'average-luck-strategy') { if((int)$field->getValue() >= $ranges['luck-strategy'][0] && (int)$field->getValue() <= $ranges['luck-strategy'][1]) { $keep++; } } else if($field->getName() == 'average-casual-intense') { if((int)$field->getValue() >= $ranges['casual-intense'][0] && (int)$field->getValue() <= $ranges['casual-intense'][1]) { $keep++; } } else if($field->getName() == 'average-complex-simple') { if((int)$field->getValue() >= $ranges['complex-simple'][0] && (int)$field->getValue() <= $ranges['complex-simple'][1]) { $keep++; } } else { continue; } } if($keep == 0) { unset($entries[$key]); continue; } }Does anyone know why the datasource is returning all of the results, when I expect this code to whittle it down to only one out of 10?