2

我目前正在尝试修改一个几乎可以完美满足我的要求但缺少分页的模块。我似乎无法掌握应该如何为其添加分页;大多数 JPagination 示例假设我可以访问查询,但在这个模块(使用 JFactory、JmoduleHelper 和 JModel)中,我看不到查询以添加限制等。

mod_otmininews.php

//No direct access!
defined('_JEXEC') or die;

// Include the syndicate functions only once
require_once dirname(__FILE__).DS.'helper.php';

$doc = &JFactory::getDocument();
$doc->addStyleSheet(JURI::base().'/modules/mod_otmininews/css/layout.css');

$list = modOtMiniNewsHelper::getList($params);
$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'));

require JModuleHelper::getLayoutPath('mod_otmininews', $params->get('layout', 'default'));

助手.php

//No direct access!
defined('_JEXEC') or die;

require_once JPATH_SITE.'/components/com_content/helpers/route.php';

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

JModel::addIncludePath(JPATH_SITE.'/components/com_content/models');

abstract class modOtMiniNewsHelper
{
public static function getList(&$params)
{
    // Get the dbo
    $db = JFactory::getDbo();

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

    // Set the filters based on the module params
    $model->setState('list.start', 0);
    $model->setState('list.limit', (int) $params->get('count', 3));
    $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 '1':
            $model->setState('filter.featured', 'only');
            break;
        case '0':
            $model->setState('filter.featured', 'hide');
            break;
        default:
            $model->setState('filter.featured', 'show');
            break;
    }

    // Set ordering
    $order_map = array(
        'm_dsc' => 'a.modified DESC, a.created',
        'mc_dsc' => 'CASE WHEN (a.modified = '.$db->quote($db->getNullDate()).') THEN a.created ELSE a.modified END',
        'c_dsc' => 'a.created',
        'p_dsc' => 'a.publish_up',
        'h_dsc' =>  'a.hits',
    );
    $ordering = JArrayHelper::getValue($order_map, $params->get('ordering'), 'a.publish_up');
    $dir = 'DESC';

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

    $items = $model->getItems();

    foreach ($items as &$item) {
        $item->slug = $item->id.':'.$item->alias;
        $item->catslug = $item->catid.':'.$item->category_alias;

        if ($access || in_array($item->access, $authorised))
        {
            // We know that user has the privilege to view the article
            $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
        }
        else {
            $item->link = JRoute::_('index.php?option=com_user&view=login');
        }
        //$item->title = htmlspecialchars( $item->title );
        $item->content= strip_tags(preg_replace('/<img([^>]+)>/i',"",$item->introtext));
        $item->content = substr($item->content, 0, $params->get('introtext_limit'));

        preg_match_all('/img.+src="([^"]+)"/i', $item->introtext, $matches);
        if(empty($matches[1][0])){
            $item->images="";
        }else{
            $item->images= $matches [1] [0];
        }

        //Show thumbnails
        if($params->get('showthumbnails')==1){
            if($item->images == ""){
                $item->thumbnail = '<img src="'.$params->get('directory_thumbdefault').'" width="'.$params->get('thumbwidth').'" height="'.$params->get('thumbheight').'" alt="'.$item->title.'" />';
            }else{
                $item->thumbnail = '<img src="' .$item->images.'" width="'.$params->get('thumbwidth').'" height="'.$params->get('thumbheight').'" alt="'.$item->title.'" />'  ;    //show images
            }
        }
        $item->created_date = $item->created; 
    }
    return $items;
}
}

所有这些代码都创建了一个由

默认的.php

    defined('_JEXEC') or die;
?>

