model in codeigniter won't recognize id number - php

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

Related

Codeigniter Model returning empty data

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

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

multiple model not load in same controller in codeigniter

i want to run multiple model in same controller but its show below error
Severity: Notice
Message: Undefined property: Home::$Company
Filename: controllers/Home.php
Line Number: 23
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct ();
$this->load->model('Product');
$this->load->model('Company');
//$this->load->model(array('Product','Company'));
}
public function index()
{
$Product = $this->Product->getProduct(10);
if($Product)
{
$data['Product'] = $Product;
}
$Company = $this->Company->getCompany(10);
if($Company)
{
$data['Company'] = $Company;
}
$this->load->view('Home',$data);
}
}
//Company Model
class Company extends CI_Controller {
function getCompany($limit=null,$start=0)
{
$data = array();
$query = $this->db->query('SELECT * FROM user as u,category as c,sub_category as sc WHERE c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
$res = $query->result();
return $res;
}
function getOneCompany($id)
{
$this->db->where('User_Id',$id);
$query = $this->db->get('user');
$res = $query->result();
return $res;
}
}
// Product model
class Product extends CI_Controller {
function getProduct($limit=null,$start=0)
{
$data = array();
$query = $this->db->query('SELECT * FROM product as p,user as u,category as c,sub_category as sc WHERE p._User_Id = u.User_Id and c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
$res = $query->result();
return $res;
}
function getOneProduct($id)
{
$this->db->where('User_Id',$id);
$query = $this->db->get('user');
$res = $query->result();
return $res;
}
}
Your models start with
class Company extends CI_Controller {
They should be
class Company extends CI_Model {
Try changing
$this->load->model('Product');
$this->load->model('Company');
to
$this->load->model('product');
$this->load->model('company');
notice the lower case model names. Just tried it here and got the same error as you.
$models = array(
'menu_model' => 'mmodel',
'user_model' => 'umodel',
'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
$this->load->model($file, $object_name);
}
Try giving a name to you model :
$this->load->model('Company', 'Company');

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.

Categories