Marcus
Well-known member
Can you work with the query returns as arrays? I only have luck with looping through the database returns. The arrays are a bit nested. This is an example from a textarea input where each line represents a new item and items can be added (or removed). The looping code is so much complicated, where a simple array_diff is much more elegant.
I would love to do this:
Currently my code is much more complicated by looping through the results
I would love to do this:
PHP:
$editorItems = input[from Editor template]
$editorItemArray = implode("\n", $editorItems) // that works
$existingItems = db->fetchAll()
$existingItemsArray = get_item_id_out_of_QueryReturn($existingItemsArray) // that does not work
$existingItemsInEditor = array_intersect($editorItemArray, $existingItemsArray)
$removeItems = array_diff ($existingItemsInEditor , $existingItemsArray)
$addItems = array_diff($existingItemsInEditor , $editorItemArray)
foreach ($addItems as $item)
{
dw->write
}
foreach ($removeItems as $item)
{
dw->delete
}
Currently my code is much more complicated by looping through the results
PHP:
$allItems = db->fetchAll()
foreach ($allItems as $item)
{
$existingItems[] = $item['item_id']}
}
$editorItems = input[from Editor template]
$editorItemArray = implode("\n", $editorItems) // that works
foreach ($editorItemArray as $item)
{
if(!in_array($item, $allItems)
{
dw->write
$addedItems[] = $item['item_id']
}
}
$missingItems = array_diff( $existingItems, $addedItems )
foreach ($missingItems as $item)
{
dw->delete
}