[英]How to pass data from controller to models in codeigniter
I passed parameter from view to controller via URL.我通过 URL 将参数从视图传递到控制器。 Now I want to send it from controller to model so that I can use it to pick data from tables.现在我想将它从控制器发送到模型,以便我可以使用它从表中选取数据。 Here is my code:这是我的代码:
controller:控制器:
function view(){
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}
$rank=$this->uri->segment($rank);
$this->load->model('names_rank');
$data=$this->names_rank->get_names($rank);
print_r($rank);
}
model:模型:
function get_names($rank){
$this->db->select('u.*,v.*');
$this->db->from('unit_member u, Vyeo v');
$this->db->where('v.fno = u.fno');
$this->db->where('u.present = ""');
$this->db->where('v.rank', $rank);
$this->db->where('v.date_of_end="0000-00-00"');
$query = $this->db->get();
return $query->result_array();
}
this is the result:这是结果:
A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Names_rank::get_names(), called in C:\\xampp\\htdocs\\unit\\application\\controllers\\names.php on line 32 and defined遇到 PHP 错误 严重性:警告消息:Names_rank::get_names() 缺少参数 1,在 C:\\xampp\\htdocs\\unit\\application\\controllers\\names.php 中第 32 行调用并定义
This will work to send to model but your code isn't understandable for me, you re-declare the variable after setting it in the IF?这可以发送到模型,但我无法理解您的代码,您在 IF 中设置变量后重新声明变量? are you trying to print_r() the output from the model?你想打印_r() 模型的输出吗?
I think you are trying to achieve this maybe?我认为您正在努力实现这一目标?
function view() {
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}else{
$rank = $this->uri->segment($rank);
}
$this->load->model('names_rank');
$data = $this->names_rank->get_names($rank);
print_r($data);
}
You can pass a Parameter to your model.您可以将参数传递给您的模型。 First you have to call your model within your controller if you not enable it on autoload.首先,如果您没有在自动加载时启用它,您必须在控制器中调用您的模型。
Your Model:您的型号:
<?php
class AwesomeModel extends CI_Model
{
publif function do_work($param, $anotherParam)
{
//code here
}
}
Then your controller:
<?php
class AwesomeController extends CI_Controller
{
public function __construct()
{
/*
* load in constructor so not need to recall every time you want use it
* second parameter is model renaming (optional)
*/
$this->load->model('AwesomeModel', 'awe');
}
public function pass_data()
{
$this->awe->do_work($param1, $param2);
}
?>
Thats all.就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.