user838531 user838531 - 1 month ago 10
PHP Question

passing query results to view (Codeigniter 2)

I am following a tutorial on Codeigniter 2. Unfortunately, I can't get the sample to work. I receive an error:


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: text1

Filename: views/content_home.php

Line Number: 14


Controller:

public function home()
{

$this->load->model('model_get');
$data['results'] = $this->model_get->getData('home');

$this->load->view('site_header');
$this->load->view('site_nav');
$this->load->view('content_home', $data);
$this->load->view('site_footer');
}


Content Page / View:

<div id="content">

<?php

foreach($results as $row) {

$title = $row->title;
$text1 = $row->text1;
$text2 = $row->text2;
}
?>

<h1>Home Page von Milstink</h1>
<p><?php echo $text1; ?></p>
<p><?php echo $text2; ?></p>


</div>


Model:

<?php

class Model_get extends CI_Model {

public function getData($page)
{
$query = $this->db->get_where('pageData', array('page' => '$page'));
return $query->result();

}
}


Can someone help?

Answer

It seems like $text1 doesn't exits. Change '$page' to $page in model.

class Model_get extends CI_Model {

public function getData($page)
 {
    $query = $this->db->get_where('pageData', array('page' => $page));
    return $query->result();

 }
}