<div class="ot_news">
    <div class="ot_news_i">


        <?php 
        $count = 0;
        foreach ($list as $item) :  ?>
        <?php $count++; ?>
        <div class="ot_items <?php echo ($params->get('count') == $count)?'last-item':''; ?>">
            <!-- Show thumbnails -->
            <?php if($params->get('showthumbnails') == 1){?>
                <div class="ot_thumbs"  style="width:<?php echo $params->get('thumbwidth')?>px; height:<?php echo $params->get('thumbheight')?>px;">
                     <?php if($params->get('enablelinkthumb') == 1) {?>
                            <a href="<?php echo $item->link; ?>"><?php echo $item->thumbnail ;?></a>
                    <?php } else { ?>
                            <?php echo $item->thumbnail?>
                     <?php }?>
                </div>
            <?php } else { ?>
                <?php echo ''; ?>
            <?php } ?>
            <!-- End -->
             <!-- Show Titles -->
            <div class="ot_articles">
                <?php if($params->get('showtitle') == 1) { ?>
                    <div class="ot_title">
                     <?php if($params->get('enablelinktitle') == 1) { ?>
                         <a href="<?php echo $item->link; ?>" class="title"><?php echo $item->title; ?></a>
                    <?php }else{?>
                        <span class="title"><?php echo $item->title; ?></span>
                   <?php }?>
                   </div>
               <?php } ?>
                <!-- Show Created Date -->
               <?php
                   if ($params->get('show_date') == 1) { 
                    echo '<p class="createddate"><span class="ot_date">'; 
                    $date = $item->created_date;
                    echo JHTML::_('date', $date, $params->get( 'date_format' ));
                    echo '</span></p>'; 
                }
               ?>
               <!-- Show Content -->
                <div class="ot_content"><?php echo $item->content; ?></div>
                <!-- Show Readmore -->
                 <?php if($params->get('readmore') == 1) {?>
                <div class="ot_readmore"><a href="<?php echo $item->link; ?>" class="ot_readm"><?php echo JText::_('READMORE') ?></a></div>
                <?php }else {?>
                    <?php echo ''; ?>
                <?php } ?>
            </div>
        </div>
        <div class="spaces"></div>
        <?php endforeach; ?>


    </div>
</div>
<div class="ot-mini-news"><?php echo JText::_('ABOUT_OT_MINI_NEWS'); ?></div>

仅使用 for each 来显示带有照片链接和描述的项目。

所以你可以看到我真的不知道在哪里添加分页代码,我怀疑他们甚至使用其中的一部分进行显示(我在 helper.php 文件中看到了一个 getlist 函数。

任何帮助将非常感激。

4

1 回答 1

0

http://docs.joomla.org/Using_JPagination_in_your_component这是一个非常有用的文档——即使这是一个模块并且维基页面正在使用它作为一个组件。

$db =& JFactory::getDBO();
$lim    = $mainframe->getUserStateFromRequest("$option.limit", 'limit', 14, 'int'); //I guess getUserStateFromRequest is for session or different reasons
$lim0   = JRequest::getVar('limitstart', 0, '', 'int');
$db->setQuery('SELECT SQL_CALC_FOUND_ROWS x, y, z FROM jos_content WHERE x',$lim0, $lim);
$rL=&$db->loadAssocList();
if (empty($rL)) {$jAp->enqueueMessage($db->getErrorMsg(),'error'); return;}     
else {
////Here the beauty starts
$db->setQuery('SELECT FOUND_ROWS();');  //no reloading the query! Just asking for total without limit
jimport('joomla.html.pagination');
$pageNav = new JPagination( $db->loadResult(), $lim0, $lim );
foreach($rL as $r) {
//your display code here
}
echo $pageNav->getListFooter(  ); //Displays a nice footer

您可以在他们的代码中看到,虽然他们确实使用了 db 结果,但分页并不“依赖”查询。只有来自它的结果。因此,$db->loadResult()例如,您可以使用 php count forumla 查看模块生成的数组中有多少行。在您的情况下,计算$items. foreach 命令仍然是你所拥有的。与foreach($rL as $r)刚才对应的只是现有的foreach($items as $item)。因此,您没有如您所见的数据库查询这一事实 - 实际上不应该成为问题!

所以你想要的代码将是这样的:

global $option; //If Joomla 1.5
global $mainframe; //If Joomla 2.5
$option = JRequest::getCmd('option') //If Joomla 2.5
$mainframe = JFactory::getApplication(); //If Joomla 2.5
$lim    = $mainframe->getUserStateFromRequest("$option.limit", 'limit', 14, 'int'); //I guess getUserStateFromRequest is for session or different reasons
$lim0   = JRequest::getVar('limitstart', 0, '', 'int');
if (empty($items)) {
    $app->enqueueMessage($db->getErrorMsg(),'error');
    return;
} else {
    jimport('joomla.html.pagination');
    $pageNav = new JPagination( count($list), $lim0, $lim );
    foreach($list as $item) {
     //wrap in your code here that you had in the foreach already
}
echo $pageNav->getListFooter(  ); //Displays a nice footer

根据您使用的是 Joomla 1.5 还是 2.5,请确保删除前两行之一,但这应该可以。

于 2012-10-04T09:19:11.133 回答