Last active
December 22, 2015 04:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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