Getting Error : Trying to get property of non object - php

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 }?>

Related

Unable to pass data from controller to view in codeigniter

I'm having issues passing data from my controller to my view. I have a contenteditable div that I am saving the contents of to my database; I want to then display that in the view.
My controller
{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('view_data');
$data = array();
}
public function index(){
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data);
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
}
My model
<?php
class View_data extends CI_Model{
function __construct(){
parent::__construct();
}
public function loadData(){
$this->db->where('contactMe');
$query = $this->db->get('content');
return $query->result_array();
}
}
My view
<div class="w-100" ondblclick="makeEditable(this)" onblur="makeReadOnly(this)" contenteditable="true" id="contactMe">
<?php
foreach($contactMeData as $contact){
echo $contact;
}
?>
</div>
<button id="editBtn" type="submit" value="">Save changes
</button>
Every time I reload the page it replaces the data already in the database and I just get a blank div. I'm unable to view the data that was in the database to begin with.
try to add parameter on where
$this->db->where('contactMe',$parameter);
try to add field name on your view
echo $contact['field_name'];
This might be because every time you open the URL it updates the table, so you need a condition to check if POST request was made or not and then update the column
public function index(){
if($this->input->post()){ // check for post request
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data); // try writing db queries in model
}
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
See, if it helps you.

How to Pass ID variable to controller in CodeIgniter

I have a list of messages and when the click on the title it takes them to another view where they can see the expanded message.
This is the view from which I click the link.
Postings View.
Link
Message Controller:
class Message extends CI_Controller {
var $TPL;
public function __construct()
{
parent::__construct();
}
private function display()
{
$query = $this->db->query("SELECT FROM messages WHERE id = '$id';");
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
public function index()
{
$this->display();
}
}
Message View
<?$int=0;?>
<? foreach ($threads as $row) { ?>
<div class="row">
<div class="message">
<h3><?= $row['title']?></h3>
<p><?= $row['message']?></p>
<p><?= $row['member']?></p>
</div>
</div>
<hr>
<? $int++;?>
<? } ?>
This is easy. Change your code as follwoing
Link
Then change you display method as public and send ID param there like
public function display($id){
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
And finally remove $int=0 & $int++; from view file as you are not using this. Now test
Change the link
Link
And in your controller
public function display() {
$id=$this->uri->segment(3);
if($id==null) {
redirect('Index');
}
else {
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
}

CodeIgniter - Undefined Property Error

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;?>

How to show category name in Product Page in Codigniter?

I have two table in database ::: 1. tbl_category 2. tbl_product.
Showing all category list in home page and when user clicks on signle category then it goes to product page(from tbl_product) by category id (included category id in tbl_product)
Issue ::: I want to show the category Name and Image Here. So that user can see in which category they clicked on (from tbl_category ) ::: how can I get them ??? please help me
Controller ::::
class Home extends CI_Controller{
//put your code here
public function __construct() {
parent::__construct();
$this->load->model('home_model');
}
public function index(){
$data=array();
$data['result']=$this->home_model->selectCategory($config['per_page'], $this->uri->segment(3));
$data['maincontent']=$this->load->view('home_message',$data,TRUE);
$this->load->view('home', $data);
}
public function category($category_id){
$data=array();
$data['result']=$this->home_model->selectProductByCategoryId($category_id);
$data['maincontent']=$this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
Model::::
class Home_Model extends CI_Model{
//put your code here
public function selectCategory($per_page, $offset)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->order_by("category_id", "desc");
$query = $this->db->get('', $per_page, $offset);
foreach ($query->result() as $row)
$data[] = $row;
return $data;
}
public function selectProductByCategoryId($category_id)
{
$this->db->select('*');
$this->db->from('tbl_product');
$this->db->where('category_id',$category_id);
//$this->db->order_by("product_id", "desc");
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
View::::
I want to show the category Name and Image Here. So that user can see in which category they clicked on ::::
<?php
foreach ($result as $values)
{
?>
<div>
<div>
<img src="<?php echo base_url();?><?php echo $values->product_image ?>" width="90%" height="220" />
</div>
<div>
<b><?php echo $values->product_name ?></b> <br>
<b>Price: <?php echo $values->product_price ?></b> <br>
Order Now
</div>
</div>
<?php } ?>
Controller
public function category($category_id){
$data=array();
$data['result'] = $this->home_model->selectProductByCategoryId($category_id);
// Added this line
$data['category'] = $this->home_model->selectCategoryByd($category_id);
$data['maincontent'] = $this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
Model_Home
public function selectCategoryById($category_id)
{
$result = $this->db->select('*')
->from('tbl_category')
->where('id',$category_id)
->get()
->result();
return $result;
}
Then echo some of those in your view.
You are using a Model_Home for other models functions?
Models are for data and everything what has to do with that data. You should have a Model_Category with a selectById function and a Model_Product with a selectByCategoryId AND a selectById function. Models are NOT used to describe pages.
Please read about MVC and CodeIgniters implementation of it. Then refactor your code and properly use codeigniters features which, if implemented correctly, allow you to not even have to write these functions yourself! Note: I do not know CI very well, so I don't know about the specific implementations of e.g. MVC, ORM, and the like

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;
}
}

Categories