How to call function in view from controller in codeigniter?

by herminia_bruen , in category: PHP CMS , 9 months ago

How to call function in view from controller in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 9 months 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.