Rows into columns and / or crosstabbing array in php - php

I have this billing table from wherein I get records as per the report requirement.
The array I get is like this:
Array(
[0] => stdClass Object
(
[bid] => 3
[uid] => 2
[total_inc] => 100
[total_exp] => 55
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[1] => stdClass Object
(
[bid] => 2
[uid] => 3
[total_inc] => 85
[total_exp] => 45
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[2] => stdClass Object
(
[bid] => 1
[uid] => 8
[total_inc] => 130
[total_exp] => 75
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[3] => stdClass Object
(
[bid] => 5
[uid] => 25
[total_inc] => 130
[total_exp] => 65
[mon] => 2
[year] => 2012
[mstdatereg] => 2012-03-14
)
[4] => stdClass Object
(
[bid] => 4
[uid] => 27
[total_inc] => 75
[total_exp] => 50
[mon] => 2
[year] => 2012
[mstdatereg] => 2012-03-14
)
[5] => stdClass Object
(
[bid] => 10
[uid] => 3
[total_inc] => 180
[total_exp] => 100
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-04-05
)
[6] => stdClass Object
(
[bid] => 6
[uid] => 12
[total_inc] => 60
[total_exp] => 35
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-03-14
)
[7] => stdClass Object
(
[bid] => 7
[uid] => 22
[total_inc] => 160
[total_exp] => 90
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-03-14
)
[8] => stdClass Object
(
[bid] => 9
[uid] => 3
[total_inc] => 115
[total_exp] => 70
[mon] => 4
[year] => 2012
[mstdatereg] => 2012-03-16
)
)
What I have done through looping is the result which is like this:
January, 2012
==========
Income Expense
2 100 55
3 85 45
8 130 75
---------------------------------
Total 315 175
February, 2012
===========
Income Expense
25 130 65
27 75 50
---------------------------------
Total 205 115
March, 2012
Income Expense
3 180 100
12 60 35
22 160 90
---------------------------------
Total 400 225
April, 2012
Income Expense
3 115 70
---------------------------------
Total 115 70
Net Total Income: 1035
Net Total Expense: 585code here
However what I want is
Sr.No Member 1, 2012 2, 2012 3, 2012 4, 2012 Total
Inc|Exp Inc|Exp Inc|Exp Inc|Exp Inc|Exp
=======================================================================
1 2 100|55 100|55
2 3 85|45 180|100 115|70 380|215
3 8 130|75 130|75
4 25 130|65 130|65
5 27 75|50 75|50
6 12 60|35 60|35
7 22 160|90 160|90
=======================================================================
Total 315|175 205|115 400|225 115|70 1035|585
Prior to posting, I searched for such problem and this is what I found: Building a "crosstab" or "pivot" table from an array in php
I tried to make it work according to my requirement but cud not do so.
Been trying for many days now. Any help will be appreciated.

I think you'll have better luck using SQL to do most of this number crunching for you.
Relatively simple process, then:
Determine the months (columns) you want to represent in the table.
Use GROUP BY to break your data into rows by Sr. No (or whatever you want to use)
Use WHERE clause to cut out data that's not in the date range you've chosen (i.e. if Report is only for Feb & March, don't include Jan)
Totals column is sum of Income or Expense for given row (remember, rows are grouped using criteria you chose earlier)
Individual month column is sum of IF(Date is in this column's month, Amount, 0)
Totals row is calculated in your PHP
You can generate the month column code programmatically.
Below's an example of some code to generate the query for a given set of month-year combos (untested). It's trivial to get it into a table from here (you just need to calculate the totals row in PHP).
$qstr = 'SELECT bid, uid, ';
$reportMonths = array(1 => 2012, 2 => 2012, 3 => 2012, 4 => 2012);
foreach ($reportMonths as $mon => $year) {
$qstr .= "SUM(IF(mon=$mon AND year=$year),total_inc,0)) as $mon-inc, SUM(IF(mon=$mon AND year=$year,total_exp,0)) as $mon-exp, ";
}
$qstr .= 'SUM(total_inc) as total-inc, SUM(total_exp) as total-exp from bill_mst WHERE ';
foreach ($reportMonths as $mon => $year) {
$qstr .= '(mon=$mon AND year=$year) OR ';
}
// chop off last or
$qstr = substr($qstr, 0, -3);
$qstr .= 'GROUP BY bid, uid ASC';
$res = mysql_query($qstr);

Related

group same name from array and show total of an indivisual group

I have an array which look like this:
Array
(
[0] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 500
[amount] => 50000
)
[1] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 0
[product_ordered_quantity] => 45
[others_feature] =>
[rate] => 45
[amount] => 2025
)
[2] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 2
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 850
[amount] => 85000
)
.......
Now I want to group the same product (product will be identified by combination of products_name, measurement and products_type) to show one product at a time and make total of "product_ordered_quantity" of the same product. See the image you may understand what I am trying to say. I will generate PDF file so no javascript please.
result:
I want:
If this is a query from the DB you can use some methods provided by the Collection class (https://laravel.com/docs/5.4/eloquent-collections)
$query; //The query object BEFORE getting the data, so before get() or paginate()
$data = $query->selectRaw('sum(product_ordered_quantity) as quantity')
->groupBy('products_name','measurement', 'products_type')
->get();
You can give a try to this beautiful class, which will let you query to your array same as you query to the database - https://phplinq.codeplex.com/

How to sort multikey in php? [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
Array ( [0] => stdClass Object ( [Id] => 18
[AccNo] => 1
[Title] => Hardware
[Description] => Mobile.
[ManuDate] => 8th July 1942
[MusCat] => Album
[month] => 7
[date] => 8 )
[1] => stdClass Object ( [Id] => 20
[AccNo] => 2
[Title] => Food
[Description] => Apple.
[ManuDate] => 27th July 1942
[MusCat] => Album
[month] => 7
[date] => 27 )
[2] => stdClass Object ( [Id] => 24
[AccNo] => 3
[Title] => Hardware
[Description] => Computer.
[ManuDate] => 2nd July 1942
[MusCat] => Album
[month] => 7
[date] => 2 )
[3] => stdClass Object ( [Id] => 56
[AccNo] => 4
[Title] => Hardware
[Description] => Printer
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 )
[4] => stdClass Object ( [Id] => 105
[AccNo] => 5
[Title] => Object
[Description] => Chair.
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 ) )
This is my array input Like
Id Date Title Description
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
2 2nd July 1942 Hardware Computer
3 1942 Hardware Printer
4 1942 Object Chair
I want output like
Id Date Title Description
************************************************************
3 1942 Hardware Printer
4 1942 Object Chair
2 2nd July 1942 Hardware Computer
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
How to sort multikey in php?
I am beginner in Php. I am using following code in php but out put will not correctly. If any one of datewise or monthwise sort, Output will come correctly. otherwise both (datewise or monthwise), Output will not come correctly. Plz help any solution.
usort($value['year'], function ($a, $b) {
if ($a->date == $b->date) return 0;
return $a->date < $b->date ? -1 : 1;
});
usort($value['year'], function ($a, $b) {
if ($a->month == $b->month) return 0;
return $a->month < $b->month ? -1 : 1;
});
The following code does the job. strtotime parses text date into a Unix timestamp.
usort($array, function($v1, $v2) {
$d1 = strlen($v1->ManuDate) === 4 ? '01-01-' . $v1->ManuDate : $v1->ManuDate;
$d2 = strlen($v2->ManuDate) === 4 ? '01-01-' . $v2->ManuDate : $v2->ManuDate;
return strtotime($d1) - strtotime($d2);
});

Array in php codeigniter framework

I am having problem with the array order problem, array coming from the database is correct but when i do print_r($data) each and every time i refresh the pages the order of the array change.
Array
(
[0] => stdClass Object
(
[prodtmappid] => 53624
[totalrating] => 5.00
[SKUNO] => C46P53624R7621
[ProId] => 31369
[CatId] => 46
[proQnty] => 15
[productMRP] => 1299
[ProDisPrice] => 0
[SellingPrice] => 390
[AdditionDiscount] => 0
[FinalPrice] => 390
[ProDiscount] => 70
[ProDisType] => percentage
[ProName] => Trendy Bandey Men TB20 Yellow Tshirt
[TotalValues] => 0.00
[image] => MCTSTB-TB20-YL_1_55x_885.JPG
[BrandId] => 4233
[FDisId] => 7
[ColorID] => 191
[size_Val] => M
)
[1] => stdClass Object
(
[prodtmappid] => 9526
[totalrating] => 0.00
[SKUNO] => C46P9526R1870
[ProId] => 3351
[CatId] => 46
[proQnty] => 8
[productMRP] => 1899
[ProDisPrice] => 0
[SellingPrice] => 1329
[AdditionDiscount] => 0
[FinalPrice] => 1329
[ProDiscount] => 30
[ProDisType] => percentage
[ProName] => FCUK 56DPJ Pink Men T-Shirt
[TotalValues] => 0.00
[image] => FCUK2_1_28x_885.JPG
[BrandId] => 1285
[FDisId] => 4
[ColorID] => 194
[size_Val] => S
)
so what problem i am facing is array[0]['prodtmappid'] will have diffrent values on every page refress.
What exactly is your query? From the results, it looks like it's ORDER BY prodQnty DESC and GROUP BY catId. If you want to keep the results consistent, add ORDER BY prodtmappid to your query.

how to count common values ina array

Array (
[attendance_id] => 18
[attendance] => 1
[student_id] => 1
[date] => 2015-01-19
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 15000 )
Array (
[attendance_id] => 19
[attendance] => 1
[student_id] => 2
[date] => 2015-01-19
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 2000 )
Array (
[attendance_id] => 20
[attendance] => 0
[student_id] => 1
[date] => 2014-01-15
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 0
)
I want to count the common values in these Arrays. Please guide how to do this.
As result I want to get this:
date [2014-01-15 ] => 2
attendance [1] => 2
This is actually for a small institute where single student details for a day may entered into the system as above. So I just want to make a report day end saying how many students has come to the class on specific day and how many didn't.
Try this..
<?php
$array = array(array('18','2015-01-19','15000'),array('2015-01-19','18','22'),array('20','11','22'));
$newcountarray = array();
foreach ($array as $key=>$value) {
foreach ($value as $newvalue) {
if ($foundKey = array_key_exists($newvalue,$newcountarray)) {
$newcountarray[$newvalue] += 1;
}
else{
$newcountarray[$newvalue] = 1;
}
}}
print_r($newcountarray);
?>
Output
Array ( [18] => 2 [2015-01-19] => 2 [15000] => 1 [22] => 2 [20] => 1 [11] => 1 )
Yes Deena Let's say
$array = array(array('18',<==row id no
'2015-01-19',<=date
'1',<=student id>
'2'<=present(1),absent(2))),
array(array('19',<==row id no
'2015-01-19',<=date
'2',<=student id>
'1'<=present(1),absent(2))),
its like for the day 2015-01-19 i want to get all 2 stuudent and among them 1 Student is present and 1 is absent

Call to a member function set() on a non-object error looping through array

Working in CI 2.1.0 and getting a "Call to a member function set() on a non-object..." error with the code excerpt looping through the array below. Seemingly it's an array of all strings so I'm having trouble seeing why it seems to fail in the middle.
Any insights as to what's going on would be great. Researched other questions on this error, but still at a loss.
Code excerpt:
foreach($data['jobs'] as $job)
{
foreach($job as $key=>$value){
$job->set($key,$value);
}
}
$data['jobs'] array:
Array ( [0] => Array ( [job_id] => 149 [company] => Minnesota Life Insurance [location] => Barrington, IL [start_date] => March '98 [end_date] => June '98 [description] =>
Primary responsibility was research and design of sales and training presentations.
Responsible for scheduling a three person sales and training staff.
[title] => Administrative Secretary [resume_id] => 96 [order_id] => 0 [profile_id] => 38 [user_id] => 1 [vanity_name] => Sample of Template 3 [template_id] => 3 [date_add] => 0000-00-00 00:00:00 [date_mod] => 2012-03-04 11:00:05 ) [1] => Array ( [job_id] => 150 [company] => Manpower Temporary Services [location] => Naperville, IL [start_date] => Dec' 04 [end_date] => June '98 [description] =>
Assigned to GE Silicones in the industrial sales division.
Responsible for analysis of monthly, weekly, and, daily sales reports.
Responsible for scheduling training classes, seminars, and conferences
[title] => Independent Contractor [resume_id] => 96 [order_id] => 1 [profile_id] => 38 [user_id] => 1 [vanity_name] => Sample of Template 3 [template_id] => 3 [date_add] => 0000-00-00 00:00:00 [date_mod] => 2012-03-04 11:00:05 ) [2] => Array ( [job_id] => 151 [company] => KSMR Radio 92.5/94.3FM [location] => Winona, MN [start_date] => May '96 [end_date] => May '97 [description] =>
In charge of a 36 member staff, as well as a 7 member management team, with duties that include disciplinary actions, budgeting, special requests, program scheduling, and hiring.
Balancing a $15,000 dollar budget, as well as chairing a committee to receive a grant for increasing station amenities such as wattage, equipment, space, and music library.
[title] => General Manager [resume_id] => 96 [order_id] => 2 [profile_id] => 38 [user_id] => 1 [vanity_name] => Sample of Template 3 [template_id] => 3 [date_add] => 0000-00-00 00:00:00 [date_mod] => 2012-03-04 11:00:05 ) )
It's because $job isn't a class, thats why your getting the 'call to a member function set() on a non-object' - $job is an array. You have to access it like the following.
$job['job_id'] = 149;
$job['company'] = 'Minnesota Life Insurance';
Maybe you want to transfer the values of $job into a class?

Categories