[英]call method in controller in codeigniter
I need to call a method in controller from tag while passing parameter,here is the code.When I click the link I need to call that function in model, 我需要在传递参数的同时从标记中调用控制器中的方法,这是代码。单击链接时,我需要在模型中调用该函数,
**controller**
public function company_details($id){
$this->load->view('view_header');
$this->load->view('view_nav');
$this->load->model('company_detail_model');
$data['company_result'] = $this->load->company_detail_model->getRecords();
$this->load->view('company_details',$data);
$this->load->view('view_footer');
}
model 模型
class Company_detail_model extends CI_Model{
function getRecords()
{
$this->load->database();
$q = $this->db->get("companydetails");
if($q->num_rows() > 0)
{
return $q->result();
}
return array();
}
}
view 视图
<label for="folder1"><a href="<?php echo site_url('site2/company_details'.$row->id); ?>"><?=$row->name?></label></a>
I need to display these data in text input form like this, 我需要以这样的文本输入形式显示这些数据,
<?php echo form_open_multipart('site/upload');?>
<label>Code : </label> <?php echo form_input('code');?><br/><br/>
<label>Name : </label> <?php echo form_input('name');?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url');?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"></textarea><br/><br/>
<input type="submit" name="submit" value="Save"/>
</form>
You need to study basics of Codeigniter . 您需要学习Codeigniter的基础知识。
try following code 尝试以下代码
**controller**
public function company_details($id){
$this->load->view('view_header');
$this->load->view('view_nav');
$this->load->model('company_detail_model');
$data['company_result'] =$this->company_detail_model->getRecords();
$this->load->view('company_details',$data);
$this->load->view('view_footer');
}
You need to change $this->load->company_detail_model->getRecords();
您需要更改$this->load->company_detail_model->getRecords();
to $this->company_detail_model->getRecords();
到$this->company_detail_model->getRecords();
EDIT : 编辑:
Your model function could be like below : 您的模型函数可能如下所示:
function getRecords()
{
$this->load->database();
$query = $this->db->get("companydetails");
return $query->result_array();
}
The result_array()
method reruns result in array form. result_array()
方法以数组形式重新运行结果。
Fix the model call after it's loaded (no more loading): 加载后修复模型调用(不再加载):
$data['company_result'] = $this->company_detail_model->getRecords();
Fix the anchor link (you're missing a slash) and the HTML structure (nesting errors): 修复锚链接(缺少斜线)和HTML结构(嵌套错误):
<label for="folder1">
<a href="<?php echo site_url('site2/company_details/'.$row->id); ?>">
<?=$row->name?>
</a>
</label>
This is not breaking anything, but usually the views are loaded at the end of the controller method call. 这不会破坏任何内容,但通常在控制器方法调用的末尾加载视图。 Ideally, the method should look something like this: 理想情况下,该方法应如下所示:
public function company_details($id){
$this->load->model('company_detail_model');
$data['company_result'] = $this->company_detail_model->getRecords();
$this->load->view('view_header');
$this->load->view('view_nav');
$this->load->view('company_details',$data);
$this->load->view('view_footer');
}
Your model method could also be optimized to something like this: 您的模型方法也可以优化为以下形式:
function getRecords()
{
$this->load->database();
return $this->db->get("companydetails")->result();
}
In the end, it might be a good idea to use autoloading for common stuff such as the database. 最后,将自动加载用于数据库等常见内容可能是一个好主意。
no need to add load
when calling model method 调用模型方法时无需添加load
please change 请更换
$this->load->company_detail_model->getRecords();
to 至
$this->company_detail_model->getRecords();
And in the model, use $this->load->library
to load database
library 在模型中,使用$this->load->library
加载database
库
function getRecords()
{
$this->load->library("database");
$q = $this->db->get("companydetails");
if($q->num_rows() > 0)
{
return $q->result();
}
return array();
}
So if you wan to do this, 因此,如果您想这样做,
in you controller: 在您的控制器中:
public function company_details($id){
//load model
$this->load->model('company_detail_model');
//get back your data
$data['company_result'] = $this->company_detail_model->getRecords();
$this->load->view('view_header');
$this->load->view('view_nav');
$this->load->view('company_details',$data);
$this->load->view('view_footer');
}
in you model : 在您的模型中:
class Company_detail_model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
public function getRecords()
{
$query = $this->db->get("companydetails");
return $query->result();
}
}
And in your view 在您看来
<!-- we check if $company_result is not empty instead of control in model -->
<php? if($company_result!= FALSE): ?>
<label for="folder1">
<!--+
|note, we read the value id from $company_result because you pass this
|varible in your controller with $data['company_result']
+-->
<a href="<?php echo site_url('site2/company_details'.$company_result->id); ?>">
<?=$company_result->name?>
</a>
</label>
<?php endif; ?>
after if you want to get an specific page using parameter you have to make an other method in your controller or add a control like : 如果要使用参数获取特定页面,则必须在控制器中创建其他方法或添加类似以下的控件:
public function company_details($id = null){
//load model
$this->load->model('company_detail_model');
$this->load->view('view_header');
$this->load->view('view_nav');
if($id != null && is_numeric($id)){
//get back your specific data in another wiew
//make your custom method
$data['company_result'] = $this->company_detail_model->getRecordsByID($id);
//load your specific view
$this->load->view('company_details_wiew',$data);
}else{
//get back your data
$data['company_result'] = $this->company_detail_model->getRecords();
$this->load->view('company_details',$data);
}
$this->load->view('view_footer');
}
and in model 并在模型中
public function getRecordsByID($id)
{
$this->db->where("id",$id);
$query = $this->db->get("companydetails");
return $query->result();
}
i hope it's help you :) 我希望它对您有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.