I'm creating a Joomla 3x SP Pagebuilder3 addon, but I'm having a bit of challenge getting a database results as an option in a select type.

This is what I've done so far.

class MyCustomTypes {

public static function getBannerCats(){
    $db       = JFactory::getDbo();

    $query = $db->getQuery(true)
        ->select(array('id', 'title', 'alias'))
        ->where('extension = ' . $db->quote('com_banners'))
        ->where('parent_id > 0')
        ->where('published > -1');
    $items = $db->loadObjectList();

     $options = array();
     if ($items)
      foreach($items as $item)
        $options[] = $item->title;

    // $options = array_merge(parent::getOptions(), $options);

     return $options;



I want to options to show here

            'desc'=>JText::_('Category Source'),
            'values'=>array(MyCustomTypes::getBannerCats()),// Result will show here like so 'result_id'=>'result_title'

I will be glad if someone can help me. Thank you.

  • We don't know the earliest point of "breakage" for your issue. We don't know if you have any qualifying values in your database -- because we can't see your database data. Have you printed output your result set to ensure that you are receiving the correct expected values? How do you know that it isn't working properly? Are any Notices, Warnings, Errors being generated? What does $options contain?
    – mickmackusa
    Nov 23 '18 at 3:08

After updating your question...

If your $options array must be associative, then build it that way in the loop.

$options[$item->id] = $item->title;

Your values element looks to be too complex.

'values'=>array(MyCustomTypes::getBannerCats())  // multi-dim array

this will store your returned 1-dimensional array inside of an array. Instead, I suppose you should be using:

'values' => MyCustomTypes::getBannerCats()  // 1-dimensional array

Before the question update...

It looks like your select() method is receiving 3 parameters, but you intend to pass 1 string containing 3 values or an array with 3 elements.

I recommend this:

->select(['id', 'title', 'alias'])


->select('id, title, alias')


->select($db->qn(['id', 'title', 'alias']))

To explain what Joomla is doing... The select() method only accepts one parameter/argument, so only id is used and title and alias are simply ignored by the method. If you call $query->dump() you would see something like the following (with your table prefix in place of #_)

FROM #__categories
WHERE extension = 'com_banners' AND parent_id > 0 AND published > -1

Ergo, $item->title; will never have any data in it.

Before posting a question when thinks aren't working, please check for errors and dump your query to make sure that there are no simple typos.

p.s. if you are only going to use title then only write one value in the SELECT, and use a simpler result set retrieving method to process a single column of data.

  • thanks for your reply. I have edited the question. the issue actually is not the query but how to handle the results to get values into the select options Nov 23 '18 at 2:42
  • the result is supposed to be an array with items in this format 'result_id'=>'result_title' thank you Nov 24 '18 at 10:36
  • @David I've updated to show how to build the associative array in getBannerCats().
    – mickmackusa
    Nov 24 '18 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.