CodeIgniter: get data after each 'foreach()' command - php

$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'] = $query->result();
foreach($subjects as $s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'] = $query->result();
}
}
I want to print tables of Courses having top first row with single column with data $c->course
and bellow rows with two column having data $s->subject and $t->test respectively...

you can use like this for first row for courses and next two rows for subjects and tests
$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $key=>$c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'][$key] = $query->result();
foreach($subjects as $key=>$s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'][$key] = $query->result();
}
}

Related

CodeIgniter 3 - joint data from another table

Original function:
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$query = $this->db->get('orders');
return $query->result_array();
}
}
I need to JOIN data from table order_shipping and order_products where order_id = order_id
I do:
/*
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
This above code working correct. But currently JOIN only after else. I need do the same way before else:
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
But this code before else not working correct for me and currently display wrong data. When I try fetch all orders, then second function display correct all orders. But when I try fetch example only order ID 2, then is used first function and display wrong data, example when I fetch order ID2, then always display only order with ID 1.
This function is for REST API fetch orders.

return same result at multiple time in code igniter

[i want to show specific award data for specific employee but it reutene same result at multipale time
model function is here
public function get_all_awards($id = NULL) {
$this->db->select('tbl_employee_award.*', FALSE);
$this->db->select('tbl_employee.*', FALSE);
$this->db->select('tbl_designations.*', FALSE);
$this->db->select('tbl_department.department_name', FALSE);
$this->db->from('tbl_employee_award');
$this->db->join('tbl_employee', 'tbl_employee_award.employee_id = tbl_employee.employee_id', 'left');
$this->db->join('tbl_designations', 'tbl_designations.designations_id = tbl_employee.designations_id', 'left');
$this->db->join('tbl_department', 'tbl_department.department_id = tbl_designations.department_id', 'left');
$this->db->order_by("award_date", "desc");
if (!empty($id)) {
// var_dump($id);
//die();
$this->db->where('tbl_employee_award.employee_id',$id);
$query_result = $this->db->get();
$result = $query_result->row();
} else {
$query_result = $this->db->get();
$result = $query_result->result();
}
// var_dump( $result->employee_id);
// die();
return $result;
}][1]
You have to set your base table as Employee table. Because you want data according to Employees not Awards.
public function get_all_awards($id = NULL)
{
$this->db->select("E.id, E.employee_name, A.award_names, DES.designation, D.department_name");
$this->db->from('tbl_employee as E');
$this->db->join('tbl_employee_award as A', 'A.employee_id = E.employee_id', 'left');
$this->db->join('tbl_designations as DES', 'DES.designations_id = E.designations_id', 'left');
$this->db->join('tbl_department as D', 'D.department_id = E.department_id', 'left');
$this->db->order_by("A.award_date", "desc");
if($id !== NULL)
{
$this->db->where('E.employee_id', $id);
}
$query = $this->db->get();
$result = $query->result();
print_r($result);
die;
}
In above query, if you have single award for an employee then you will get single row in result. If an employee won multiple awards then it will return multiple rows for same employee but for different awards. If any employee won an award twice then also you get record twice.

if condition with where clause

I just want to do this if(status==0) then only where condition is run and print only related to status==0 record.And when status==1 come then it bypass the where clause and print both data for status 0 and 1. I don't know how to build a query for this. Please help me in this.
my model code
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->where('student_info.admin_id',$admin_id)
->join('admin','admin.admin_id = student_info.admin_id')
->get();
return $query->result();
}
This code work properly and print separate data whose status 0 or 1.
Only admin_id = 1 contain Status = 1, other id's contain 0 status.
Replace your function with the following code...
<?php
function listing($status)
{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
if($status==0)
{
$this->db->where('student_info.admin_id',$admin_id);
}
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
?>
Try this:
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->join('admin','admin.admin_id = student_info.admin_id')
->where("student_info.admin_id = IF (admin.status = 0, $admin_id, '>-1'")
->get();
return $query->result();
}
It solves my problem..
function listing()
{
$admin_id = $this->session->userdata('admin_id');
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->or_where('admin.status < 1');
$query = $this->db->get();
return $query->result();
}
<?php
function listing($status)
{
if($status == 0){
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
}
?>

Codeigniter cannot get data from mysql table

I get following error while trying to get data from my database:
Error Number: 1066
Not unique table/alias: 'faq'
SELECT *FROM (faq, faq)WHERE faq_title = 'title 1'
Please help me to find my mistake. Here is my model:
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get('faq');
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
In your query table name is called two times. This is unnecessary.
Just replace $query = $this->db->get('faq'); to $query = $this->db->get();
the bold one is correct.
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}

I would like to have an where in statement before the select_sum statement

I would like to have an where in statement before the select_sum statement.
Because i would like to have different sums based on the $project. Now it is just counting all the array variables as a sum.
Im doing this in codeigniter.
model:
function rapport_detail_opbrengsten($idKlant){
$this->db->from('Project');
$this->db->join('Opbrengsten', 'Opbrengsten.idProject = Project.idProject');
if ($idKlant > 0){
$this->db->where('idKlant', $idKlant);}
$query = $this->db->get();
$project = array();
foreach($query->result() as $row){
$project[] = $row->idProject;
}
$this->db->select_sum('Prijs');
$this->db->from('Opbrengsten');
$this->db->where_in('idProject', $project);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
else{
return false;
}
}
select_sum groups all your rows .. so there will be no effect if you manually group the rows by other criteria. Try something like this
$this->db->select('idProject, SUM(Prijs) as total');
$this->db->from('Opbrengsten');
$this->db->where_in('idProject', $project);
$this->db->group_by('idProject');
$query = $this->db->get();

Categories