2

I have a Component with the following table:

+------------------+------------------+------+-----+---------------------+----------------+
| Field            | Type             | Null | Key | Default             | Extra          |
+------------------+------------------+------+-----+---------------------+----------------+
| id               | int(11) unsigned | NO   | PRI | NULL                | auto_increment |
| asset_id         | int(10) unsigned | NO   |     | 0                   |                |
| title            | varchar(255)     | NO   |     | NULL                |                |
| file             | varchar(255)     | NO   |     | NULL                |                |
| category         | int(11)          | NO   |     | NULL                |                |
| ordering         | int(11)          | NO   |     | NULL                |                |
| state            | tinyint(1)       | NO   |     | NULL                |                |
| checked_out      | int(11)          | NO   |     | NULL                |                |
| checked_out_time | datetime         | NO   |     | 0000-00-00 00:00:00 |                |
| created_by       | int(11)          | NO   |     | NULL                |                |
+------------------+------------------+------+-----+---------------------+----------------+

I am using JCategories. Here is my current model File:

<?php

/**
 * @version     1.0.0
 * @package     com_media_centre
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Dawid van der Hoven <dawid@jamfactory.co.za> - http://www.jamfactory.co.za
 */
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of Media_centre records.
 */
class MediaModelFiles extends JModelList {

    /**
     * Constructor.
     *
     * @param    array    An optional associative array of configuration settings.
     * @see        JController
     * @since    1.6
     */
    public function __construct($config = array()) {
        parent::__construct($config);
    }

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @since   1.6
     */
    protected function populateState($ordering = null, $direction = null) {

        // Initialise variables.
        $app = JFactory::getApplication();

        // List state information
        $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
        $this->setState('list.limit', $limit);

        $limitstart = JFactory::getApplication()->input->getInt('limitstart', 0);
        $this->setState('list.start', $limitstart);


        if(empty($ordering)) {
            $ordering = 'a.ordering';
        }

        // List state information.
        parent::populateState($ordering, $direction);
    }

    /**
     * Build an SQL query to load the list data.
     *
     * @return  JDatabaseQuery
     * @since   1.6
     */
    protected function getListQuery() {
        // Create a new query object.
        $db = $this->getDbo();
        $query = $db->getQuery(true);

        // Select the required fields from the table.
        $query->select(
                $this->getState(
                        'list.select', 'a.*'
                )
        );

        $query->from('`#__media_files` AS a');


    // Join over the users for the checked out user.
    $query->select('uc.name AS editor');
    $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');

        // Join over the category 'category'
        $query->select('category.title AS category_title');
        $query->join('LEFT', '#__categories AS category ON category.id = a.category');
        // Join over the created by field 'created_by'
        $query->select('created_by.name AS created_by');
        $query->join('LEFT', '#__users AS created_by ON created_by.id = a.created_by');


        // Filter by search in title
        $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('a.id = ' . (int) substr($search, 3));
            } else {
                $search = $db->Quote('%' . $db->escape($search, true) . '%');
                $query->where('( a.title LIKE '.$search.' )');
            }
        }



        //Filtering category
        $filter_category = $this->state->get("filter.category");
        if ($filter_category) {
            $query->where("a.category = '".$filter_category."'");
        }

        return $query;
    }

    public function getItems() {
        return parent::getItems();
    }

}

Okay Now the actual question:

How can I return data in a format like:

Category 1 (and the alternative layout param)
 - Item in Category 1
 - Item in Category 1
 - Item in Category 1

Category 2 (and the alternative layout param)
 - Item in Category 2
 - Item in Category 2
 - Item in Category 2

I apologize its not in the scope or if its to heavy of a question, If it is just drop a comment and ill remove, otherwise please let me know if any other information is required.

2

I do not know if this is possible using SQL, but you can get such result using PHP:

$byCategory = array();

foreach ($items as $item)
{
    if (empty($byCategory[$item->category_title]))
    {
            $byCategory[$item->category_title] = array();
    }
    $byCategory[$item->category_title][] = $item;
}

$byCategory array will have the data sorted as you have described:

Array
(
    [Category 1] => Array
        (
            [0] => stdClass Object
                (
                    Item in Category 1
                )

            [1] => stdClass Object
                (
                    Item in Category 1
                )

            [2] => stdClass Object
                (
                    Item in Category 1
                )
        )

    [Category 2] => Array
        (
            [0] => stdClass Object
                (
                    Item in Category 2
                )

            [1] => stdClass Object
                (
                    Item in Category 2
                )

            [2] => stdClass Object
                (
                    Item in Category 2
                )
        )
)
  • I think your right, multi level arrays were never made for mysql, so its best to logically split them from the array given by mysql. – Jordan Ramstad May 23 '14 at 20:42
1

If you're using PDO, you can fetch using key-pairs methods, I'm not extremely experienced with PDO + Joomla, but in pure PDO you can do something like this:

$q = $db->query("SELECT `category_title` AS name, `value` AS value FROM `wherever`;");
$r  = $q->fetchAll(PDO::FETCH_KEY_PAIR);

Then it will group them as key => value, you can also write something like "$sth->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP, 2)" to fetch them as groups.

http://php.net/manual/en/pdo.constants.php Search for "FETCH_" and you can see a set of fetch methods.

It might not answer your question entirely, but hopefully wake some ideas on how to solve your problem.

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.