我希望能够在应用程序的任何位置显示表中的属性,并让用户对其进行编辑。这意味着数据将来自数据库中的一行。每列都有一个默认值,如果他们愿意,可以由用户更改。
表格列可能如下所示:
| id | admin_id |从业者|从业者|学生|学生|等等...
这是我的模特:
class Lang_trans_model extends CI_Model {
public function get_lang_trans($admin_id){
$this->db->where('admin_id', $admin_id);
$query = $this->db->get('lang_trans');
return $query->row();
}
这是我的MY_Controller:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result){
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
$lang = $this->lang_trans_model->get_lang_trans($admin_id);
//not sure what should go here????
//I want to be able to use $lang->practitioner; to be able to have practitioner, or the edited attribute to show on the page
var_dump($lang);
// produces the following:
//object(stdClass)[26]
//public 'id' => string '3' (length=1)
//public 'admin_id' => string '3' (length=1)
//public 'practitioner' => string 'blah' (length=4)
//public 'practitioners' => string 'blahplural' (length=10)
如何完成MY_Controller以便能够在我的应用程序中的任何位置使用这些数据字段?
我不介意使用面向对象的语法或数组语法。哪个更容易。我无法弄清楚如何在每个视图中不使用foreach语句来显示字段。我想用$ lang->从业者调用数据;视图中的格式。
提前致谢。
答案 0 :(得分:0)
声明父类属性。比从子类中调用它:
class MY_Controller extends CI_Controller
{
public $lang;//parent variable declared
public function __construct()
{
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result) {
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
//dedicating value to variable
$this->lang = $this->lang_trans_model->get_lang_trans($admin_id);
}
}
儿童控制器:
class Practitioners extends MY_Controller
{
public function __construct()
{
parent::__construct();
// no need to call model if it is called in parent controller
}
public function index()
{
var_dump($this->lang);
}
}