How to run Model function in View with codeigniter?

Asked

Viewed 757 times

0

I am using codeigniter (based on PHP and Model-Control-View). I have a controller that loads a Model and a View.

[Controller]

public function index()
    {
        $data['agenda_model'] = $this->load->model('agenda_model');
        $this->load->view('home', $data);
        $this->load->view('commons/bootstrap-scripts');
    }

[View]

<body onload="initialize()">
</body>
<script>
   function initialize(){
       console.log(<?php echo $agenda_model->getAgendaDate('05-03-2019'); ?>);
  }
</script>

[Model]

public function getAgendaDate($date){
        echo $date;
        $sql = 'select * from agenda';
        $result = $this->db->query($sql);
        return $result->num_rows;
    }

When I run the above code, I have the following error in the script. ERROR: Uncaught Referenceerror: initialize is not defined at onload

[Simplification/Debbuging] To simplify things, I decided to do a php echo inside javascript.

var result = <?php echo "maria"; ?>;
console.log(result);

(index):2698 Uncaught ReferenceError: maria is not defined

[update] - 02/03/2019

"PHP runs next to the server" this may explain why I can’t perform one of the Model functions. Is there a Pattern design to run model methods in the view? However this does not explain why it does not perform:

var result = <?php echo "maria"; ?>;

My question. It will be efficient with alternative, do HTTP GET/POST?

1 answer

3

To call the model in a view, you can try to instantiate the Codeigniter core in it, using $CI =& get_instance() and then load the model using $CI->load->model('suaModel'). From then on you can use the $CI->suaModel to call her methods in the view.

Regarding the javascript error, this is because you are printing the maria string inside a script, however the php output is out of quotes. Therefore, javasript understands as a variable name, not a string.

Following his example:

var result = <?php echo "maria"; ?>;

The exit will be:

var result = maria;

That is, your script will look for a variable called maria, which is 'Undefined' because it is not set. The correct is to quote the PHP output from the script:

var result = '<?php echo "maria"; ?>';

So the way out will be:

var result = 'maria';

I hope I’ve helped !

  • Thanks for the help. Where I run the functions to instantiate the core?

  • In the file itself where you want to use it. In your case, in the view. Remembering that this is a sin in MVC haha

  • It also seems to go against the ideology of MVC. For example if you make a $_GET('name') in the view, which means the 'name' argument. Where do I wait for GET? Do I have to assign a route? Runs a little away from the topic, but it’s an alternative solution. However not yet found clear documentation for GET/POST, can help me?

  • Considering that you are using version 3.x of the IC, you can see the documentation that explains about get, post and everything else related to inputs here: https://www.codeigniter.com/user_guide/libraries/input.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.