2010-12-16 62 views
0

我到處搜索和搜索,但我無法找到明確的答案在這個問題上。如何實施與Kohana 2.3.x分頁搜索

我想做一個網頁搜索和分頁結果(並按標題排序)。

請參閱原型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

你的代碼應工作? AFAIR,分頁會自動添加當前查詢字符串。 – biakaveron 2010-12-16 13:41:19

回答

0

我設法使它工作。查看Kohana分頁庫,它只擴展GET參數和函數http_build_query。然後我改變了表單方法到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')) . '&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>'; 

>