duanqian6982
2014-07-02 10:10
浏览 44
已采纳

在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>

图片转代码服务由CSDN问答提供 功能建议

我需要在传递参数时从标签调用控制器中的方法,这里是代码。当我点击链接时 我需要在模型中调用该函数,

  ** controller ** 
public function company_details($ id){
 $ this-&gt; load-&gt; view(  'view_header'); 
 $ this-&gt; load-&gt; view('view_nav'); 
 $ this-&gt; load-&gt; model('company_detail_model'); 
 $ data ['company_result']  = $ this-&gt; load-&gt; company_detail_model-&gt; getRecords(); 
 $ this-&gt; load-&gt; view('company_details',$ data); 
 $ this-&gt; load-&gt;  view('view_footer'); 
} 
   
 
 

模型

  class  Company_detail_model扩展了CI_Model {
 
函数getRecords()
 {
 $ this-&gt; load-&gt; database(); 
 $ q = $ this-&gt; db-&gt; get(“companydetails”)  ; 
 if($ q-&gt; num_rows()&gt; 0)
 {
返回$ q-&gt; result(); 
} 
返回array(); 
} 
  
 
} 
   
 
 

查看

 &lt; label for =“  folder1“&gt;&lt; a href =”&lt;?php echo site_url('site2 / company_details'。$ row-&gt; id);  ?&gt;“&gt;&lt;?= $ row-&gt; name?&gt;&lt; / label&gt;&lt; / a&gt; 
   
 
 

我需要显示 这些数据在文本输入形式如下,

 &lt;?php echo form_open_multipart('site / upload');?&gt; 
&lt; label&gt;代码:&lt;  / label&gt;&lt;?php echo form_input('code');?&gt;&lt; br /&gt;&lt; br /&gt; 
&lt; label&gt;名称:&lt; / label&gt;&lt;?php echo form_input(  'name');?&gt;&lt; br /&gt;&lt; br /&gt; 
&lt; label&gt;徽标:&lt; / label&gt;&lt; input type =“file”name =“userfile”/&gt;&lt;  ; br /&gt;&lt; br /&gt; 
&lt; label&gt;网址:&lt; / label&gt;&lt;?php echo form_input('url');?&gt;&lt; br /&gt;&lt; br /&gt;  ; 
&lt; label&gt;说明:&lt; / label&gt;&lt; textarea name =“description”rows =“4”cols =“50”&gt;&lt; / textarea&gt;&lt; br /&gt;&lt; br /&gt;  ; 
&lt; input type =“submit”name =“submit”value =“Save”/&gt; 
&lt; / form&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • dongyi2006 2014-07-02 10:33
    最佳回答

    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):

    <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.

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题