Print two-column table from Mongo aggregation in PHP - php

I have the following Mongo aggregation in PHP:
$results = $collection1 -> aggregate (array(
'$group' => array(
'_id' => '$Issue',
'total' => array('$sum' => 1 ),
)), array( '$sort' => array( 'total' => -1 ), ));
print_r($results);
It adequately produces the following output:
Array ( [result] => Array ( [0] => Array ( [_id] => Sales [total] => 51 )
[1] => Array ( [_id] => Service [total] => 41 ) [2] => Array ( [_id] =>
Marketing [total] => 31 ) [3] => Array ( [_id] =>Delivery [total] => 28 )
... etc.
Pardon my "noob" question here, but how do I format this into a simple two-column HTML format showing the name of the category in the first column (e.g., Sales, Marketing, etc.) and the total for each category on the same row?
I don't want to use print_r or var_dump and I've tried various 'foreach' and 'while' methods from this site and am just stumped at this point.
Thank you, kindly, in advance.

Hmm maybe I have missed something here but does this do it?
<table>
<thead>
<tr>
<th></th><th>Total</th>
</tr>
</thead>
<tbody>
foreach($output_var['result'] as $row){
?><tr><td><? echo $row['_id']; ?></td><td><? echo $row['total'] ?></td></tr><?
}
</tbody>
</table>

Related

Retrieving data in unidimensional array in Cakephp 1.3

I am using cakephp 1.3 to extract data from a table with Cities and Postal Codes and I would like to extract these two columns to populate drop down menu with postal codes as menu 'id' and town names as menu 'values'.
The “find( 'list')” method is out of option because some of the towns have the same postcodes and the class is picking only unique value so instead of getting :
Array
(
[800] => Darwin
[801] => Darwin
[810] => Allawa
)
I am getting
Array
(
[800] => Darwin
[810] => Allawa
)
The method find('all') is returning a multidimensional array
Array
(
[0] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Acton
)
)
[1] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Ainslie
)
)
)
What is the best work around to get the desired data in unidimensional array without reducing the output to unique array keys? Something like :
Array ( [800] => Darwin [801] => Darwin [810] => Allawa )
You should still be able to do this with $Model->find('list').
$this->Model->find(
'list',
array(
'fields' => array(
'postal_code',
'city_name'
),
'contain' => false
)
);
This should return a list resultset using the two fields that you selected (postal code and city name).
Your alternate option, is to do a find('all'):
$results = $this->Model->find(
'all',
array(
'conditions' => array( ... )
)
);
Now, you have this format in your data results:
$results = Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 1
[postal_code] => 800,
[city_name] => 'Darwin'
)
)
[1] => Array
(
[ModelName] => Array
(
[id] => 2
[postal_code] => 801,
[city_name] => 'Darwin'
)
)
[2] => Array
(
[ModelName] => Array
(
[id] => 3
[postal_code] => 810,
[city_name] => 'Allawa'
)
)
)
Then use the Set class within Cake to extract some data:
$keys = Set::extract('/ModelName/postal_code', $results);
$values = Set::extract('/ModelName/city_name', $results);
/* Then some array_combine magic */
$list = array_combine($keys, $values);
Now you have an array that is what you want. Albeit a longer way, but it works. You can then pass this variable to your Form::select() helper to create your dropdown.
Hope this helps!

How do I correct my arrays to create my desired table output?

I have two arrays, one for creating the first column (service) in a table like structure and the second for every other column (day) after that. First column array has the structure:
Array
(
[0] => DPW
[1] => PNS
[2] => WS1
[3] => WS2
)
This is a dynamic array so there could be more values than this, but it will always be unique and sorted alphabetically.
My second array for the other columns has the structure:
Array
(
[0] => Array
(
[Monday] => Array
(
[0] => Array
(
[date] => 42016
[message] => 07:00 START
[service] => DPW
)
[1] => Array
(
[date] => 42016
[message] => 16:30 START
[service] => PNS
)
[2] => Array
(
[date] => 42016
[message] =>
[service] => WS1
)
// etc ...
[3] => Array
(
[Thursday] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
[1] => Array
(
[date] => 42017
[message] => 16:00 CUT OFF
[service] => DPW
)
[2] => Array
(
[date] => 42017
[message] =>
[service] => PNS
)
)
)
)
So basically each day is the next column. My problem is I initially thought that the service key in the second array was always in the same order for each day, but its not. You can see from the Thursday that DPW is twice in a row, whereas Monday isn't. I can change the format of these arrays, so this is where my questions lie.
View desired output
My thoughts are for starters the day should contain the same number of keys as the services array, so should I add a blank value to make up the same number of keys? To cater for the same service in a row should I add a further level to cater for multiple values look like so:
Array
(
// etc ...
[3] => Array
(
[Thursday] => Array
(
[0] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
[1] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
)
[2] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => PNS
)
)
[3] => Array
(
)
[4] => Array
(
)
)
)
)
I have also added in the blank values. Finally how would I print these values in the same order as the first (services) array so they aren't under the wrong service?
If is difficult to explain this and I could be going about it the wrong way, but that's why I am asking the questions.
EDIT:
Almost there with this, my only issue now is I need to show the Start and Cut Off date as being the same item spread out across multiple days. For instance the array might have these values at some point:
[1] => Array
(
[date] => 42016
[message] => 16:30 START
[service] => DPM
)
// etc...
[3] => Array
(
[date] => 42021
[message] => 16:30 CUT OFF
[service] => DPM
)
So I need a way to have these on their own row with a background colour or something to show they are the one item, like the graphic above. Not sure if this is possible?
Thanks
Robert!
I'm not sure this is exactly what you want, but it might help :
$services = array('DPW', 'PNS', 'WS1', 'WS2');
$days = array(
array(
'Monday' => array(
array(
'date' => 42016,
'message' => '07:00 START',
'service' => 'DPW'
),
array(
'date' => 42016,
'message' => '16:30 START',
'service' => 'PNS'
),
array(
'date' => 42016,
'message' => '',
'service' => 'WS1'
),
),
),
array(
'Thursday' => array(
array(
'date' => 42017,
'message' => '',
'service' => 'DPW'
),
array(
'date' => 42017,
'message' => '16:00 CUT OFF',
'service' => 'DPW'
),
array(
'date' => 42017,
'message' => '',
'service' => 'PNS'
)
)
)
);
And here is the HTML Table:
<?php $service_days = []; ?>
<table cellpadding="10" width="100%" align="left">
<thead>
<td style="background:#333;color:#FFF">Service</td>
<?php foreach($days as $day): ?>
<td style="background:#666;color:#FFF"><?php echo array_keys($day)[0]; ?></td>
<?php
foreach(array_values($day)[0] as $d)
$service_days[array_keys($day)[0]][$d['service']][] = $d;
?>
<?php endforeach; ?>
</thead>
<?php foreach($services as $service): ?>
<tr>
<td><?php echo $service ?></td>
<?php foreach($service_days as $day => $srv): ?>
<td>
<?php foreach($srv as $_service => $periode): ?>
<?php if($_service == $service): ?>
<?php foreach($periode as $p): ?>
<p style="background:#DDD"><?php echo $p['date'], '<br>', $p['message']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

