Trying to get property of non-object in view - php

I have a link ending with index.php/welcome/item/1
I have my controller welcome.php with the function item
public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}
and my model with the get_item function
public function get_item($id)
{
$this->db->select('*');
$this->db->from('portfolio');
$this->db->where('id', $id);
$query = $this->db->get();
return $item = $query->result();
}
In my item view i want to echo info out of the database. Why doesn't it do that now
<h1>item</h1>
<?php echo $item->naam; ?>
Error i get on the page:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/item.php
Line Number: 2
The page gets loaded normal, no problems.
What am i doing wrong?

Since you're expecting one single result, your get_item function should
return $query->row();

Replace in your get_item function this :
result()
This function returns the query result as an array of objects, or an
empty array on failure.
return $item = $query->result();
Like this :
row()
This function returns a single result row. If your query has more than
one row, it returns only the first row. The result is returned as an
object.
return $item = $query->row();
Documentation

You need to call the library database ( $this->load->database(); ) and the model ($this->load->model('port_model');) :
Result:
<code>public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$this->load->database();
$this->load->model('port_model');
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}</code>

Related

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

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

can't pass data to view

I'm learning codeigniter framework. I've managed to make login page work, now I'm trying to pass data from controller to my view. I'm getting errors I have looked and looked here and did as suggested still getting the errors/
Model
class Dashboard_Model extends CI_Model{
function __construct(){
parent::__construct();
}
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result();
}
}
Controller
class Dashboard extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('admin/dashboard_model');
}
public function index(){
if(!$this->session->logged_in){
redirect('user_login');
}else{
$data['title'] = $this->session->fullname. " | Dashboard";
$this->load->view('admin/common/header',$data);
$data['total'] = $this->dashboard_model->getTasks();
$this->load->view('admin/dashboard',$data);
$this->load->view('admin/common/footer');
}
}
}
View
<h2 class="text-white"><span data-plugin="counterup"><?php echo $total?></span> <small><i class="mdi mdi-arrow-up text-success"></i></small></h2>
Error :
Severity: Notice
Message: Array to string conversion
Filename: admin/dashboard.php
Line Number: 40
What I am doing wrong?
result() This method returns the query result as an array of objects, or an empty array on failure update getTasks like below
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result()[0]->totaltasks;
}
You can use row() to get single row instead of result() when you already know that you will get one row as a result
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->row()->totaltasks;
}

I can't get data and post it in my html

model
public function getPost(){
$this->db->select('post_title', 'post_auth', 'post_content');
$this->db->from('post');
$query = $this->db->get();
return $query->result_array();
}
controller
public function index(){
$this->load->model('swmodel');
$data['posts'] = $this->swmodel->getPost();
$this->load->view('dashBoard', $data);
}
How can I transfer data in my view?
Honestly I can't say you why, but for some reason in CodeIgniter 3 you have to store the result or return of your function in a variable and then that variable assign it within the array based value you pass to your view as $this->load->view('dashBoard', $data);.
You need to check does exist a view called dashBoard? and where is it?
So you can try with something like:
public function index() {
$this->load->model('posts_model');
// Get your posts, store them in $posts.
$posts = $this->posts_model->getPost();
// Assign your $posts to your $data.
$data['posts'] = $posts;
// Pass your $data when you load the view.
$this->load->view('dashBoard', $data);
}
And then in your view you can easily access to the passed data as $posts.
# dashBoard.php
<?php var_dump($posts); ?>

how to use model function result in controller by codeigniter

I have a function in Codeignite model that return just a row. This bellow code is my function in Codeigniter model:
public function Load_Home_Page_Theme(){
$CMD = "call load_home_page_theme()";
$query = $this->db->query($CMD);
return $query->result();
}
Now I want to use a field value that this function returned in a controller function like this:
public function Home(){
$this->load->model("Admin/Admin_getdata");
$data["HomePageTheme"] = $this->Admin_getdata->Load_Home_Page_Theme();
$this->load->view("User/Header", $data);
$data["AdsData"] = $this->Admin_getdata->Load_Ads_Data($data["HomePageTheme"]["home_ads_quantity"]);
$this->load->view("User/Home", $data);
$this->load->view("User/Footer");
}
But when I run this code, this error will shown:
Message: Undefined index: home_ads_quantity
How I can solve it?
Please guide me.
Try it:::
Model
public function Load_Home_Page_Theme(){
$CMD = "call load_home_page_theme()";
$query = $this->db->query($CMD);
return $query->row();
}
Controller:
public function Home(){
$this->load->model("Admin/Admin_getdata");
$HomePageTheme = $this->Admin_getdata->Load_Home_Page_Theme();
$data['HomePageTheme']=$HomePageTheme;
$this->load->view("User/Header", $data);
$data["AdsData"] = $this->Admin_getdata->Load_Ads_Data($HomePageTheme->home_ads_quantity);
$this->load->view("User/Home", $data);
$this->load->view("User/Footer");
}

Variable passed but exception still thrown?

I create and pass a data array:
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
$this->load->view('template', $data);
}
This is the template view:
<?php
$this->load->view('header');
$this->load->view('nav', $data);
$data['results'] = $results;
$this->load->view($temp, $data);
$this->load->view('footer');
?>
It throws an exception for undefined variable at:
$this->load->view('nav', $data);
but still loads the view and completes all the if statements inside it and loads the view from the name stored in $temp.
Why does it throw the exception?
You haven't populated $data in that view to load a second view. Your code is too vaguely named to really understand what it's supposed to be doing but let's break it down.
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
//I assume getData takes a table name and returns all the data
//This only works because you're taking all the data passed from the first function
//in the function above.
$this->load->view('template', $data);
}
<?php
$this->load->view('header');
$this->load->view('nav', $data);
//At this point $data is empty. You have available $results, $page, $table and $temp
//because they were passed from the template function above.
$this->load->view('nav', $results);
//The line above is what it appears you need, rather than $data.
$data['results'] = $results;
//You've repopulated $data here but now all it contains is $results.
$this->load->view($temp, $data);
$this->load->view('footer');
?>
What you're misunderstanding is $data is not persistent, when you pass it to a view by loading a view with $data as the second parameter it breaks down into it's components, so $data itself is no longer available.

Categories