About Join in codeigniter - codeigniter

I have four table given below
first one
enter image description here
2nd is retration table
filds st_name, Adhar no, address, exam_level
trans temp table
hire student adhar no. is there and amount detail for payment
fields are
id, student_id, reference_no, sub_merchant_id, amount, student_name, mobile_no, father_name, clas, uid, exam_lavel
last one is transaction success table
i have used to join command
public function show_payment_modal()
{
$this->db->select('*');
$this->db->from('student_login');
$this->db->where('stauts'=='active');
$this->db->join('student_registration', 'student_login.student_id =
student_registration.id', 'left');
$this->db->join('stud_trans_temp',
'stud_trans_temp.student_id=student_registration.id', 'left');
$this->db->join('stud_trans',
'stud_trans.referenceno=stud_trans_temp.reference_no', 'left');
$this->db->where('student_login.status', 'active');
$query = $this->db->get();
return $query->result();
}
in stud_trans i have two payment mode cash and success
i want to cash mode that depends upon attached image exam_level

Try some changes in your code marked in.
$this->db->select('*');
$this->db->from('student_login');
$this->db->where('stauts', 'active');
$this->db->join('student_registration', 'student_login.student_id =
student_registration.id', 'left');
$this->db->join('stud_trans_temp',
'stud_trans_temp.student_id=student_registration.id', 'left');
$this->db->join('stud_trans',
'stud_trans.referenceno=stud_trans_temp.reference_no', 'left');
$this->db->where('student_login.status', 'active');
$query = $this->db->get();
return $query->result_array();

Clean up code some
//might need to specify what you want since you have a lot of joins ex. student_registration.id as reg_id
$this->db->select('*');
//can make aliases
$this->db->join('student_registration sr', 'sl.student_id =
sr.id', 'left');
$this->db->join('stud_trans_temp',
'stud_trans_temp.student_id = sr.id', 'left');
$this->db->join('stud_trans',
'stud_trans.referenceno=stud_trans_temp.reference_no', 'left');
$this->db->where('stauts', 'active');
$this->db->where('student_login.status', 'active');
//can make this one line or chain them all together
return $this->db->get('student_login sl')->result_array();

Related

LEFT JOIN model codeigniter

I want get field on msph_pl from ms_prod_harga
this is my model promo
is this correct ?
$this->db->select('*');
$this->db->from('md_promo');
$this->db->join('ms_promo','mspr_no = mdpr_mspd_no','left');
$this->db->join('ms_produk','mspd_no = mspr_no','left');
$this->db->join('ms_prod_harga', 'mspr_no = msph_no','left');
$this->db->where('mdpr_no',$mspr_no);
If your tables looks like this:
(table) md_promo
(value) mspr_no
(table) ms_produk
(value) mspd_no
(table) ms_prod_harga
(value) msph_no
And if you need to join result on msph_pl (value) from ms_prod_harga (table), then this code should work:
$this->db->select('*');
$this->db->from('md_promo');
$this->db->join('ms_prod_harga', 'md_promo.mspr_no = ms_prod_harga.msph_no', 'left');
$this->db->join('ms_produk', 'md_promo.mspr_no = ms_produk.mspd_no', 'left');
$query = $this->db->get();
echo "<pre>" . var_export($query->result_array(), true) . "</pre>"; // display the result_array in elegant form
Hope that helps, though, it may be not what you really need..

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

fetch data where column not null not working

try to fetch data by CodeIgniter active record where field in not empty
$this->db->select('tags');
$this->db->where('tags IS NOT NULL', null, false);
$this->db->from('users');
$query = $this->db->get();
return $query->result();
not working.
Try this may be this will help you
$this->db->select('tags');
$this->db->where('tags IS NOT NULL');
$this->db->from('users');
$query = $this->db->get();
return $query->result();

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 select_max and return other row fields

I'm trying to select the max bid amount on a particular item and return more information from the row that the max bid is found in.
At the moment I have
$item_id = $this->input->post('item_id');
$this->db->from('bids');
$this->db->where('item_id', $item_id);
$this->db->where('outbid_alert', '1');
$this->db->select_max('bid_amount');
$query = $this->db->get();
return $query->result();
This returns the max bid for the item and that's as far as I have gotten. What's the best way to get the rest of the fields from that row? Run another query or use a subquery?
Thanks!
If you want to return the fields from the row with the highest bid_amount, just ORDER BY bid_amount and select only the first row.
$item_id = $this->input->post('item_id');
$this->db->from('bids');
$this->db->where('item_id', $item_id);
$this->db->where('outbid_alert', '1');
$this->db->select('*');
$this->db->order_by('bid_amount', 'DESC');
$this->db->limit(1);
$query = $this->db->get();
return $query->result();

Resources