Hello World! I'm russian, so sorry, i'm not so good in english )
My question is about Joomla pagination. When in our task exemple.php we are only one function display(); make pagination - easy.
But i have a real problem. On my page exemple i have search form and i need to paginate search result. When i start to search something i see good my search form, search result, good pagination on the footer of the page but when i click 2,3..... page they open without any results - only search form. In my task exemple.php i have 4 functions below. Please help me make pagination work around all pagination page with result.
Thank you!
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();
}
}
}