I need to get category titles for which I am using the a model where $result
returns all the data from the categories table, what I need is only the titles, that by making a loop to $result
and a comparison with in_array()
I can obtain them.
$cat_ids = $params->get('catid'); // this is = array(2, 3, 4, 5, 7);
$categories = JModelLegacy::getInstance('Categories', 'ContentModel',array('ignore_request' => true));
$categories_items = $categories->getItems();
$category_titles = array();
foreach ($categories_items as $category) {
if (in_array($category->id, $cat_ids)) {
$category_titles[] = $category->title;
}
}
return $category_titles;
These titles can also be obtained by making a loop of $catids
and in each iteration call to the database with $db = JFactory::getDbo();
in this case if I need 5 titles I must make 5 queries to the database data.
$category_titles = array();
foreach ($cat_ids as $id) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title');
$query->from($db->quoteName('#__categories'));
$query->where($db->quoteName('id') . ' = ' . $db->quote($id));
$db->setQuery($query);
$category_titles[] = $db->loadResult();
}
return $category_titles;
My question: which is more efficient? do a query with lots of data or lots of queries with unique data?
Thanks. (I'm sorry for my English)
WHERE IN
clause.