简体   繁体   中英

Joomla 2.5 limit query results from each category

I'm using the Module minifrontpage within Joomla 2.5. URL HERE: http://www.bigideaadv.com/wright_flood_OLD/

What I'm trying to do is retrieve the 4 latest articles. 2 from one category and 2 from another. And the order needs to be category 1 articles, then category 2 articles (which I have set already just can't get the limit on each category.)

Any help would be appreciated. Thanks.

My database function code is below:

    // Get the dbo
    $database = JFactory::getDbo();

    //Get the config
    $config =& JFactory::getConfig();
    $user =& JFactory::getUser();
    $tzoffset = $config->getValue('config.offset');

    // Get an instance of the generic articles model
    $model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

    // Set application parameters in model
    $app = JFactory::getApplication();
    $appParams = $app->getParams();
    $model->setState('params', $appParams);

    // Get Module Parameters
    $number_of_article = (int) $params->get('number_of_article', 5);
    $order          = $params->get( 'order_by', 1);
    $order_type     = $params->get( 'order_type', 'asc');
    $period         = intval( $params->get( 'period', 366 ) );

    echo "ORDER: ".$order."<br />";
    //echo "ORDER BY: ".$order_by;

    //$query2 = "SELECT * FROM ()"

    // Set the filters based on the module params
    $model->setState('list.start', (int) $params->get('number_of_skip', 0));
    $model->setState('list.limit', (int) $params->get('number_of_article', 5));
    $model->setState('filter.published', 1);

    // Access filter
    $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
    $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
    $model->setState('filter.access', $access);

    // Category filter
    $model->setState('filter.category_id', $params->get('catid', array()));

    // User filter
    $userId = JFactory::getUser()->get('id');
    switch ($params->get('user_id'))
    {
        case 'by_me':
            $model->setState('filter.author_id', (int) $userId);
            break;
        case 'not_me':
            $model->setState('filter.author_id', $userId);
            $model->setState('filter.author_id.include', false);
            break;

        case '0':
            break;

        default:
            $model->setState('filter.author_id', (int) $params->get('user_id'));
            break;
    }

    // Filter by language
    $model->setState('filter.language',$app->getLanguageFilter());

    //  Featured switch
    switch ($params->get('show_featured'))
    {
        case '0':
            $model->setState('filter.featured', 'hide');
            break;
        case '1':
        default:
            $model->setState('filter.featured', 'show');
            break;
        case '2':
            $model->setState('filter.featured', 'only');
            break;
    }

    // Set ordering
    $order_map = array(
        '0' => 'a.created',
        '1' => 'a.hits',
        '2' => 'a.ordering',
        '3' => 'RAND()',
    );

    $ordering = "catid, ";
    $ordering .= JArrayHelper::getValue($order_map, $params->get('order_by'), 'a.publish_up');
    //$ordering = JArrayHelper::getValue($order_map, $params->get('order_by'), 'a.publish_up');

    echo $ordering;     

    //help ordering system for DESC or ASC
    switch ($params->get( 'order_type' ))
    {
        case 1:
            $dir = 'DESC';
            break;
        case 0:
        default:
            $dir = 'ASC';
            break;
    }

    $model->setState('list.ordering', $ordering);
    $model->setState('list.direction', $dir);

    //Filter and Set Period (days)
    $model->setState('filter.date_filtering', 'relative');
    $model->setState('filter.relative_date', $period);

    $items = $model->getItems();

This is something I need as well. Limit articles per category. Was wondering if there is a simple filter we can set to achieve this. Would like to avoid a model hack.

Thanks!

EDIT For lack of resonse, I attempted this myself in quite an ugly way, and it solved part of my problem. To simplify things, I settled for 1 article per category. First I changed models\\category.php and added a state to the model called filter.subcategories_article_limit in around line 224 (with a check for the blog layout) $model->setState('filter.subcategories_article_limit', 1);

Basically this is setting a flag for the filter

Then in models\\articles.php I added a where clause in around line 322 $subcategories_article_limit = (int) $this->getState('filter.subcategories_article_limit', 0); if(!empty($subcategories_article_limit)){ $whereClause = '(a.id IN (SELECT a1.id FROM wiz3j_content AS a1 WHERE a1.catid=a.catid AND a1.publish_up=(SELECT MAX(a2.publish_up) FROM wiz3j_content AS a2 WHERE a1.catid=a2.catid) ))'; $query->where($whereClause);
}
$subcategories_article_limit = (int) $this->getState('filter.subcategories_article_limit', 0); if(!empty($subcategories_article_limit)){ $whereClause = '(a.id IN (SELECT a1.id FROM wiz3j_content AS a1 WHERE a1.catid=a.catid AND a1.publish_up=(SELECT MAX(a2.publish_up) FROM wiz3j_content AS a2 WHERE a1.catid=a2.catid) ))'; $query->where($whereClause);
}

I know it is a core hack, and it dosen't take care of a lot of things like user permissions, article status etc. I am sure this can be improved on.

But I do hope some guru will get a better fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM