Skip to content

Instantly share code, notes, and snippets.

@sergiois
Last active December 22, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiois/6416319 to your computer and use it in GitHub Desktop.
Save sergiois/6416319 to your computer and use it in GitHub Desktop.
Función que permite recoger las categorías de Joomla! y mostrarlas en un select
<?php
public function getCats()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('a.id, a.title, a.level');
$query->from('#__categories AS a');
$query->join('LEFT', $db->quoteName('#__categories').' AS b ON a.lft > b.lft AND a.rgt where('a.extension = "com_content"');
$query->where('a.published IN (0,1)');
$query->group('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id');
$query->order('a.lft ASC');
$db->setQuery($query);
return $db->loadObjectList();
}
?>
// HTML
<label for="categories">Seleccione categoría/s</label>
<select name="categories[]">
<?php foreach($this->cats as $cat):
$num = strlen($cat->title) + (int)$cat->level*3;
$category = str_pad(' '.$cat->title.' ', $num, "- - ", STR_PAD_LEFT);
$categories = explode(',',$this->term->category);
if(in_array($cat->id, $categories)){
echo '<option selected="selected" value="'.$cat->id.'">'.$category.'</option>';
} else{
echo '<option value="'.$cat->id.'">'.$category.'</option>';
}
endforeach; ?>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment