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

在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>
  • 写回答

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?