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

在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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端