fetch data where column not null not working - codeigniter

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

Related

fetching data of joining tables in Codeigniter

can you please tell me what's wrong with my function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
}
I wanna display Two tables in one table using the foriegn key (code_filiere)
Solution 1 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query->result();
}
In the controller
$data = $this->your_model->get_by();
Solution 2 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query;
}
In the controller
$data = $this->your_model->get_by()->result();
From the doc
nothing wrong in your function, but you didn't return the captured value from the function to the controller.
You should use
$query->result() for multi-value
OR
$query->row() for single value
from database
update your function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return ($query->num_rows() > 0) ? $query->result() : false;
}

About Join in 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();

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

how to join columns in 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();

Resources