How to implement a search with pagination with Kohana 2.3.x
i googled and searched everywhere but i could not find a clear answer on this problem.
I am trying to do a web page search and paginate the result (and sort them by headers).
Please see prototype http://i55.tinypic.com/2dlrqbs.png
I would like that if a user specify as search criteria 'a', all names that contains 'a' are shown. My problem is how can i put on the navigation links the string: ?name=a.
If i don't send back the search criteria, clicking next page will show all the records.
I read a开发者_JAVA百科 lot of post on the subject, and i do not yet understand how to do it
Controller Code (draft)
function listall( )
{
$limit = 2 ;
$orderby = 'u.id';
$direction = 'asc';
$name = '';
if ( $_POST )
{
$name = $this->input->post('name');
}
if ( $_GET )
{
$name = $this->input->post('name');
if ( $this->input->get('orderby') )
list($orderby, $direction) = explode(':', $this->input->get('orderby'));
}
$view = new view( 'character/listall');
$db = Database::instance();
$sql = "select c.id id, c.lastactiontime, c.name character_name, k.name kingdom_name, k.image kingdom_image, from_unixtime( u.last_login, '%d-%m-%y') last_login, u.nationality
from characters c, kingdoms k, users u
where 1=1 and
c.kingdom_id = k.id and
c.user_id = u.id
" ;
if ( $name != '' )
$sql .= "and c.name like '%" . $name . "%'" ;
$characters = $db->query( $sql );
//$characters = ORM::factory( "character" )->orderby( $orderby, $direction )->find_all();
$this->pagination = new Pagination(array(
'base_url'=>'character/listall',
'uri_segment'=>'listall',
'style'=>'digg',
'query_string' => 'page',
'total_items'=>$characters->count(),
'items_per_page'=>$limit));
//$characters = ORM::factory( "character" )->orderby( $orderby, $direction )->find_all($limit, $this->pagination->sql_offset);
$sql .= " order by $orderby $direction ";
$sql .= " limit $limit offset " . $this->pagination->sql_offset ;
kohana::log('debug', $sql );
$characters = $db->query( $sql );
$playersinfo = Character_Model::getplayersinfo();
$view->playersinfo = $playersinfo;
$view->pagination = $this->pagination;
$view->characters = $characters;
$this->template->content = $view;
}
Thanks
I managed to make it work. Looking at Kohana PAgination Libs, it expands only the GET parameters with the function http_build_query. I changed then the form method to GET.
Working code:
Controller:
function listall( )
{
$limit = 25 ;
$orderby = 'u.id';
$direction = 'asc';
$name = '';
$query_string= '';
if ( $_GET )
{
$name = $this->input->get('name');
$online = $this->input->get('online');
if ( $this->input->get('orderby') )
list($orderby, $direction) = explode(':', $this->input->get('orderby'));
}
kohana::log('debug', kohana::debug( $_GET ) );
$view = new view( 'character/listall');
$db = Database::instance();
$sql = "select c.id id, c.lastactiontime, c.name character_name, k.name kingdom_name,
k.image kingdom_image, from_unixtime( u.last_login, '%d-%m-%y') last_login, u.nationality
from characters c, kingdoms k, users u
where 1=1 and
c.kingdom_id = k.id and
c.user_id = u.id
" ;
$criteria = kohana::lang('global.criteria' );
if ( $name != '' )
{
$sql .= " and c.name like '%" . $name . "%' " ;
$criteria .= kohana::lang('global.name') . ' ' . kohana::lang('global.contains') . ' ' . $name . ' ' ;
}
if ( $online )
{
$sql .= " and c.lastactiontime > (unix_timestamp() - 15 * 60 ) " ;
$criteria .= kohana::lang('global.online') . ' = true' ;
}
if ( !$online and $name == '' )
$criteria .= kohana::lang('global.allrecords' ) ;
$characters = $db->query( $sql );
//$characters = ORM::factory( "character" )->orderby( $orderby, $direction )->find_all();
$this->pagination = new Pagination(array(
'base_url'=>'character/listall',
'uri_segment'=>'listall',
'style'=>'digg',
'query_string' => 'page',
'total_items'=>$characters->count(),
'items_per_page'=>$limit));
//$characters = ORM::factory( "character" )->orderby( $orderby, $direction )->find_all($limit, $this->pagination->sql_offset);
$sql .= " order by $orderby $direction ";
$sql .= " limit $limit offset " . $this->pagination->sql_offset ;
kohana::log('debug', $sql );
$characters = $db->query( $sql );
$playersinfo = Character_Model::getplayersinfo();
$view->playersinfo = $playersinfo;
$view->pagination = $this->pagination;
$view->characters = $characters;
$view->criteria = $criteria ;
$this->template->content = $view;
}
View
[cut]
<?php
echo form::open('/character/listall', array('method' => 'get' ) );
echo form::label( kohana::lang('global.name')) . ' ' . form::input( array( 'id' => 'name', 'name' => 'name', 'style' => 'width:200px') );
echo ' ';
echo form::label( kohana::lang('global.online')) . ' ' . form::checkbox('online', true );
echo "<div style='float:right'>";
echo form::submit( array( 'id' => 'submit', 'class' => 'submit', 'value' => kohana::lang('global.search') ) );
echo form::submit( array( 'id' => 'reset', 'class' => 'submit', 'value' => kohana::lang ('global.reset') ) );
echo "</div>";
echo form::close();
echo '<br/>';
echo '<b>' . $criteria .'</b>';
?>
精彩评论