update data in codeigniter - php

my code update is not working. i have some code in controller
function update($id) {
$this->load->model("Model_mahasiswa");
$data['nim']=$_POST['nim'];
$data['nama']=$_POST['nama'];
$data['alamat']=$_POST['alamat'];
$result=$this->Model_mahasiswa->edit($id, $data);
if($result){
header("location: http://localhost/si_akademik/index.php/mahasiswa/");
}
}
and in modal code
public function edit($id, $data)
{
$this->db->where('id', $id);
$result = $this->db->update('mahasiswa', $data);
return $result;
}
but error: Call to a member function edit() on a non-object
can you help me solve this problem?

Modify you model function :
public function edit($id, $data)
{
$this->load->database();
$this->db->where('id', $id);
$result = $this->db->update('mahasiswa', $data);
return $result;
}

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>

Call to a member function updat_data() on null

I have got this problem, I tried many ways but I couldnt reach a soulution.
this is my main.php code for updation it edits the data but doesnot insert it to update . null error is in updat_data() function. Please help. I am confused.
function get_data($id){
$this->index();
$data['ed']= $this->main_model->get_data($id);
$this->load->view('edit',$data);
}
public function update_data()
{
$id = $this->input->post('id');
$data= array('first_name' =>$this->input->post('first_name') , 'last_name' =>$this->input->post('last_name') );
$this->main_model->updat_data($id,$data);
$this->load->model("main_model");
}
this is model.php code for update..
function get_data($id)
{
//$this->db->where("id", $id);
/// $this->db->update("user",$data);
$this->db->where("id",$id);
$query=$this->db->get("user");
return $query->result();
}
function updat_data($id,$data)
{
$this->db->where('id',$id);
$this->db->update('user',$data);
//return $query->result();
}
try to load the model before you use it in main.php

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

Not Sure How to call Store Procedure in Codeigniter

I am having an issue calling a stored procedure in my model not sure if I am doing it right the screen is coming up blank, also do i need to auto load anything to call a stored procedure?
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return $this->db->insert_id();
}
public function get_employee()
{
$this->db->query("call {storedprocedure Select_employeelist} ");
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
}
To call a procedure from codeigniter
Consider test_proc as your procedure. Then it should be
$this->db->query("call test_proc()");
If you need to to send parameters then
$query = $this->db->query($test_proc,array('id'=>'1','name'=>'test','address'=>'abc'));
To see the result then
print_r($query);

Categories