Joomla DB Queries

16.04.19

Consider two types of queries into the Joomla database by its own means. The code was written for CMS version 3.0 or higher. Perhaps in the versions below it will also work, but it was not checked.

For clarity, the examples will be wrapped in a function. The first is when we need to get multiple values.

function getAllCategoryesId(){
   $db = JFactory::getDBO();
   $query = "SELECT `category_id` FROM `#__jshopping_categories` WHERE `category_publish` = 1";
   $db->setQuery($query);
   $categoriesId = $db->loadObjectList();
   $ids = array();
   foreach ($categoriesId as $catId){
       array_push($ids, $catId->category_id);
   }
   return $ids;
}

In this case, we received a list of identifiers of published categories as an array. But there is a situation when we need to get one specific value. For this fit a slightly different design. Also we will wrap everything in a function.

function getDescriptionCategory(){
 
    $db = JFactory::getDBO();
    $query = "SELECT `description` FROM #__jshopping_categories WHERE category_id = 1";
    $db->setQuery($query);
    $description = $db->loadResult();
    return $description;
     
}

So we get a description of one particular category. It is clear that in real life our functions may contain arguments.

blog comments powered by Disqus