How to call function in view from controller in codeigniter?

by herminia_bruen , in category: PHP CMS , a year ago

How to call function in view from controller in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , a year ago

@herminia_bruen 

In CodeIgniter, you can call a function in a view from a controller by passing data to the view using the $this->load->view() method. Here is an example of how to call a function in a view from a controller:

  1. In your controller, load the view and pass data to it:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function index()
{
    $data['message'] = $this->getMessage(); // Call the function getMessage() and pass the result to the view
    $this->load->view('my_view', $data);
}

public function getMessage()
{
    return "Hello, World!";
}


  1. In your view (e.g. my_view.php), you can access the data passed from the controller:
1
2
3
<?php
echo $message; // Output: Hello, World!
?>


By following these steps, you can call a function from a controller and display the result in a view in CodeIgniter.

Related Threads:

How to use model function result in controller by codeigniter?
How to call function inside function in codeigniter?
How to redirect from view to controller in codeigniter?
What is best way to call postgresql function from codeigniter?
How to send data from angular function to codeigniter view to model?
How to call a javascript function in a mustache template?