I'm trying to return an array or comma-separated list of related item IDs (as determined by the Item Relations plugin) but the following is giving me only the first related item id. What am I missing?
function related_items_query($item_id=null){
if ($item_id){
$db = get_db();
$sql='SELECT object_item_id FROM omeka_item_relations_relations WHERE subject_item_id LIKE '.$item_id;
$result = $db->query($sql);
if($count=$result->rowCount()){
$ids=$result->fetch();
return implode(', ', $ids);
}
}
}
fetch()
only fetches one row.
You probably wanted to use fetchAll()
. Additionally, since it looks like you just want a single value from each row, you want to use the Zend_Db::FETCH_COLUMN
mode.
Just caught the fetchAll()
part. I'll look into FETCH_COLUMN
. Thanks!
function related_items_query($item_id=null){
if ($item_id){
$db = get_db();
$sql='SELECT object_item_id FROM omeka_item_relations_relations WHERE subject_item_id LIKE '.$item_id;
$result = $db->query($sql);
if($count=$result->rowCount()){
$ids=$result->fetchAll();
$relations=array();
for($i = 0; $i < $count; $i++){
$relations[]=$ids[$i]['object_item_id'];
}
return implode(', ',$relations);
}
}
}