簡體   English   中英

每個類別的Joomla 2.5限制查詢結果

[英]Joomla 2.5 limit query results from each category

我正在Joomla 2.5中使用Module minifrontpage。 此處的網址: http : //www.bigideaadv.com/wright_flood_OLD/

我想做的是檢索4篇最新文章。 2個來自一個類別,2個來自另一類別。 順序必須是1類商品,然后是2類商品(我已經設置好了,但是無法獲得每個類別的限制。)

任何幫助,將不勝感激。 謝謝。

我的數據庫功能代碼如下:

    // 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();

這也是我需要的。 限制每個類別的文章。 想知道是否可以設置一個簡單的過濾器來實現這一目標。 想要避免模型黑客。

謝謝!

編輯由於缺乏共鳴,我以一種非常丑陋的方式嘗試了此操作,這解決了部分問題。 為簡化起見,我為每個類別定了一篇文章。 首先,我更改了models \\ category.php,並在第224行附近為模型添加了一個名為filter.subcategories_article_limit的狀態(檢查博客布局) $model->setState('filter.subcategories_article_limit', 1);

基本上這是為過濾器設置一個標志

然后在models \\ articles.php中,在第322行附近添加了一個where子句$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);
}

我知道這是一個核心技巧,它不會處理很多事情,例如用戶權限,文章狀態等。我相信可以對此進行改進。

但我確實希望一些專家會提供更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM