CodeIgniter - Undefined Property Error - php

I am trying to access all the bands in my table and print them out in a list, but when I run it I get this error:
Severity: Notice
Message: Undefined property: CI_Loader::$model_bands
Filename: views/band_view.php
Line Number: 16
band_view.php:
<h3>List of Bands:</h3>
<?php
$bands = $this->model_bands->getAllBands();
echo $bands;
?>
model_bands.php:
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
Could someone pleease tell me why it is doing this?

Why do you need to do this, the correct way is to use the model methods inside a controller, then passing it to the view:
public function controller_name()
{
$data = array();
$this->load->model('Model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('view_name', $data); // load the gathered data into the view
}
And then use $bands (loop) in the view.
<h3>List of Bands:</h3>
<?php foreach($bands as $band): ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php endforeach; ?>

Did you load your model in the Controller?
$this->load->model("model_bands");

You need to change your code like
Controller
public function AllBrands()
{
$data = array();
$this->load->model('model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('band_view', $data); // load the gathered data into the view
}
Then View
<h3>List of Bands:</h3>
<?php foreach($bands as $band){ ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php } ?>
Your Model is ok
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}

You forgot to load the model on your controller:
//controller
function __construct()
{
$this->load->model('model_bands'); // load the model
}
BTW, why are you calling the model directly from your view? Should it be:
//model
$bands = $this->model_bands->getAllBands();
$this->load->view('band_view', array('bands' => $bands));
//view
<h3>List of Bands:</h3>
<?php echo $bands;?>

Related

Getting Error : Trying to get property of non object

I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>

CodeIgniter- Message: Undefined variable: data

I'm working on a view that shows me the data from the database.
but the result is
Severity: Notice
Message: Undefined variable: data
Filename: mentee/index.php
UserModel.php
<?php
class UserModel extends CI_Model{
public function __construct(){
parent::__construct();
}
public function userProfile($nim){
$this->db->where('userNIM',$nim);
$query= $this->db->get('msuser');
return $query->result();
}
}
MainController.php
public function indexMentee()
{
$this->load->model('UserModel');
$userID=$this->session->userdata("userNIM");
$data['data'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', array('data' => $data));
}
View
<?php
die(var_dump($data));
if(is_array($data)){
foreach ($data as $profile):
?>
<div class="header-content">
<div class="header-content-inner">
<h1>Learning Center</h1>
<hr>
<h3> Welcome,<?php echo $profile->userName ?></h3>
</div>
</div>
<?php
endforeach;
}
?>
Please help me..Thanks a lot!
Instead of passing array('data' => $data)); just pass $data to the view.
First print session data. If session data present then pass data to view page
$data['details'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', $data);
And print print_r($details); on your view page

Why does this model return "non-object error", when it worked in the controller?

In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>

Display multiple rows in codeigniter

I have query where I fetch the content of about 5 rows each with different content, but i dont now to display them individually in my views.
When I pass the the query->result() from my model back to my controller as $query, and then load a view with the $query data, I don't know how to echo out the e.g. content field of row 1.
I only know how to do this: foreach($query as $row) { echo $row->content; }
But this doesn't allow me to select what row content data (out of the 5 retrieved) I wanna echo out.
Could someone please explain how this is done?
The CodeIgniter documentation includes a section titled Generating Query Results which has some examples of what you are looking for.
Here's an (obviously contrived) example:
model:
class Model extends CI_Model
{
public function get_data()
{
return $this->db->query('...');
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
$data['query_result'] = $this->model->get_data();
$this->load->view('index', $data);
}
}
view:
<?php foreach ($query_result->result() as $row): ?>
<?php echo $row->field_1; ?>
<?php echo $row->field_2; ?>
<?php // and so on... ?>
<?php endforeach; ?>
function a(){
$this->db->select('*');
$this->db->from('table');
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$query=$this->db->get();
for($i=1;$i<=$query->num_rows();++$i){
$data[$i]['task'] = $query->row($i)->task;
$data[$i]['subject_id'] = $query->row($i)->subject_id;
$data[$i]['postdate'] = $query->row($i)->postdate;
$data[$i]['cdate'] = $query->row($i)->cdate;
}
}

code igniter question: problem with passing variable to a view

I am a newbie with CodeIgniter (2.03), and I have the following problem:
Here is my main template (view):
<?php $this->load->view('backOffice/bo_header_in'); ?>
<?php $this->load->view($bo_main_content); ?>
<?php $this->load->view('backOffice/bo_footer_in'); ?>
Here is my model:
<?php
class Back_office_users extends CI_Model
{
public function getAllUsers ()
{
$query = $this->db->query("SELECT * FROM users");
if ($query->num_rows() > 0) {
foreach ($query->result() as $rows) {
$users[] = $rows;
}
return $users;
}
}
}
And here is my controler:
<?php
class Dashboard extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->is_logged_in();
}
public function index ()
{
$this->load->model('back_office_users');
$users['rows'] = $this->back_office_users->getAllUsers();
$data['bo_main_content'] = "backOffice/dashboard";
$this->load->view('backOffice/bo_template_in', $data, $users);
// if I pass the variable like this it works just fine...
//$this->load->view('backOffice/users', $users);
}
public function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || ($is_logged_in != true)) {
$this->accessdenied();
}
}
public function accessdenied ()
{
$data['bo_main_content'] = 'backOffice/accessdenied';
$this->load->view('backOffice/bo_template', $data);
}
public function logout ()
{
$this->session->sess_destroy();
redirect('backOffice/index');
}
}
And the dashboard view is like this:
<?php
print_r($users);
?>
I am getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: users
Filename: backOffice/dashboard.php
Line Number: 9
Can anyone shed some light how can I resolve this? I create another view without use of the template, and it print the array.
You're not passing the $users variable to the second (nested) view.
I'd suggest adding $users to the $data array, and then in the first view pass the $users array to the embedded view. So, in your controller:
public function index () {
/* stuff... */
$data['users']['rows'] = $this->back_office_users->getAllUsers();
$data['bo_main_content'] = "backOffice/dashboard";
/* stuff... */
$this->load->view('backOffice/bo_template_in', $data);
}
Then in the main view:
<?php $this->load->view($bo_main_content, $users); ?>
Then in the dashboard view:
<?php
print_r($rows);
?>
This is because in the main view, as you know, CodeIgniter transforms all elements of $data in to variables, so we'll end up with the $users variables. $users is an array containing rows, so when we pass $users to the second view, the second view transforms all elements of $users in to view variables, hence we now have access to $row.

Categories