简体   繁体   中英

Codeigniter undefined variable: title , PHP error is encountered

I'm working on simple application consisting of simple form. My code really works fine in local environment. But when i uploaded it on a live server gives an error, unable to fetch database fields and i think there is an error in my model.

Here is my model class

 class Model_get extends CI_Model
    {

    function getData($page) {   

    $query = $this->db->get_where('ci_tbl', array('page' => $page));

    print_r($query->result());
    return $query->result();
}

}

Here goes my view

<div id="content">

<?php


foreach ($results as $row) {
    $title = $row->title;
    $para1 = $row->para1;
    $para2 = $row->para2;

}

echo heading($title, 1);

?>
    <p><?php echo $title;?></p>
    <p><?php echo $para1;?></p>
    <p><?php echo $para2;?></p>
</div>

My controller goes as

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

public function index()
{
    $this->home();
}

public function home() {

$this->load->model('model_get');

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

$this->load->view('header');
$this->load->view('nav');
$this->load->view('main_content',$data);
$this->load->view('footer');


}


public function about() {
$this->load->model('model_get');

$data['results'] = $this->model_get->getData('about');

$this->load->view('header');
$this->load->view('nav');
$this->load->view('about_page',$data);
$this->load->view('footer');

}

Make sure you actually return the results of the query:

//print_r($query->result());
return $query->result();

At the moment you've commented out return $query->result(); and you're just printing the result. The controller calling the model isn't going to get that information and therefore isn't passing through to the view.

Also check that the database connection is correct if moving from local to a public environment.

Actually you had an error

foreach ($results as $row) { 
$title=$ row->title;
       ^^^^

It should be

$title = $row->title;
         ^^^

You need to update your query within model as

class Model_get extends CI_Model {  
  function getData($page) {    
    $query = $this - > db - > get_where('ci_tbl', array('page' => $page));
    return $query->result_array();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM