如何使用Kohana 2.3.x分页实现搜索

时间:2021-05-16 14:30:40

i googled and searched everywhere but i could not find a clear answer on this problem.

我用Google搜索并搜索到各处,但我找不到这个问题的明确答案。

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

请参阅原型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.

我希望如果用户指定搜索条件'a',则显示包含'a'的所有名称。我的问题是如何在导航链接上添加字符串:?name = a。

If i don't send back the search criteria, clicking next page will show all the records.

如果我没有发回搜索条件,点击下一页将显示所有记录。

I read a 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

谢谢

1 个解决方案

#1


0  

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.

我设法让它发挥作用。查看Kohana PAgination Libs,它只使用函数http_build_query扩展GET参数。然后我将表单方法更改为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')) . '&nbsp;' . form::input( array( 'id' => 'name', 'name' => 'name', 'style' => 'width:200px') );
echo '&nbsp;&nbsp;';
echo form::label( kohana::lang('global.online')) . '&nbsp;' . 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>';

?>

?>

#1


0  

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.

我设法让它发挥作用。查看Kohana PAgination Libs,它只使用函数http_build_query扩展GET参数。然后我将表单方法更改为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')) . '&nbsp;' . form::input( array( 'id' => 'name', 'name' => 'name', 'style' => 'width:200px') );
echo '&nbsp;&nbsp;';
echo form::label( kohana::lang('global.online')) . '&nbsp;' . 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>';

?>

?>