2014-09-29 21 views
0

我正在使用GroceryCrud保存数据。用户注册来自网站本身。当选择他们的性别时,我为男性节省1,为女性节省2。数据库字段是tinyint。所以问题是,当管理员从后台查看他们的数据时,很明显1或2会出现在Sex字段中。如何根据价值将其变为男性,女性?GroceryCrud + CodeIgniter更改字段内的值

enter image description here

回答

2

可以使用callback_column这一点。

在你的情况,你可以这样做:

public function webpages() 
{ 
    $c = new grocery_CRUD(); 

    $c->set_table('users'); 
    $c->columns('firstname','lastname','sex','age'); 

    $c->callback_column('sex',array($this,'_callback_sex')); 

    $output = $c->render(); 
    $this->_view_output($output); 
} 

public function _callback_sex($value, $row) 
{ 
    if ($value == '2') { 
     return 'male' 
    } elseif ($value == '1') { 
     return 'female'; 
    } else { 
     return "-"; 
    } 
} 
+0

谢谢你,它的工作原理。但只有在看到电网时。它如何实现编辑? – Gereltod 2014-10-06 08:51:04

+0

您可以使用field_type方法。更具体地说,您可以使用下拉列表http://www.grocerycrud.com/documentation/options_functions/field_type#dropdown-field – 2014-10-13 12:29:58