can't pass data to view - php

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

Related

How Select and view count rows from database with Codeigniter

This is my controller : Admin
class Admin extends CI_Controller {
public function index()
{
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
$this->load->view('Admin_view',$data);
}
}
This my model : Dash_model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
This my view : Admin_view
<?php echo $jumlah_instansi; ?>
Please help me, sorry newbie..Thank you..
Thats show error
Message: Undefined property: Admin::$Dash_model
Filename: controllers/Admin.php
and
Message: Call to a member function jml_instansi() on a non-object
You must first load a model before accessing it, e.g.
public function index()
{
$this->load->model('Dash_model');
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
// ...
}
See Loading a Model for more.
Try like this.You are returning integer value of count so no need to use result() result set in controller.
Controller:
class Admin extends CI_Controller {
public function index()
{
$this->load->model('Dash_model');//load model
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi();
$this->load->view('Admin_view',$data);
}
}
Model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
View:
<?php echo $jumlah_instansi; ?>

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

Message: Trying to get property of non-object while passing data from cotroller to view

I wrote function in model as:
public function select()
{
$this->db->select('*');
$this->db->from('students');
$query = $this->db->get();
return $query->result();
}
And controller as:
public function edit()
{
$data['row']=$this->student_model->select();
//print_r($data['row']);
$this->load->view('edit.php',$data);
}
It could print data in controller function..But while passing into the view page I'm getting the error message as:
Message: Trying to get property of non-object
You need to echo your data in edit page like this for every fields
echo $row[0]->field_name;

How to get and display database table value in codeigniter error was coming

I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}

Fatal error: Call to a member function where() on a non-object

i got this fatal error when i run this on browser please solve my problem...i don't get whats wrong with this code.........Fatal error: Call to a member function where() on a non-object
Fatal error: Call to a member function where() on a non-object
model
<?php
class News extends CI_Model
{
function Users(){
//call model constructor
parent::Model();
//load database
$this->load->database();
}
public function get_all_news(){
$this->db->get('news');
if($query->num_rows()>0){
//return result set as associate array
return $query->result_array();
}
}
public function getnews($field,$param){
$this->db->where($field,$param);
$query=$this->db->get('news');
// return result set as accosiate array
return $query->result_array();
}
public function getnumnews(){
return $this->db->count_all('news');
}
}
?>
controller
<?php
class News_data extends CI_Controller{
public function Users(){
// load controller parent
parent::Controller();
// load ‘Users’ model
}
public function index(){
$this->load->model('News');
$data['users']=$this->News->getnews('id <',5);
$data['numusers']=$this->News->getnumnews();
$data['title']='Displaying user data';
$data['header']='User List';
// load ‘Show_data’ view
$data['title']='Display data';
$this->load->view('Show_data',$data);
}
}
?>
view
</head>
<body>
<h1><?php echo $header;?></h1>
<ul>
<?php foreach($users as $user):?>
<li>
<p><?php echo $user['id'].' '.$user['title'].' '.$user['tetxt'];?></p>
</li>
<?php endforeach;?>
</ul>
<p><?php echo 'Total number of users :'.$numusers;?></p>
</body>
</html>
?>
You may not initialising the db object $this->db here $this->db->where($field,$param); OR your $this->db is null.
Please load the database in the model constructor or load via autoload.php
please modify your model as shown below
class News extends CI_Model
{
function __construct(){
$this->load->database();
}
function Users(){
//call model constructor
parent::Model();
//load database
$this->load->database();
}
public function get_all_news(){
$this->db->get('news');
if($query->num_rows()>0){
//return result set as associate array
return $query->result_array();
}
}

Categories