Now, we will make up list and give more power. We will add function like paginating, search box, and ordering.
Replace showHello() in "admin.hello.html.php" file with following code:
function showHello(&$rows, &$pageNav, &$lists){
JHTML::_('behavior.tooltip');
?>
<form action="index.php?
option=com_hello" method="post" name="adminForm">
<table>
<tr>
<td align="left" width="100%">
<?php echo JText::_( 'Filter' ); ?>:
<input type="text" name="search" id="search"
value="<?php echo $lists['search'];?>"
class="text_area" onchange="document.adminForm.submit();" />
<button onclick="this.form.submit();">
<?php echo JText::_( 'Go' ); ?></button>
<button onclick="document.getElementById('search').value='';this.form.submit();">
<?php echo JText::_( 'Reset' ); ?></button>
</td>
</tr>
</table>
<table class="adminlist">
<thead>
<tr>
<th width="20">
<input type="checkbox" name="toggle"
value="" onclick="checkAll(<?php echo count($rows)?>)">
</th>
<th width="50" class="title">
<?php echo JHTML::_('grid.sort', 'ID', 'id', @$lists['order_Dir'], @$lists['order'] ); ?></td>
<th><?php echo JHTML::_('grid.sort',
'Message', 'message',
@$lists['order_Dir'], @$lists['order'] ); ?></th>
<th nowrap="nowrap"><?php echo JHTML::_('grid.sort', 'Published', 'published',
@$lists['order_Dir'], @$lists['order'] ); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="11">
<?php echo $pageNav->getListFooter(); ?>
</td>
</tr>
</tfoot>
<?php
$k = 0;
for($i=0, $n=count($rows); $i < $n ; $i++)
{
$row = &$rows[$i];
$checked = JHTML::_('grid.id', $i, $row->id);
$published = JHTML::_('grid.published', $row, $i);
?>
<tr class="<?php echo "row$k"; ?>">
<td><?php echo $checked?></td>
<td><?php echo $row->id?></td>
<td><?php echo $row->message?></td>
<td><?php echo $published?></td>
</tr>
<?
$k = 1 - $k;
}
?>
</table>
<input type="hidden" name="option" value="com_hello">
<input type="hidden" name="task" value="">
<input type="hidden" name="boxchecked" value="0">
<input type="hidden" name="filter_order"
value="<?php echo $lists['order']; ?>" />
<input type="hidden" name="filter_order_Dir" value="" />
</form>
<?php
}
Then, replace showHello() in "admin.hello.php" file with following code:
function showHello(){
global $mainframe;
$db =& JFactory::getDBO();
$filter_order= $mainframe->
getUserStateFromRequest( $option.'filter_order',
'filter_order','id','cmd' );
$filter_order_Dir= $mainframe->
getUserStateFromRequest( $option.'filter_order_Dir',
'filter_order_Dir','','word' );
$filter_state = $mainframe->
getUserStateFromRequest( $option.'filter_state', 'filter_state', '','word' );
$search = $mainframe->
getUserStateFromRequest( $option.'search',
'search','','string' );
$search = JString::strtolower( $search );
$limit= $mainframe->
getUserStateFromRequest('global.list.limit',
'limit', $mainframe->getCfg('list_limit'), 'int');
$limitstart= $mainframe->
getUserStateFromRequest($option.'.limitstart',
'limitstart', 0, 'int');
$where = array();
if ( $search ) {
$where[] = 'message LIKE "%'.$db->getEscaped($search).'%"';
}
$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
if ($filter_order == 'id'){
$orderby = ' ORDER BY id';
} else {
$orderby = ' ORDER BY '.
$filter_order .' '. $filter_order_Dir .', id';
}
// get the total number of records
$query = 'SELECT COUNT(*)'
. ' FROM #__hello'
. $where
;
$db->setQuery( $query );
$total = $db->loadResult();
jimport('joomla.html.pagination');
$pageNav = new JPagination( $total, $limitstart, $limit );
$query = "SELECT * FROM #__hello". $where. $orderby;
$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
$rows = $db->loadObjectList();
if($db->getErrorNum()){
echo $db->stderr();
return false;
}
// table ordering
$lists['order_Dir'] = $filter_order_Dir;
$lists['order'] = $filter_order;
// search filter
$lists['search']= $search;
// display function
HTML_contact::showHello(&$rows, &$pageNav, &$lists);
}