php - Joomla 2.5 限制每个类别的查询结果

标签 php mysql joomla2.5

我在 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();

最佳答案

这也是我需要的东西。限制每个类别的文章。想知道我们是否可以设置一个简单的过滤器来实现这一点。 想避免模型破解。

谢谢!

编辑 由于缺乏反响,我自己以一种非常丑陋的方式尝试了这个,它解决了我的部分问题。为了简化事情,我选择了每个类别 1 篇文章。 首先,我更改了 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);<br/> }

我知道这是一个核心 hack,它不会处理很多事情,例如用户权限、文章状态等。我相信这可以改进。

但我确实希望一些专家能得到更好的解决方案。

关于php - Joomla 2.5 限制每个类别的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519823/

相关文章:

php - 通过数据库插入处理 PayPal IPN

php - Joomla 2.5 隐藏 div

php - Joomla 2.5 - 为 iPad 添加自定义 CSS

php - MySQL访问=访问+1,凝视行为

php - 如何从两个 MySQL 表中显示用户

php - 使用哪个,concat,concat_ws?在 mysql 中

php - 将自定义按钮添加到 Joomla 的文章编辑器 (TinyMCE)

php - 无法使用准备好的语句保存到数据库中

php - Active Record 不唯一表问题

c++ - 如何使用 XDevAPI 将行结果存储到字符串对象