2011-11-11 30 views
0

我正在尋找一種方法來返回查詢的第一個結果中的精確列的值。使用代碼點火器在查詢結果中獲取精確的列

查詢結果總是包含包含以下幾列1行:

ID
PW
RANK

我想只是爲了推入拿到等級值一個會話變量。

我試圖搞亂:

$row-> $query->row(); 

但後來我不知道怎麼去排名列僅值。理想情況下,我希望獲得該值,然後將其返回以允許我的控制器將其設置爲會話變量(在模型中執行此操作會更容易,但會打破MVC模式,對不對?)

我該如何才能做這個 ?

謝謝

回答

0

讓控制器調用返回該值的模型中的函數。假設該型號具有功能名稱get_rank()

public function get_rank($id) 
{ 
    // assumes your ID is an int 
    $id = (int)$id; 

    // avoid wasting a query 
    if ($id < 1) return FALSE; 

    // build query 
    $this->db->select('rank'); 
    $this->db->from('YOUR_TABLE'); 
    $this->db->where('id', $id); 
    // maybe some sorting/limiting here if you need it 

    // get result 
    $rs = $this->db->get(); 

    if ($rs->num_rows() > 0) 
    { 
    // just get the first row 
    $row = $rs->row(); 
    return $row->rank; 
    } 

    return FALSE; 
}