PHP Nested Array loop nested results

I am experimenting with my first API and getting stuck with the results. I am getting an Array Back:
Array
(
[GetOrderListResult] => Array
(
[Status] => Success
[MessageCode] => 0
[ResultData] => Array
(
[OrderResponseItem] => Array
(
[0] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T11:41:45.953
[TotalOrderAmount] => 12.7800
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138711
[ClientOrderIdentifier] => 138711
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
[1] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T12:59:01.78
[TotalOrderAmount] => 6.3900
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138750
[ClientOrderIdentifier] => 138750
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
)
)
)
)
Now the onyl way I know how to reference a fied such as the order id in the array is:
echo "Order ID: ".$result['GetOrderListResult']['ResultData']['OrderResponseItem']['0']['OrderID'];
But I want to be able to loop through the array of orders and execute code for each item, could somewbody please point me in the right direction for:
a) is there a better way to refernce these fields?
b) how do I loop through the OrderResponseItem part of the array?
The only loop I could think of was for the whole array not nested items in the array.
Sorry if I'm missing something simple....
Thanks and if you need the data in any other format please let me know.
Since you already know the keys you could just use a foreach to access them an point to that key then loop. Something like this:
foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $value) {
$order_id = $value['OrderID'];
// your other processes
}

Remove key from multidimensional array in PHP

Hi I am going to make tree array for multilevel category menu and I have found the solution from below link:
stackoverflow answer
But my problem is that i am getting output of my array as FORMAT-1 and in the above given link the source array required as FORMAT-2
So can you please give me a hand for how to convert my array from FORMAT-1 to FORMAT-2
FORMAT-1
Array
(
[0] => Array
(
[category_id] => 11
[category_name] => Accessories
[parent_category_id] => 1
)
[1] => Array
(
[category_id] => 12
[category_name] => Keyrings
[parent_category_id] => 1
)
[2] => Array
(
[category_id] => 13
[category_name] => Photo Frames/Photo Albums
[parent_category_id] => 1
)
)
FORMAT-2 (I want output as below)
Array(
Array(
'category_id' => 11
'category_name' => 'Accessories'
'parent_category_id' => 1
),
Array(
'category_id' => 12
'category_name' => 'Keyrings'
'parent_category_id' => 1
),
Array(
'category_id' => 13
'category_name' => 'Photo Frames/Photo Albums'
'parent_category_id' => 1
)
);
Thanks in advance for your help and much appreciated
Both arrays are more are like same..
Format-1 looks print_r($yourarray); version
Format-2 looks like var_export($yourarray); version.
You need to have a look at the debugger functions.. print_r(), var_dump() and var_export().
If your purpose is debugging you may use <pre>var_dump()</pre>.

cakephp find order sorting with associated fields

Array
(
[Site] => Array
(
[id] => 1
[parent_id] => 0
[title] => test
[url] => http://www.test.com
[slug] => www_test_com
[keywords] => cpc,seo
[language_id] => 1
)
[SiteMeta] => Array
(
[0] => Array
(
[id] => 1
[site_id] => 1
[key] => pagerank
[value] => 5
[created] => 2010-08-03 00:00:00
)
)
)
By using cakephp, I debug my find('all') and returned me above array. I can sort Site field values by order value inside find function how I am able to order also with SiteMeta values
Any ideas?
Thanks
I'd do this in the Model as part of the association.
$hasMany = array('SiteMeta'=>array('order'=>array('SiteMeta.value'=>'asc')))
You won't have to repeat yourself anywhere then.
you can use a default query with a order condition like this:
$result = $this->Site->find('all', array(
'order'=>array('SiteMeta.value DESC' , 'Site.value DESC'),
'recursive'=>1));
Obviously you can put a condition inside the array to retrieve your right result
I would do it this way (as shown in the Docs)
$result = $this->Site
->find()
->contain([
'SiteMeta' => [
'sort' => ['SiteMeta.pagerank' => 'ASC']
]
]);

Categories