PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] Why doesn't the controller recognise the data passed on from the model?

 January 30, 2022     codeigniter, php     No comments   

Issue

I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.

I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:

controller.php

public function Home()
{
  $this->load->model('Main');
  $this->Main->getresults();

  $this->load->view('header', $data);
}

model.php

public function getresults() {

  $query = $this->db->get('table');
  foreach ($query->result_array() as $row) {

    $data = array(
      'column' => $row["column"]
    );
}
return $data;
}

view.php

<?php echo $column; ?>

I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.


Solution

In your controller you don't assign and send data to view. Change your code there:

public function Home()
{
  $this->load->model('Main');
  $data = $this->Main->getresults();

  $this->load->view('header', $data);
}


Answered By - stefo91
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing