How Select and view count rows from database with Codeigniter - php

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

Related

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

codeigniter how to send query->num_rows from model to controller to view

I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>

Php - passing a variable from controller to view

whenever I run this code i get the error of undefined variable $d, so i noticed i have to pass the variable from the controller to the view. I am new to MVC, hence why i would be grateful if someone helps me with this issue.
This is my implementation so far:
**
Controller
**
class ControllerModuleGetstoreproducts extends Controller{
public function index() {
$object = new ModelGetstoreproducts();
$d = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
Model
<?php
class ModelGetstoreproducts{
function fetch(){
$sql = 'SELECT count(*) FROM oc_product';
$req = #mysql_query($sql);
return #mysql_fetch_object($req);
//return $req;
}
}
?>
View
<p>The total is <?php var_dump($d) ?></p>
Try this update to your files:
Controller:
class ControllerModuleGetstoreproducts extends Controller{
public $data;
public function index() {
$object = new ModelGetstoreproducts();
$this->data['products'] = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
View:
<p>The total is <?php var_dump($this->data['products']) ?></p>
Pass your id from View to Controller and get it into Model.Apply this rule You will not get any kind of error.

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

Call to a member function get() on a non-object

I'm using the Codeigniter Framework and I'm getting the following error,
call to a member function get() on a non-object in site_model.php on line 11
which is the row with the get() . test is the name of my table in my database. What am i doing wrong?
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->model('site_model');
$data['records']= $this->site_model->getAll();
$this->load->view('home',$data);
}
}
?>
<?php
class Site_model extends CI_Model
{
function getAll()
{
$q = $this->db->get('test');
if($q->num_rows > 0 )
{
foreach ($q ->result() as $row )
{
$data[] = $row ;
}
}
return $data;
}
}
My best guess is you didn't setup your database:
When a model is loaded it does NOT connect automatically to your
database.
See the guide here: https://www.codeigniter.com/user_guide/general/models.html#connecting-to-your-database

Categories