【问题标题】:Joomla pagination problemJoomla 分页问题
【发布时间】:2011-03-25 15:34:10
【问题描述】:

我是俄罗斯人,很抱歉,我的英语不太好)

我的问题是关于 Joomla 分页。 在我们的任务 example.php 中,我们只有一个函数 display();让分页变得简单。

但我有一个真正的问题。在我的页面示例上,我有搜索表单,我需要对搜索结果进行分页。当我开始搜索某些内容时,我的搜索表单、搜索结果、页脚上的分页很好,但是当我单击 2,3..... 页面时,它们打开时没有任何结果——只有搜索表单。在我的任务 example.php 中,我有以下 4 个函数。请帮助我在所有分页页面上进行分页并获得结果。

谢谢!

class SearchTask extends Controller

{

var $_plugins       = null;



function SearchTask()
{
    $this->toolbar  = MY_TOOLBAR_SEARCH;
    $this->_plugins = new MYPlugins();
}

function display()
{
    global $Itemid,$MY_LANG, $_MY_CONFIG, $pagination, $total, $limitstart, $limit;

    $mainframe  =& JFactory::getApplication();
    $my         =& JFactory::getUser();
    myAddPageTitle( JText::_( 'SEARCH BLOG ENTRY TITLE') );


    $template   = new Template(time() . $my->usertype . $_MY_CONFIG->get('template'));

    $blogger        = JRequest::getVar('blogger','','POST','string');
    $keyword        = JRequest::getVar('keyword','','POST','string');
    $catid          = JRequest::getVar('catid','','POST','int');
    $from           = JRequest::getVar('from','','POST');
    $to         = JRequest::getVar('to','','POST');
    $agefrom            = JRequest::getVar('agefrom','','POST','int');
    $ageto          = JRequest::getVar('ageto','','POST','int');
    $onan           = JRequest::getVar('onan','','POST','int');

    $limitstart = JRequest::getVar('limitstart', '0', 'GET');
    $limit      = "2";
    // Display form for user
    $searchURL  = JRoute::_('index.php?option=com_exemple&task=search&Itemid=' . myGetItemId());

    $template->set('searchURL', $searchURL);
    $template->set('Itemid', myGetItemId());
    $results    = false;
    if((!empty($blogger) && isset($blogger)) || (!empty($keyword) && isset($keyword)) || (!empty($catid) && isset($catid)) || (!empty($from) && isset($from))  || (!empty($to) && isset($to))  || (!empty($agefrom) && isset($agefrom)) || (!empty($ageto) && isset($ageto)) || (!empty($onan) && isset($onan)))
    {
        // Post action, perform search
        $results    = $this->_search(array('blogger' => $blogger, 'keyword' => $keyword, 'catid' => $catid, 'from' => $from, 'to' => $to, 'agefrom' => $agefrom, 'ageto' => $ageto, 'onan' => $onan));
    }

    if($results>0)
    {   if ((!empty($blogger) && isset($blogger)) && (!empty($keyword) && isset($keyword)) && (!empty($catid) && isset($catid)) && (!empty($from) && isset($from)) && (!empty($to) && isset($to)) && (!empty($agefrom) && isset($agefrom)) && (!empty($ageto) && isset($ageto)) && (!empty($onan) && isset($onan)))
        {$sorry = "<div style='padding-top:20px;'>No result for <b>$keyword</b> and <b>$blogger</b></div>";}
        else {$sorry = "<div style='padding-top:20px;'>No result for <b>$blogger</b><b>$keyword</b></div>";}
    }
    else{$sorry = "";}

    echo $limitstart;
    echo $limit;
    $template->set('blogger', $blogger);
    $template->set('keyword', $keyword);
    $template->set('catid', $catid);
    $template->set('from', $from);
    $template->set('to', $to);
    $template->set('agefrom', $agefrom);
    $template->set('ageto', $ageto);
    $template->set('onan', $onan);
    $template->set('results', $results);
    $template->set('total', $total);
    $template->set('sorry', $sorry);
    $template->set('pagination', $pagination);
    $content    = $template->fetch($this->_getTemplateName('search'));

    return $content;
}

/**
 * _search
 * params: $filter (assoc array)
 **/            
function _search($filter)
{

    JRequest::checkToken() or jexit( 'Invalid Token' );
    global $_MY_CONFIG, $keyword, $pagination, $total, $limitstart, $limit;

    $db         =& JFactory::getDBO();
    $limitstart = JRequest::getVar('limitstart', '0', 'GET');
    $limit      = "2";
    $blogger    = isset( $filter['blogger'] ) ? $db->getEscaped( $filter['blogger'] ) : '';
    $keyword    = isset( $filter['keyword'] ) ? $db->getEscaped( $filter['keyword'] ) : '';
    $catid      = isset( $filter['catid'] ) ? $db->getEscaped( $filter['catid'] ) : '';
    $from       = isset( $filter['from'] ) ? $db->getEscaped( $filter['from'] ) : '';
    $to     = isset( $filter['to'] ) ? $db->getEscaped( $filter['to'] ) : '';
    $agefrom        = isset( $filter['agefrom'] ) ? $db->getEscaped( $filter['agefrom'] ) : '';
    $ageto      = isset( $filter['ageto'] ) ? $db->getEscaped( $filter['ageto'] ) : '';
    $onan       = isset( $filter['onan'] ) ? $db->getEscaped( $filter['onan'] ) : '';

    if (!empty( $filter['from']) && !preg_match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$^",$from))
    {
        JError::raiseWarning( 100, 'Some error' );
        return;
    }
    if (!empty( $filter['to']) && !preg_match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$^",$to))
    {
        JError::raiseWarning( 100, 'Some error' );
        return;
    }

    $query  = (!empty( $filter['blogger']) || !empty( $filter['keyword']) || !empty( $filter['catid']) || !empty( $filter['from']) || !empty( $filter['to']) || !empty( $filter['agefrom']) || !empty( $filter['ageto']) || !empty( $filter['onan'])) ? 'SELECT SQL_CALC_FOUND_ROWS id,title,introtext,created_by,catid,created,vozvras,anonym FROM #__content WHERE ' : '';


    if(!empty($keyword))
    {
     $query      .= " (`title` LIKE '%{$keyword}%' "
                         . "OR `introtext` LIKE '%{$keyword}%')";
    }


    if(!empty($blogger))
    {
        if(!empty($keyword))
        {
            $query  .= " AND `created_by`='" . myGetAuthorId($blogger) ."'";
        }
        else
        {
            $query  .= " `created_by`='" . myGetAuthorId($blogger) ."'";
        }
    }


    if(!empty($catid) && is_numeric($catid))
    {
        if( !empty($blogger) || !empty($keyword))
        {
        $query  .= ' AND `catid`=' .(int) $catid . ' ';
        }
        else
        {
            $query  .= ' `catid`=' .(int) $catid . ' ';
        }
    }


    if(!empty($from))
    {
        $datefrom =& JFactory::getDate( $from );
        if(!empty($blogger) || !empty($keyword) || !empty($catid))
        {
        $query  .= " AND `created`>='{$datefrom->toMySQL()}'";
        }
        else
        {
            $query  .= " `created`>='{$datefrom->toMySQL()}'";
        }
    }

    if(!empty($to))
    {

        $to .=' 23:59:59';
        $dateto =& JFactory::getDate( $to );
        if(!empty($blogger) || !empty($keyword) || !empty($catid) || !empty($from))
        {
        $query  .= " AND `created`<='{$dateto->toMySQL()}'";
        }
        else
        {
            $query  .= " `created`<='{$dateto->toMySQL()}'";
        }
    }


    if(!empty($agefrom))
    {
        if(!empty($blogger) || !empty($keyword) || !empty($catid) || !empty($from) || !empty($to))
        {
        $query  .= ' AND `vozvras`>=' .(int) $agefrom . ' ';
        }
        else
        {
        $query  .= ' `vozvras`>=' .(int) $agefrom . ' ';
        }
    }



    if(!empty($ageto))
    {
        if(!empty($blogger) || !empty($keyword) || !empty($catid) || !empty($from) || !empty($to) || !empty($agefrom))
        {
        $query  .= ' AND `vozvras`<=' .(int) $ageto . ' ';
        }
        else
        {
        $query  .= ' `vozvras`<=' .(int) $ageto . ' ';
        }
    }


    if(!empty($onan))
    {
        if(!empty($blogger) || !empty($keyword) || !empty($catid) || !empty($from) || !empty($to) || !empty($agefrom) || !empty($ageto))
        {
        $query  .= ' AND `anonym`=' .(int) $onan . ' ';
        }
        else
        {
        $query  .= ' `anonym`=' .(int) $onan . ' ';
        }
    }
    $sections   = $_MY_CONFIG->get('managedSections');
    $query  .= " AND `sectionid` IN ({$sections}) AND `state`='1' ORDER BY `created` DESC LIMIT $limitstart, $limit";
    $db->setQuery( $query );
    $results    = $db->loadObjectList();
    $db->setQuery( "SELECT FOUND_ROWS();" );
        $total      = $db->loadResult();
        jimport( 'joomla.html.pagination' );
        $pagination = new JPagination( $total , $limitstart , $limit );
        $pagination = $pagination->getPagesLinks();

    $this->_format($results);


    return $results;
}

function _format(&$rows)
{
    global $_MY_CONFIG, $keyword;

    // Load Plugins
    $this->_plugins->load();

    // Format results
    for($i =0; $i < count($rows); $i++){
        $row    =& $rows[$i];


        $row->text = SearchHelper::prepareSearchContent( $row->introtext, 200, $keyword );
        $row->text = preg_replace("/$keyword/i", "<span class='highlight'>$keyword</span>", $row->text);
        $row->user      = myGetAuthorName($row->created_by, $_MY_CONFIG->get('useFullName'));
        $row->user      = $row->user;
        $row->link      = myGetPermalinkURL($row->id);
        $row->userlink  = JRoute::_('index.php?option=example&blogger=' . myGetAuthorName($row->created_by));
        $row->jcategory     = '<a href="' . JRoute::_('index.php?option=example&task=tag&category=' . $row->catid ) . '"><b id="bold">' . myGetJoomlaCategoryName( $row->catid ) . '</b></a>';
        $avatar = 'My' . ucfirst($_MY_CONFIG->get('avatar')) . 'Avatar';
        $avatar = new $avatar($row->created_by);

        $row->avatar    = $avatar->get();

        $date           =& JFactory::getDate( $row->created );
        $date->setOffSet( $_MY_CONFIG->get('dateFormat') );
        $row->date      = $date->toFormat();
    }
}

}

【问题讨论】:

    标签: joomla pagination


    【解决方案1】:

    女贞,

    你有一个愚蠢的问题。您最初的查询生成了有限数量的记录的选择。

    SELECT ... FROM ... WHERE ... LIMIT $limitstart, $limit
    

    然后你使用"SELECT FOUND_ROWS();",这将是$limit,所以实际上你只有一页用于分页。

    你的查询需要是这样的:

    1. SELECT ... FROM ... WHERE ... LIMIT $limitstart, $limit
    2. `SELECT COUNT(*) FROM ... WHERE ...

    Uda4i!

    【讨论】:

      【解决方案2】:

      joomla分页的简单解决方法,看看

      ...templates/protostar/html/pagination.php 并复制粘贴到您的

      模板html文件夹...templates/mytemplate/html/pagination.php,

      用你喜欢的样式编辑它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-06
        • 1970-01-01
        • 2013-03-11
        • 2014-03-13
        • 2013-06-30
        • 2013-09-26
        • 2011-06-16
        • 2012-10-02
        相关资源
        最近更新 更多