我用Google搜索并搜索到各处,但我找不到关于这个问题的明确答案。
我正在尝试进行网页搜索并对结果进行分页(并按标题对其进行排序)。
请参阅原型http://i55.tinypic.com/2dlrqbs.png
我希望如果用户指定搜索条件“a”,则会显示包含“a”的所有名称。我的问题是如何在导航链接上添加字符串:?name = a。
如果我没有发回搜索条件,点击下一页将显示所有记录。
我读了很多关于这个主题的帖子,我还不知道该怎么做
控制器代码(草案)
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;
}
由于
答案 0 :(得分:0)
我设法让它发挥作用。查看Kohana PAgination Libs,它只使用函数http_build_query扩展GET参数。然后我将表单方法更改为GET。
工作代码:
<强>控制器:强>
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;
}
查看强>
[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>';
&GT;