last day (19 days later) » 

10:19 AM
1
A: How to group data to show average price by weeks from a select query

Mike MillerTo do the whole process in PHP change your select and remove the select_avg and do a normal select of unitPrice. $new_result_array=[]; $average_prices=[]; foreach($result_array as $row){ $new_result_array[date('W',$row['createdDate'])['weekly_order_total']+=$row['unitPrice']; $new_result_...

 
Nope notice the sub array assignment. It will create a sub array indexed at top level by week number. Further work to do to count items in each sub array and calculate the average but this should be trivial once all data is properly grouped. In spirit of completeness I have updated my answer
 
I tried with this code but it's giving me errors:A non well formed numeric value encountered,Undefined index: weekly_order_total Code is: $new_result_array=[]; $average_prices=[]; foreach($query->result_array() as $row){ $new_result_array[date('W',$row['interval'])]['weekly_order_total']+=$row['unitP‌​‌​rice']; $new_result_array[date('W',$row['interval'])]['orders'][]=$row; } foreach($new_result_array as $week=>$orders){ $average_prices[$week]=$orders['weekly_order_total']/count($orders['orders']); }
 
Not much help posting into the comment as I cant follow it. What does print_r($new_result_array); output? Inspect the arrays and you will see that for some reason you dont have a number where its supposed to be
 
I'll update in my question, so you can see it correct.
 
Crucially need to see the output of print_r($new_result_array);
 
10:19 AM
I edited my question and I added it. It wasn't closed one of the brackets of the arrey, I closed it, see my edited code if it is correct, please. :)
Гс
hi
 
The dates are not parsing correctly is first issue so I have amended the answer to wrap each created_date in strtotime(). Change to this and repost the print_r() output. If possible indent it so its easy to read
 
to change this?
$this->db->select('DATE_FORMAT(DATE_SUB(ordersheader.createdDate,
INTERVAL DAYOFWEEK(ordersheader.createdDate)-2 DAY), "%Y-%m-%d") AS "interval"',FALSE);
 
see my answer its a small change to the first foreach(){}
 
$new_result_array[date('W',strtotime($row['interval']))['weekly_order_total']+=$‌​row['unitPrice'];
where should I close [ this bracket
should be like this, isn't it?
$new_result_array[date('W',strtotime($row['interval']))]['weekly_order_total']+=‌​$row['unitPrice'];
see it in this link, it's from localhost
 
Yep that is right
 
10:30 AM
it's showing these errors again
 
Its a notice about creating the new index on the fly actually doesnt matter. If you change your index.php file and amend your environment to use error_reporting(E_ERROR) these will go away
 
is there something wrong to show these notices
why does it show them
 
its telling you that the index you are writing to doesnt yet exist. This is not perfect practice but would require a large change to your code to get rid of them and really it doesnt matter as PHP is clever enough to just send a notice and then create the index. In production we would suppress all errors (ie error_reporting(0) so these will not be shown
urgh ok I will change my answer to remove the notices
 
I visualize them in a chart
in controller I get result from my query
and I have this:
$firms = $this->Receivedorders_model->average_price_by_week();
$p = array();
foreach ($firms as $key => $firm) {
$p[$key] = array('c' => array(array(
'v' => $firm['interval'],
),


array(
'v' => round($firm['unitPrice'],2),
)));
}
echo json_encode(array(
'cols' => array(
array('id' => 'name', 'label' => lang("customer"), 'type' => 'string'),
array('id' => 'incomes', 'label' => lang("chart_average_price"), 'type' => 'number'),

),
'rows' => $p

));
should I put your foreach here
instead of $firms I should use $average_prices?
 
10:49 AM
I think I didi it for the chart
my query should be the same like in my question, isn't it?
I mean this query remains the same:
$this->db->select('DATE_FORMAT(DATE_SUB(ordersheader.createdDate,
INTERVAL DAYOFWEEK(ordersheader.createdDate)-2 DAY), "%Y-%m-%d") AS "interval"',FALSE);
$this->db->select('unitPrice');
$this->db->from('orderitems');
$this->db->join('ordersheader', 'ordersheader.idOrder=
orderitems.idOrder');

$this->db->where('ordersheader.createdDate > ',$current);


$this->db->order_by('interval');
$this->db->group_by('interval');
$query=$this->db->get();
return $query->result_array();
 
 
1 hour later…
11:55 AM
Are you here?
 
 
1 hour later…
12:55 PM
so yes I think your SQL looks fine
 

  last day (19 days later) »