I have table which have these fields patchno, Project_no Item_Desc Quantity Store Action
And currently it looks like this:
I expect to be as this:
Every patch_no must have unique project_no and store other two columns are variety.
My array variable which is PHP if I use print_r($orders) it outputs this.
Array ( [0] => stdClass Object ( [ID] => 1 [patchno] => 1 [item_id] => 1 [Quantity] => 10 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-22 14:55:51 [Desc] => Jiingado [Cost] => 6.60 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ) [1] =>
stdClass Object ( [ID] => 1 [patchno] => 1 [item_id] => 3 [Quantity] => 2 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-22 14:55:51 [Desc] => Marmar [Cost] => 1.00 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ) [2] => stdClass Object ( [ID] => 1 [patchno] => 1000002 [item_id] => 2 [Quantity] => 21 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-24 15:34:52 [Desc] => Masaabiir [Cost] => 2.00 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ))
the query that outputs these value is this:
SELECT orders.*, items.*, stores.*, projects.*
FROM orders
join items on items.ID=orders.item_id
join stores on stores.ID=orders.store_id
join projects on projects.ID=orders.project_id
Use group_concat to get a comma separated string of all the "variety" values in the group and the unique values of the group should be specified in your group by:
select patchno, Project_no, group_concat(Item), group_concat(Quantity), store
from orders
join items on items.ID=orders.item_id
join stores on stores.ID=orders.store_id
join projects on projects.ID=orders.project_id
group by patchno, project_no, store
Related
SQL:
$sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.* FROM orders
LEFT JOIN order_products ON orders.id=order_products.order_id
LEFT JOIN products ON products.id=order_products.product_id
LEFT JOIN psettings ON psettings.product_id=order_products.product_id
LEFT JOIN cities ON cities.id=orders.city_id
LEFT JOIN locations ON locations.id=orders.location_id
WHERE orders.status = '$status'
ORDER BY orders.id ASC ";
It returns unique data. Here is the returned data:
Array
(
[orders] => Array
(
[id] => 12
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => confirmed
[cashed] => 1115
[created] => 2015-07-02 01:07:18
[modified] => 2015-07-02 01:01:57
[comment] => 07/02/2015 06:00 am
)
[city] => Array
(
[id] => 2
[name] => comilla
)
[location] => Array
(
[id] => 5
[city_id] => 2
[name] => homna
)
[order_products] => Array
(
[0] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
But When I Add another table to join left with orders table it returns duplicate entry. Additionally I join left 'action_bies' with orders table as follows:
$sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.*,action_bies.* FROM orders
LEFT JOIN order_products ON orders.id=order_products.order_id
LEFT JOIN action_bies ON orders.id=action_bies.order_id AND action_bies.action='$action'
LEFT JOIN products ON products.id=order_products.product_id
LEFT JOIN psettings ON psettings.product_id=order_products.product_id
LEFT JOIN cities ON cities.id=orders.city_id
LEFT JOIN locations ON locations.id=orders.location_id
WHERE orders.status = '$status'
ORDER BY orders.id ASC ";
This sql return duplicate data. Here is the data:
Array
(
[orders] => Array
(
[id] => 12
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => confirmed
[cashed] => 1115
[created] => 2015-07-02 01:07:18
[modified] => 2015-07-02 01:01:57
[comment] => 07/02/2015 06:00 am
)
[city] => Array
(
[id] => 2
[name] => comilla
)
[location] => Array
(
[id] => 5
[city_id] => 2
[name] => homna
)
[action] => Array
(
[0] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[1] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[2] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[3] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[4] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[5] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[6] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[7] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[8] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
)
[order_products] => Array
(
[0] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[2] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[3] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[4] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[5] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[6] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
[7] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
[8] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[2] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[3] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[4] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[5] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[6] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[7] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[8] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[1] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[3] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[4] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[5] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[6] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
[7] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
[8] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
Here it should be mentioned that a action_bies table has duplicate data as follows:
How can I get unique data in this case. Thanks to read this large data.
You can select DISTINCT on the most unique identifier you have, which would be orders_products.id most likely.
Example:
SELECT DISTINCT(orders_products.id) FROM orders_products WHERE x = '$y';
I don't see a GROUP BY statement in your original query.
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 5
[user_id] => 5
[category_id] => 1
[name] => Suger
[weight] => 1
[measurement] => kg
[image] =>
[status] => 1
[created] => 2015-05-29 17:02:25
[updated] => 0000-00-00 00:00:00
)
[Category] => Array
(
[id] => 1
[user_id] => 1
[category_name] => Daily uses
[category_image] =>
[status] => 1
[created] => 2015-05-25 12:56:10
[updated] => 2015-06-25 10:27:40
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 23
[product_id] => 5
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055384
[updated] => 1435055384
)
[1] => Array
(
[id] => 28
[product_id] => 5
[store_id] => 1
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435062129
[updated] => 1435062129
)
)
)
[1] => Array
(
[Product] => Array
(
[id] => 6
[user_id] => 1
[category_id] => 1
[name] => Tea
[weight] => 1
[measurement] => litre
[image] =>
[status] => 1
[created] => 2015-05-29 17:02:48
[updated] => 2015-06-18 18:53:30
)
[Category] => Array
(
[id] => 1
[user_id] => 1
[category_name] => Daily uses
[category_image] =>
[status] => 1
[created] => 2015-05-25 12:56:10
[updated] => 2015-06-25 10:27:40
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 22
[product_id] => 6
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055076
[updated] => 1435055076
)
[1] => Array
(
[id] => 25
[product_id] => 6
[store_id] => 1
[details] =>
[mrp] => 12
[selling_price] => 12
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435059905
[updated] => 1435059905
)
)
)
[5] => Array
(
[Product] => Array
(
[id] => 11
[user_id] => 2
[category_id] => 13
[name] => Real Mix Fruit Juice
[weight] => 200
[measurement] => litre
[image] =>
[status] => 1
[created] => 2015-06-17 12:03:19
[updated] => 2015-06-18 13:05:16
)
[Category] => Array
(
[id] => 13
[user_id] => 2
[category_name] => Soft Drink juices
[category_image] =>
[status] => 1
[created] => 2015-06-17 11:15:33
[updated] => 2015-06-18 18:46:03
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 2
[product_id] => 11
[store_id] => 2
[details] => Real Mix Fruit Juice(200 litre)
[mrp] => 20
[selling_price] => 18
[discount] => 10
[price_difference] => 2
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1434613767
[updated] => 1434613767
)
[1] => Array
(
[id] => 29
[product_id] => 11
[store_id] => 1
[details] => Real Mix Fruit Juice
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435062192
[updated] => 1435123348
)
)
)
[8] => Array
(
[Product] => Array
(
[id] => 14
[user_id] => 2
[category_id] => 13
[name] => Real Apple Juice
[weight] => 1
[measurement] => ml
[image] =>
[status] => 1
[created] => 2015-06-18 13:06:44
[updated] => 0000-00-00 00:00:00
)
[Category] => Array
(
[id] => 13
[user_id] => 2
[category_name] => Soft Drink juices
[category_image] =>
[status] => 1
[created] => 2015-06-17 11:15:33
[updated] => 2015-06-18 18:46:03
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 24
[product_id] => 14
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055411
[updated] => 1435055411
)
)
)
)
In the above array there are two same category array .I would like merge the same category array records
I need a output like
Category => array(
//category data,
Products=>array(//product data,
Storeproducts =>array(
//Storeproducts data
)
)
)
PHP is pretty well documented and you can find all of the Array sorting methods on this page.
There are some good examples in the comments as well - all you need to do is to sort the array by category and you are done.
I have two tables: orders and product_orders
there are one id in orders table. i.e: 9
and order_id(as foreign key) in product_orders repeated 3 times. So I want this three entry in product_orders table with one entry in orders table.
My query is:
SELECT orders.*,order_products.* FROM orders LEFT JOIN order_products ON orders.id=order_products.order_id
this gives :
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
)
[1] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
)
[2] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
)
My expected result is:
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[0] => Array(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[1] => Array(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[2] => Array(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
)
)
I used GROUP BY orders.id. But no luck.
You are basically expecting a result that is not possible to obtain with a SQL query: you want a hierarchical structure, with a single order and a set of order_product for that order, while the query returns a flat table, with one row for each order together with the information of a different order_product. This is the way in which SQL works.
A way to solve your problem is to do a sort of post-processing in php, after getting all the rows, group all of them that have the same order part, and produce a new array for the order_product part related to that order.
Hi my zend paginator works well when the $business_list array format is like below.
but when the array format changes it displays only one page instead of many pages.
$paginator = Zend_Paginator::factory($business_list);
Array
(
[0] => Array
(
[id] => 216
[userid] => 141
[title] => first req
[image] =>
[logo] =>
[description] =>
this is the first requirment
[date] => 2012-06-12 10:31:01
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 315
[business_id] => 216
[section_id] => 1
[subsection_id] => 13
[description] =>
)
[1] => Array
(
[id] => 316
[business_id] => 216
[section_id] => 3
[subsection_id] => 14
[description] =>
)
[2] => Array
(
[id] => 317
[business_id] => 216
[section_id] => 4
[subsection_id] => 15
[description] =>
)
)
[CmUser] => Array
(
[id] => 141
[username] => venki
[password] =>
[firstname] => venkatesh
[lastname] => abus
[image] => 54winter.jpg
[email] => xxx#xx.com
[phone] => 23423452
[group_id] => 2
[status] =>
[registered_date] => 2012-06-04 06:32:58
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
[1] => Array
(
[id] => 214
[userid] => 98
[title] => gopicontractor
[image] => 54bluehills.jpg
[logo] => 239waterlilies.jpg
[description] =>
TIt uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
[date] => 2012-06-11 12:18:58
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 305
[business_id] => 214
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 306
[business_id] => 214
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 307
[business_id] => 214
[section_id] => 3
[subsection_id] => 1
[description] =>
)
[3] => Array
(
[id] => 308
[business_id] => 214
[section_id] => 4
[subsection_id] => 9
[description] =>
)
)
[CmUser] => Array
(
[id] => 98
[username] => gopi.s
[password] =>
[firstname] => venkatesh
[lastname] => franc
[image] =>
[email] => ss#ss.com
[phone] => 23423452
[group_id] => 3
[status] =>
[registered_date] => 2012-05-16 12:36:57
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
)
the above array format displays paginatin correctly.
This is the array that displays only one page in pagination.
Zend_Paginator Object
(
[_cacheEnabled:protected] => 1
[_adapter:protected] => Zend_Paginator_Adapter_Array Object
(
[_array:protected] => Array
(
[0] => Array
(
[id] => 89
[username] => xx
[password] => xx
[firstname] => xx
[lastname] => xx
[image] =>
[email] => xx#ymail.com
[phone] => 2342345
[group_id] => 2
[status] => offline
[registered_date] => 2012-05-15 10:58:53
[last_visit] =>
[is_active] => 1
[subscribe] => 0
[CmBusiness] => Array
(
[0] => Array
(
[id] => 204
[userid] => 89
[title] => xxhousing
[image] => 760067.jpg
[logo] => xx_818f3c97e6_o.jpg
[description] => xx
[date] => 2012-05-31 13:36:17
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 251
[business_id] => 204
[section_id] => 1
[subsection_id] => 6
[description] => xx
)
[1] => Array
(
[id] => 252
[business_id] => 204
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[2] => Array
(
[id] => 253
[business_id] => 204
[section_id] => 3
[subsection_id] => 4
[description] => xx
)
[3] => Array
(
[id] => 254
[business_id] => 204
[section_id] => 4
[subsection_id] => 9
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 1
[type] => Personal business
[status] => 1
)
)
[1] => Array
(
[id] => 205
[userid] => 89
[title] => xx
[image] => 41217850-desktop-wallpapers-new-windows-xp.jpg
[logo] => 356anger_n.jpg
[description] => xx
[date] => 2012-05-31 13:37:15
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 255
[business_id] => 205
[section_id] => 1
[subsection_id] => 6
[description] =>xx
)
[1] => Array
(
[id] => 256
[business_id] => 205
[section_id] => 3
[subsection_id] => 1
[description] => xx
)
[2] => Array
(
[id] => 257
[business_id] => 205
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[3] => Array
(
[id] => 258
[business_id] => 205
[section_id] => 4
[subsection_id] => 10
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 3
[type] => Bilde companies
[status] => 1
)
)
[2] => Array
(
[id] => 206
[userid] => 89
[title] => Nuestros recursos
[image] =>
[logo] =>
[description] => xx
[date] => 2012-05-31 13:38:04
[area] => 1
[budget] => 1
[type] => 4
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 259
[business_id] => 206
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 260
[business_id] => 206
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 261
[business_id] => 206
[section_id] => 3
[subsection_id] => 2
[description] =>
)
[3] => Array
(
[id] => 262
[business_id] => 206
[section_id] => 4
[subsection_id] => 10
[description] =>
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 4
[type] => Designer
[status] => 1
)
)
)
)
)
[_count:protected] => 1
)
[_currentItemCount:protected] =>
[_currentItems:protected] =>
[_currentPageNumber:protected] => 1
[_filter:protected] =>
[_itemCountPerPage:protected] => 2
[_pageCount:protected] => 1
[_pageRange:protected] =>
[_pages:protected] =>
[_view:protected] =>
)
I have a query which gives back an array, I'd like to be able to merge the array values which have the same "clientid"
Here's my query:
SELECT * FROM #__db_clients_trip WHERE clientid IN (1999, 2984, 1681) AND companyid IN (1,2)
Here's my array:
stdClass Object
(
[id] => 61
[trip_code] => EUR0600
[clientid] => 1999
[date] => 2000-06-17
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 89
[trip_code] =>
[clientid] => 2984
[date] => 2000-03-18
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 176
[trip_code] => EUR0799
[clientid] => 1999
[date] => 1999-07-09
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 281
[trip_code] => EUR0299
[clientid] => 1681
[date] => 1999-03-01
[invoice] => 30666
[aud] => 1000
[wsale] => 950
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 296
[trip_code] => EUR0799
[clientid] => 1681
[date] => 1999-07-15
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
Is this possible?
EDIT:
To either show a single row per client id, or to merge the array like so:
stdClass Object
(
[id] => 61
[trip_code] => EUR0600, EUR0799
[clientid] => 1999
[date] => 2000-06-17, 1999-07-09
[invoice] =>
[aud] => 0, 0
[wsale] => 0, 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2, 2
)
^^ or something like that? I'm guessing a single row per clientid would be easier..?
If you just want a single row, add this to the end of the SQL:
GROUP BY clientid
If you want to get (for instance), the latest date for all the records matching a clientid, change:
SELECT *
To:
SELECT *, MAX(date) AS latest_date
You can use functions like MAX, MIN and SUM on fields to get a 'merged' view when using GROUP BY
sounds like you should loop over your result set and do the merge yourself... Use a 2 dimensional array...
If all you care about is a distinct clientID, then you should use the Distinct keyword:
select distinct(clientID) from clients