I am using three tables and one query in mysql in order to retrieve a particular user.
I am getting exactly what I want from the query, but it's not formatted in the right way.
My query:
public function get_all_payslips()
{
$empid = $this->session->userdata('EMPLOYEE_ID');
$orgid = $this->session->userdata('CURRENT_ORG_ID');
$where = "EMPLOYEE_ID ='".$empid."' ";
$response = array();
$queryString = "SELECT
em.EMPLOYEE_ID,
em.EMPLOYEE_NAME
FROM
uk_new_salary_slip assd,
employee_master em
WHERE
em.".$where."
GROUP BY em.EMPLOYEE_ID
order by em.EMPLOYEE_NAME asc";
$query = $this->db->query($queryString);
foreach ($query->result() as $data)
{
$result = array();
$result['EMPLOYEE_NAME'] = $data-> EMPLOYEE_NAME;
$queryString = "SELECT
mo.months,
MONTH((assd.pay_period)) as monthss,
YEAR((assd.pay_period)) as PAY_YEAR,
GROUP_CONCAT(DISTINCT(assd.id)) as action,
GROUP_CONCAT(DISTINCT(assd.ORG_ID)) as org
FROM
uk_new_salary_slip assd,
employee_master em,
months mo
WHERE
assd.emp_id = ". $data->EMPLOYEE_ID ."
AND mo.id = MONTH((assd.pay_period))
GROUP BY monthss,PAY_YEAR
order by PAY_YEAR desc";
$queryString1 = "SELECT
mo.months,
MONTH((germany.pay_from)) as monthss,
YEAR((germany.pay_from)) as PAY_YEAR,
GROUP_CONCAT(DISTINCT(germany.id)) as action,
GROUP_CONCAT(DISTINCT(germany.ORG_ID)) as org
FROM
new_germany_salary_slip germany,
employee_master em,
months mo
WHERE
germany.emp_id = ". $data->EMPLOYEE_ID ."
AND mo.id = MONTH((germany.pay_from))
GROUP BY monthss,PAY_YEAR
order by PAY_YEAR desc";
$queryString2 = "SELECT
mo.months,
MONTH((poland.pay_from)) as monthss,
YEAR((poland.pay_from)) as PAY_YEAR,
GROUP_CONCAT(DISTINCT(poland.id)) as action,
GROUP_CONCAT(DISTINCT(poland.ORG_ID)) as org
FROM
new_poland_salary_slip poland,
employee_master em,
months mo
WHERE
poland.emp_id = ". $data->EMPLOYEE_ID ."
AND mo.id = MONTH((poland.pay_from))
GROUP BY monthss,PAY_YEAR
order by PAY_YEAR desc";
$query1 = $this->db->query($queryString);
$query2 = $this->db->query($queryString1);
$query3 = $this->db->query($queryString2);
$children = array();
foreach ($query1->result() as $data1)
{
$yearArray = array();
$yearArray['month'] = $data1->months;
$yearArray['year'] = $data1->PAY_YEAR;
$yearArray['org'] = $data1->org;
$yearArray['action'] = $data1->action;
array_push($children, $yearArray);
}
foreach ($query2->result() as $data2)
{
$yearArray = array();
$yearArray['month'] = $data2->months;
$yearArray['year'] = $data2->PAY_YEAR;
$yearArray['org'] = $data2->org;
$yearArray['action'] = $data2->action;
array_push($children, $yearArray);
}
foreach ($query3->result() as $data3)
{
$yearArray = array();
$yearArray['month'] = $data3->months;
$yearArray['year'] = $data3->PAY_YEAR;
$yearArray['org'] = $data3->org;
$yearArray['action'] = $data3->action;
array_push($children, $yearArray);
}
$result['children'] = $children;
array_push($response, $result);
}
return $response;
}
If in the month
array the year
is same then the IN action
value should be 4,2
. If you see in object 0th and 4th month and year is the same so both object's action
values should be in one row, and similarly its org
value should be org:40,47
respectively.
{month:"FEBRUARY", year:"2018",org:"40,47",action:"4,2"}
I want the data to be formatted like this but in my case it shows different rows for each action.
Array push just adds elements on to the end of the array. It is not what you want. You want to essentially compound elements of the same year and month. To do that we can assign that as the 1st sub array key and simply append the array values to org
and action
:
$output = array();
foreach ($query1->result() as $data1) {
$key = $data1->PAY_YEAR . $data1->months;
if (!isset($output[$key]['month'])) {
$output[$key]['month'] = $data1->months;
$output[$key]['year'] = $data1->PAY_YEAR;
}
$output[$key]['org'][] = $data1->org;
$output[$key]['action'][] = $data1->action;
}
foreach ($query2->result() as $data2) {
$key = $data2->PAY_YEAR . $data2->months;
if (!isset($output[$key]['month'])) {
$output[$key]['month'] = $data2->months;
$output[$key]['year'] = $data2->PAY_YEAR;
}
$output[$key]['org'][] = $data2->org;
$output[$key]['action'][] = $data2->action;
}
foreach ($query3->result() as $data3) {
$key = $data3->PAY_YEAR . $data3->months;
if (!isset($output[$key]['month'])) {
$output[$key]['month'] = $data3->months;
$output[$key]['year'] = $data3->PAY_YEAR;
}
$output[$key]['org'][] = $data3->org;
$output[$key]['action'][] = $data3->action;
}
echo '<pre>';
print_r(array_keys($output));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.