Gihan Wijethunga
Gihan Wijethunga

Reputation: 85

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>

Upvotes: 0

Views: 1195

Answers (4)

Bouhnosaure
Bouhnosaure

Reputation: 78

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

Upvotes: -1

AkshayP
AkshayP

Reputation: 2159

You need to study basics of 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(); to $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.

Upvotes: 2

Shomz
Shomz

Reputation: 37711

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.

Upvotes: 1

Girish
Girish

Reputation: 12127

no need to add load when calling model method

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

 function getRecords()
    {
        $this->load->library("database");
        $q = $this->db->get("companydetails");
        if($q->num_rows() > 0)
        {
            return $q->result();
        }
        return array();
    }

Upvotes: 0

Related Questions