how to join columns in codeigniter - codeigniter

login(id, name, role,user_id)
teacher(teacher_id)
I need to check role is 2.
$this->db->select('*');
$this->db->from('teacher');
$this->db->where('teacher_branch_id',$this->session->userdata('branch_id'));
$this->db->join('login','teacher.teacher_id = login.login_user_id');
$query = $this->db->get();

Just add a where condition like this: $this->db->where('login.role', 2);
$this->db->select('*');
$this->db->from('teacher');
$this->db->join('login', 'teacher.teacher_id = login.login_user_id');
$this->db->where('teacher_branch_id', $this->session->userdata('branch_id'));
$this->db->where('login.role', 2);
$query = $this->db->get();

Related

I am working on project and my rsult query give a multiple records but i want to each record at one time only, i used inner join into this query?

$this->db
->select(array('courses.Course_Name','teacher.First_Name','teacher.Last_Name','teacher.Reg_Id','teacher_assignment.Assignment_Id','Assignment_Url','assignment.Solution_Url','assignment.Status','courses.Course_Id'));
$this->db
->from(array('student_registration'));
$this->db
->join('courses','student_registration.Course_Id=courses.Course_Id','inner');
$this->db
->Join('teacher_assignment','teacher_assignment.Course_Id=courses.Course_Id','inner');
$this->db
->Join('assignment','assignment.OEN_Number = student_registration.OEN_Number','teacher_assignment.Reg_Id = assignment.Reg_Id','inner');
$this->db
->Join('teacher','teacher.Reg_Id = assignment.Reg_Id','inner');
$this->db
->where(array('student_registration.OEN_Number'=> $uid));
$query = $this->db->get();

How to convert this SQL Query on Code Igniter Model

Good Day Masters,
Can anyone help me how to convert this SQL Query into Code Igniter format (model).
SELECT firstName, FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) as age FROM residents_tbl WHERE FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) >= 18
I don't know how to write it on WHERE clause.
$query = $this->db->select('*');
$query = $this->db->from('residents_tbl');
**$query = $this->db->where('isHead', '1');**
$query = $this->db->order_by('lastName', 'ASC');
$query = $this->db->get('', 15, $this->uri->segment(3));
if ($query->num_rows() > 0) {
return $query->result();
}
TIA.
This is a simplified version with chaining. I just changed the type of 1 from string to number which might caused the problem.
$query = $this->db
->where('isHead', 1)
->get('residents_tbl')
->order_by('lastName', 'ASC');

Codeigniter db_where in 3 joined tables

So this code is a combination of 3 table joined, now i want to filter the query where only the request that a user can have is his/her own request and not from other user.. the problem is in where clause i have duplicated.
$this->db->order_by('loanrequest.ApplicationNo', 'DESC');
$query = $this->db->get('loanrequest','loanapplication','user');
$this->db->join('loanapplication','loanrequest.ApplicationNo = loanapplication.ApplicationNo');
$this->db->join('user', 'user.userId = loanapplication.userId');
$this->db->where('loanapplication.userId', $this->session->userdata('userId'));
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
return $query->result_array();
I can not get what you have done in your join query but Here is how I do join in my CodeIgniter model.
You don't need to put this in where condition:
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
Because you have already done that in the join query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC')
->get();
return $query->result_array();
I will recommend you should use $this->db->last_query() because it gives you a better idea about your query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC');
$result = $query->get();
echo $this->db->last_query();exit; //use this to print your query so that you can get the actual issue
if ($result->num_rows() > 0) {
return $result->result_array();
} else
return '';

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

codeigniter multi joins

Hello i have this join, but i wanna join another tabel named "users" and i want the "profil_billed" row, how can i do that
i mean "WHERE forum_traad.brugernavn = users.profil_billed" something like that
function posts($id)
{
$this->db->select('*,forum_traad.indhold as traad_indhold,
forum_kommentare.indhold as kommentare_indhold,
forum_traad.brugernavn as traad_brugernavn,
forum_traad.id as traad_id
');
$this->db->from('forum_traad');
$this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad', 'forum_traad.id');
$this->db->where('forum_traad.id', $id);
$query = $this->db->get();
if($query->num_rows > 0)
{
return $query->row();
} else {
return false;
}
}
You can write your query as below -
$this->db->select('*,forum_traad.indhold as traad_indhold,
forum_kommentare.indhold as kommentare_indhold,
forum_traad.brugernavn as traad_brugernavn,
forum_traad.id as traad_id, users.profil_billed as billed
');
$this->db->from('forum_traad');
$this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad = forum_traad.id');
$this->db->join('users', 'forum_traad.brugernavn = users.profil_billed');
$this->db->where('forum_traad.id', $id);
$query = $this->db->get();
For detailed documentation of the syntax i have used refer below -
http://codeigniter.com/user_guide/database/active_record.html

Resources