I've had around 2 hours sleep in 36 hours so I may have minor over sights here but Here is the link of my previous post to get to where I am now:
Pivot MySQL Table with Dynamic Columns
With the help of that post I was able to generate a dynamic SQL query using PDO and PHP. In comparison to my attempts earlier in manipulating the array and displaying it in a tabular format I am completely stumped in this.
Here is the query:
$sql = "
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN EXTRACT(YEAR_MONTH FROM month_payment) = ',
EXTRACT(YEAR_MONTH FROM month_payment),
' THEN AMOUNT ELSE 0 END) AS `',
EXTRACT(YEAR_MONTH FROM month_payment),
'`'
)
) AS `pivot_columns`
FROM record_payment
WHERE month_payment BETWEEN ? AND ?
";
$stmt = $pdo->prepare($sql);
$date_from = '2015-01-01';
$date_to = '2017-08-01';
$stmt->execute([$date_from, $date_to]);
$row = $stmt->fetch();
$stmt->closeCursor();
$pivot_columns = $row['pivot_columns'];
$sql = "
SELECT title AS `Payment Method`, {$pivot_columns}
FROM record_payment t1
JOIN setting_payment_method spm ON spm.id = t1.method_id
WHERE month_payment BETWEEN ? AND ?
GROUP BY title WITH ROLLUP
";
echo $sql;
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$results = $stmt->fetchAll();
$stmt->closeCursor();
var_dump($results);
Here is a vardump of my array:
array (size=3)
0 =>
array (size=9)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
1 =>
array (size=9)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
2 =>
array (size=9)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
UPDATED: This is how the dump looks after this manipulation
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
}
var_dump($temp);
BELOW
array (size=3)
'Cash' =>
array (size=7)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
'Cheque' =>
array (size=7)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
'' =>
array (size=7)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
My issue are this:
The keys of the array is generated dynamically from the user
interface. They select between 2 date fields and the query extracts
the year and month from it and sums up the values. e.g 201504 = April
2015 but an index key (0) comes from nowhere and just replicates the payment method. The Payment method should be at the top level with the date as the key and the aggregated
amount the key pair value.
I have tried manipulating the array by doing a nested loop and
creating an array from the subarray but with little result. I have
tried splicing array, slicing the array, shifting the array,
flattening the array, merging the array and many other things in order to manipulate and
format the array.
This is how the table comes out:
whilst using a nested loop and echoing the key and value.
https://ibb.co/dPYSuQ
EDITED
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach ($v as $k => $v){
echo '<th>'.$k.'</th>';
}}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach ($v as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div> </div>
Related
i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.
I have this array structure (see below) grouped by date from a database query, and I wish to find the total for each group by multiplying the quantity by the transamount.
This is what i have so far:
array (size=3)
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>
array (size=9)
0 =>
array (size=4)
'paydate' => string '2017-7-04' (length=9)
'tariff' => string ' Register' (length=21)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
The aim is to get to total of all transaction grouped by the dates.
This may not work since I'm not sure if you need to access the transactions using the date as a key... But something like this would add an element to each array item with the total.
// loop through the arrays sorted by date
foreach ($groups as $group)
{
$total = 0;
// loop through each transaction
foreach ($group as $transaction)
{
// Add to the total for each purchase group
$total += $transaction->quantity * $transaction->transamount;
}
// put the total into the group array
$group->total = $total
}
With this you should end up with something like
'total' => (calculated value),
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>
Need some help formatting this array properly:
Here is the dump of the array:
array (size=5)
'Bank Deposit' =>
array (size=13)
'Payment Method' => string 'Bank Deposit' (length=12)
0 => string 'Bank Deposit' (length=12)
201704 => string '1200' (length=4)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cash' =>
array (size=13)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '300' (length=3)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '12' (length=2)
201709 => string '12' (length=2)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cheque' =>
array (size=13)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '231' (length=3)
201709 => string '231' (length=3)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Mobile Money' =>
array (size=13)
'Payment Method' => string 'Mobile Money' (length=12)
0 => string 'Mobile Money' (length=12)
201704 => string '0' (length=1)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
'' =>
array (size=13)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '243' (length=3)
201709 => string '243' (length=3)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
QUESTION: How do I get rid of the 'Payment Method' and 0 keys in the array?
WHAT I'VE TRIED:
Table as it is: https://ibb.co/gW55g5
Splicing the value from offset 1: https://ibb.co/fogNTk
Splicing the value and key from offset 1: https://ibb.co/jgU98k
This removes the columns but then reindexes the keys!
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach (array_splice($v,1) as $k => $v){
echo '<th>'.$k.'</th>';
}
var_dump($temp);
}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach (array_splice($v , 1) as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div>
</div>
I made an earlier post but it seemed to not get to the point but if you need any extra help in getting the picture you can see a more detailed question here: Multidimensional Array Dynamic Columns Format. Thanks
<?php
$data = [
'foo' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201704' => '23',
],
'bar' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201703' => '47',
]
];
foreach($data as $k => $v) {
unset($data[$k]['unwanted']);
unset($data[$k][0]);
}
var_export($data);
Output:
array (
'foo' =>
array (
201704 => '23',
),
'bar' =>
array (
201703 => '47',
),
)
I have a function that I am writing to take in values datetime values from an array and convert it into hours and minutes format like 0010. I want to use this to later extract other values from the array as this is an associative array
function getData($lengthArray, $csvdat) { # passing down the values of the array and the length of the array
$newData = array(
'nightTime1',
'nightTime2',
'dayTime1',
'peakTime',
'dayTime2',
'winterNightTime1',
'winterNightTime2',
'winterDayTime1',
'winterDayTime2',
'winterPeakTime');
for ($count = 0; $count <= $lengthArray; $count++ ) { #looping to the length of the array
$arrayTimestamp = $csvdat[$count]['timestamp'];
$comparator = date('Hs', strtotime($arrayTimestamp)); #extracting the time format
$nightTime1 = 0759;
$dayTime1 = 1659;
$peakTime = 1859;
$dayTime2 = 2259;
$nightTime2 = 2359;
if ( $comparator == $nightTime1 ) {
if (empty($newData['nightTime1'])) {
$newData['nightTime1'] = $nightTime;
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan1'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan2'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan3'];
}
else if (!empty($newData)) {
}
}
}
return $newData;
}
getData($lengthData , $csvdata);
var_dump($newData);
The problem I am facing now is that I get an Undefined offset error at the line
$arrayTimestamp = $csvdat[$count]['timestamp'];
and an undefined variable for $newData. I am not very good at php so please advise.
Here is the var_dump of the csvdat array
array (size=118061)
0 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5972' (length=4)
'curr_property_cost' => string '62' (length=2)
'day_property' => string '19' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2898' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '9' (length=1)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2189' (length=4)
'curr_chan2' => string '2898' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '7' (length=1)
'day_chan2' => string '9' (length=1)
'day_chan3' => string '2' (length=1)
1 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '37' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2141' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '16' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2141' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '14' (length=2)
'day_chan2' => string '16' (length=2)
'day_chan3' => string '5' (length=1)
2 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '54' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2157' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '23' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2157' (length=4)
'curr_chan2' => string '2157' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '21' (length=2)
'day_chan2' => string '23' (length=2)
'day_chan3' => string '8' (length=1)
3 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5183' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '71' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2125' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '31' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2125' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '28' (length=2)
'day_chan2' => string '31' (length=2)
'day_chan3' => string '11' (length=2)
Try
for ($count = 0; $count < $lengthArray; $count++ )
instead of
for ($count = 0; $count <= $lengthArray; $count++ )
If and only if you are 100% sure that $lengthArray is the size of the $csvdat array, then your error is caused by this line:
for ($count = 0; $count <= $lengthArray; $count++ ) {
it should be
for ($count = 0; $count < $lengthArray; $count++ ) {
Right now you're breaking the loop if the $count is lower OR the same as $lengthArray. Notice that last key in your array is 3 and its length is 4.
EDIT
Now about Undefined variable: $newData.
You're trying to var_dump($newData) which is undefined. It exists inside the getData function but not outside it. Maybe you wanted to do it like this:
$newData = getData($lengthData , $csvdata);
var_dump($newData);
It should be $count < $lengthArray in for loop
for ($count = 0; $count < $lengthArray; $count++ ) {
}