Codeigniter Model returning empty data - php

I have a problem with my model. It's returning empty data.
Here is the controller.
public function getReport() {
$data = array();
$id = $this->input->post('id');
$this->auth->setId($id);
$data['instant_main']=$this->auth->getReportData();
$this->load->view('auth/report-item', $data);
}
and this is my Model
class Auth_model extends CI_Model {
// Declaration of a variables
private $_id;
public function setId($id) {
$this->_id = $id;
}
function getReportData()
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $this->_id);
$query = $this->db->get();
return $query->result();
}
}
If I have removed the WHERE condition from the query, everything was fine.

Your $this_id is empty..
You have folow the code like this
Controller
$data['instant_main']=$this->auth->getReportData($id);
Model
function getReportData($id)
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $id);
$query = $this->db->get();
return $query->result();
}

Related

Codeigniter - count all on the table not working

could ya'll help me on this one please. I'm trying to count how many are there in my table as it doesn't show up the result on my view.
Controller
public function enrollment_summary()
{
$data['title'] = 'Enrollment Summary';
$this->load->model('report_model');
$data['query'] = $this->report_model->get_students();
$this->load->view('report_enrollment_summary', $data);
}
Model
<?php
class report_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_students()
{
$query = $this->db->count_all('students');
}
}
View
<body>
<h1>Enrollment Summary</h1>
<?php echo $query; ?>
</body>
Your get_students() is not returning anything?
So you need to add in a return.
Your current method...
public function get_students()
{
$query = $this->db->count_all('students');
}
becomes
public function get_students()
{
$query = $this->db->count_all('students');
return $query;
}
OR
public function get_students()
{
return $this->db->count_all('students');
}

How obtain directly the model from a query? -> CodeIgniter

Can I obtain the model directly from a query in Code Igniter?
Model
<?php
class User_Model extends CI_Model {
public $user_id;
public $name;
public $team_id
public function getTeamName() {
$this->db->select('name');
$this->db->from('team');
$this->db->where('team_id',$this->team_id);
$query = $this->db->get();
$team = $query->row();
if($empresa == null){
return "";
} else {
return $team->name;
}
}
public function getUser($id) {
$query = $this->db->get_where('users', array('user_id' => $id));
$user = $query->row();
return $user;
}
I would like getUser($id) function returns a user object like 'new User_Model()' to be able to use getTeamName() function in the view. Can I do it?
Now returns an object, but not a User_Model() object.
is wrong call a method in the model from the view.
in the model use
function getUser($id){
$query = $this-db->select('*')
->from('users')
->where('user_id', $id)
->get();
return $query->result();
}
this return a object with all data, then you call the method in the controller like this
$data['user_model'] = $this->model_name->getUser($id);
$this->load->view('Your_View',$data);
then in the view $user_model is the object
in the view
print_r($user_model);
As long as the model is loaded, in your view you can access any public function or property like:
$this->user_model->getUser() or $this->user_model->team_id
View:
<?php
echo $this->user_model->getUser(1)->username;
This is against MVC in some respects but I can understand the logic behind doing so based on the following scenario where you want to say use num_rows and result without doing count:
class User_Model extends CI_Model {
public function getUsers() {
return $this->db->get('users');
}
}
class Some_Controller extends CI_Controller {
public function index() {
$this->load->model('user_model');
$data['users'] = $this->user_model->getUsers();
$this->load->view('some_view', $data);
}
}
View:
<?php
if ($users->num_rows() > 0) {
foreach ($users->result() as $row) {
echo $row->username;
}
} else {
echo 'No users';
}
Actually there is another possibility you can use
create a Team_model and an User_model
The Team_model could look like
class Team_model extends CI_Model
{
$arrTeamNames = [];
public function getTeamName($team_id)
{
if (!isset($this->arrTeamNames[$team_id]))
{
$query = $this->db
->select('name')
->from('team')
->where('team_id',$this->team_id)
->get();
if ($query->num_rows() == 1)
{
$objTeam = $query->row();
$this->arrTeamNames[$team_id] = $objTeam->name;
return $objTeam->name;
}
return '';
}
return $this->arrTeamNames[$team_id];
}
}
and the User_model
class User_Model extends CI_Model
{
public function getUser($id)
{
$query = $this->db->get_where('users', array('user_id' => $id));
if ($query->num_rows() == 1)
{
$user = $query->row(0, 'User_Object');
return $user;
}
}
}
class User_Object
{
public function getTeamName()
{
$ci = get_instance();
$ci->load->model('Team_model');
return $ci->team_model->getTeamName($this->team_id);
}
}
Note in the User_model you have an additional User_Object which
gets mapped by the row() function which is well documented
here.
Now if you call this model in one of your controller and pass it to your view you actually can access to a model function in a proper way e.g.
class User extends CI_Controller
{
public function detail($user_id)
{
$this->load->model('User_model');
$objUser = $this->User_model->getUser($user_id);
$this->load->view('user-detail', ['objUser' => $objUser]);
}
}
//user-detail view
echo $objUser->getTeamName();

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>

How to call Base Controllers Method in Codeigniter?

I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}

model in codeigniter won't recognize id number

So here is the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: idnum
Filename: models/model_teacher.php
Line Number: 8
this is my controller:
public function teacher(){
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard();
$this->load->view('teacher/teacher', $data);
}
and my model:
class Model_teacher extends CI_Model {
public function scoreboard() {
$this->db->where('login_id', $this->input->post('idnum'));
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
}
and the view:
<?php
foreach ($result as $row) {
echo $row->login_id."<br>";
echo $row->lname."<br>";
}
?>
and don't know what is wrong with this code. please bear with me, i'm still a newbie at using codeigniter. thanks guys.
Change as following:
In Controller
public function teacher(){
$this->load->model('model_teacher');
$idNum = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($idNum);
$this->load->view('teacher/teacher', $data);
}
In Model
public function scoreboard($idNum) {
$this->db->where('login_id', $idNum);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
you can't get directly in model you have to first get in controller
Controller
public function teacher(){
$id = $this->input->post('idnum');
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard($id);
$this->load->view('teacher/teacher', $data);
}
Model`
public function scoreboard($idnum) {
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}`
This is the code you can use
Would be better if you pass it from controller to model
$id = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($id);
and model:-
public function scoreboard($id) {
//$this->db->where('login_id', $id);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$id."'");
return $query->result();
}
Also you haven't define anywhere $idnum so notices comes up

Categories