我在codeigniter中创建一个站点,这是我的控制器,模型和视图:
控制器:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
模型:
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
视图:
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
但我想在我的控制器中获取$ new_dbemail ['name'],我怎样才能在控制器中获取$ new_dbemail ['name']的值而不是查看???
为此...您必须将数据提取到row_array中,而不是在Model中的result_array中。
调节器
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$database_field = $data['dbemail']['database_field']; //to get result in controller
$date['database_field'] = $data['dbemail']['database_field']; //to get result in view
$this->load->view('home', $data);
}
模型
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
return $query->row_array();
}
视图
<p> Database Field value : <?=$database_field;?></p>
你在变量中调用模型: -
$data['dbemail']=$this->login_model->email();
因此,您将获得从数据库中获取的结果。比将此变量数据传递给View。
$this->load->view('home', $data);
因此,您已经拥有了从存储在$ data中的模型中获取的控制器中的值。
您可以使用查看$ data的值
echo "<pre>";
print_r($data);
它将显示您正在接收的数据
试试这样......
在控制器......
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();//array containing all emails
//For each loop here for displaying emails
foreach($dbemail as $email){
$mail = $email; //assigning to variable
echo $mail."<br/>";//displaying email
}
$data['dbemail'] = $dbemail;//for passing to view
$this->load->view('home', $data); //passing your value to view
}
首先,您需要从模型中的数据库中获取名称。更改查询行:
$query = $this->db->query("SELECT name, email FROM change_password");
然后要使用控制器中的名称数据,您可以尝试这样做:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$name = $data['dbemail']['name']; // example usage to retrieve name in controller
$this->load->view('home', $data);
}
现在名称数据在控制器中以$name
的形式提供。
控制器:
public function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemails']=$this->login_model->email();
$this->load->view('home', $data); //passing your value to view
}
模型
public function email()
{
$this->db->select("email"); // the row of the table you want to select.
$this->db->get('change_password'); //table name. you can still use your query, its just much neat when you use a specific query builder.
$result = $this->result(); //giving you a result of type object
return $result;
}
视图
foreach ( $dbemails as $dbemail )
{
echo $dbemail->email; // the '->' will be the identifier of which row you want to get or display, i assume its email beacause you specify it in the model.
}
希望这是你想要的。