Not Sure How to call Store Procedure in Codeigniter - php

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

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 JOIN multiple tables

I'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}

I am having an issue with my order by in codeigniter

For some reason when I do order by Date_Scheduled desc it is still leaving the oldest date first. I am trying to get it so that the newest date shows first and oldest date last. Not sure is I am writing this correctly.
Here is my model it is the get employee function.
<?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->select('*');
$this->db->from('employee_list');
$this->db->where('status',1);
$this->db->order_by ("Date_Scheduled", "desc");
$this->db->order_by("Scheduled_Area", "desc");
$this->db->order_by("Time_Reported", "desc");
$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);
}
}
This answer solves part of your problem, because you should handle dates with the correct column type date and not varchar.
If the result of your query always returns ORDER BY ASC, you can flip the array.
array_reverse($this->Employee_model->get_employee());

Codeigniter testing - call to a member function on a non object

I am trying to test my codeigniter system using simpletest, however I keep getting the error. I am very new to PHP and codeigniter so please go easy.
Fatal error: Call to a member function add() on a non-object in C:\xampp\htdocs\di2\application\tests\simple_test.php on line 35
This is my test file, at the moment I am trying to simply add a new user to the database.
require_once BASEPATH."core/Model.php";
require_once APPPATH."models/Users_model.php";
class ExampleTest extends UnitTestCase
{
function ExampleTest()
{
parent::__construct('Example tests');
}
///// All functions starting with "test" will be tested /////
function testSimpleStuff()
{
$name = 'Andreas';
$online = TRUE;
$this->assertEqual($name, 'Andreas');
$this->assertTrue($online);
}
//There are already 8 members in the table
function testAdd()
{
$this->model->add("new member");
$this->assertEqual($this->model->get(9),'new member');
$this->assertNull($this->model->get(0));
$this->assertNull($this->model->get(10));
}
}
This is the model I am calling:
class Users_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
//Used to retrieve all of the members from the database and list them by their id in descending order
function get($id = null)
{
$this->db->select('id, first_name, last_name, dob, email_address, phone_number, address_line_1, address_line_2, town, county, postcode, student, volunteer');
$this->db->from('Users');
if (!is_null($id)) $this->db->where('id', $id);
$this->db->order_by('id', 'desc');
return $this->db->get()->result();
}
function add($data)
{
$this->db->insert('Users', $data);
return $this->db->insert_id();
}
function update($data, $id)
{
$this->db->where('id', $id);
$this->db->update('Users', $data);
return $this->db->affected_rows();
}
function delete($id)
{
$this->db->where('id', $id);
$this->db->delete('Users');
return $this->db->affected_rows();
}
function filterUsers()
{
return $this->db->get('Users')->result();
}
}
Can anyone please tell me why I am getting this error, and how I can go about fixing it. Cheers in advance.
This is not the right way to call a model in codeigniter.
$this->model->add("new member");
It should be-
$this->load->model('Users_model');
$this->Users_model->add('new member');
This is because, in CodeIgntier, you need to include your model explicitly (we can also do it in autoload.php).
In your controller's constructor
function ExampleTest()
{
parent::__construct('Example tests');
$this->load->model('Users_model');
}
Now, call it from your controller like:
$this->Users_model->add("new member");

update data in codeigniter

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

Categories