How to get value from another model in codeigniter?

Member

by brandy , in category: PHP Frameworks , 9 months ago

How to get value from another model in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 9 months ago

@brandy 

To get value from another model in CodeIgniter, you can follow these steps:

  1. Load the model: First, you need to load the model that you want to get the value from. You can do this by using the load method in the controller.
1
$this->load->model('Model_name');


  1. Call the method in the model: Once the model is loaded, you can call the method in the model that returns the value you want. You can do this by using the following syntax:
1
$value = $this->Model_name->method_name();


  1. Use the value: Once you have obtained the value from the model, you can use it in your controller or view as needed.


Here is an example:


Controller:

1
2
3
4
$this->load->model('User_model');
$user_name = $this->User_model->get_user_name();
$data['user_name'] = $user_name;
$this->load->view('view_name', $data);


Model:

1
2
3
4
public function get_user_name() {
    $user = $this->db->get('users')->row();
    return $user->name;
}


View:

1
<h1>Welcome, <?php echo $user_name; ?></h1>


This is how you can get a value from another model in CodeIgniter.

Related Threads:

How to get value of model when iterating over collection in knockout.js?
How to get model instance in codeigniter?
How to get value from url in codeigniter?
How to get checkbox value in codeigniter?
How to get value from two table using join in codeigniter?
How to get max and min value of a column from database in codeigniter?