Merging php arrays if "clientid" is identical? - php

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

Related

PHP Joomla template index.php JCategoryNode output instead of current category ID number

So inside my main Joomla template/index.php file I am doing the following to try and get the category id number of the page the user is currently viewing.
The result should be 9 since that is the id number of the category
jimport('joomla.application.categories');
$category = JCategories::getInstance('hwdMediaShare')->get($this->item->categories[0]->id[0]);
print_r($category);
echo "\n\nCategory ID number is : ".($category)."\n\n";
My var_dump shows this (category id number 9 does exist and is present in the var dump so I should be able to obtain it)
[id] => 9
9:random
Shows category id 9 is there and its name too "random"
JCategoryNode Object ( [id] => root [asset_id] => 0 [parent_id] => 0 [lft] => 0 [rgt] => 21 [level] => 0 [extension] => system [title] => ROOT [alias] => root [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {} [metadesc] => [metakey] => [metadata] => {} [created_user_id] => 42 [created_time] => 2011-01-01 00:00:01 [modified_user_id] => 0 [modified_time] => 0000-00-00 00:00:00 [hits] => 0 [language] => * [numitems] => [childrennumitems] => [slug] => 1:root [assets] => [_parent:protected] => [_children:protected] => Array ( [0] => JCategoryNode Object ( [id] => 9 [asset_id] => 610 [parent_id] => root [lft] => 3 [rgt] => 4 [level] => 1 [extension] => com_hwdmediashare [title] => Random [alias] => random [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {"category_layout":"","image":""} [metadesc] => [metakey] => [metadata] => {"author":"","robots":""} [created_user_id] => 571 [created_time] => 2016-02-10 11:52:24 [modified_user_id] => 571 [modified_time] => 2016-03-16 15:45:36 [hits] => 1107 [language] => * [numitems] => [childrennumitems] => [slug] => 9:random [assets] => [_parent:protected] => JCategoryNode Object *RECURSION* [_children:protected] => Array ( ) [_path:protected] => Array ( [0] => 9:random ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => 1 [_constructor:protected] => hwdMediaShareCategories Object ( [_nodes:protected] => Array ( [root] => JCategoryNode Object *RECURSION* [9] => JCategoryNode Object *RECURSION* ) [_checkedCategories:protected] => Array ( [9] => 1 ) [_extension:protected] => com_hwdmediashare [_table:protected] => #__hwdms_category_map [_field:protected] => category_id [_key:protected] => id [_statefield:protected] => state [_options:protected] => Array ( [table] => #__hwdms_category_map [extension] => com_hwdmediashare [field] => category_id [published] => 0 [access] => true ) ) [_errors:protected] => Array ( ) [note] => [path] => uncategorised/test/uncategorised/random [version] => 1 ) ) [_path:protected] => Array ( ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => [_constructor:protected] => hwdMediaShareCategories Object ( [_nodes:protected] => Array ( [root] => JCategoryNode Object *RECURSION* [9] => JCategoryNode Object ( [id] => 9 [asset_id] => 610 [parent_id] => root [lft] => 3 [rgt] => 4 [level] => 1 [extension] => com_hwdmediashare [title] => Random [alias] => random [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {"category_layout":"","image":""} [metadesc] => [metakey] => [metadata] => {"author":"","robots":""} [created_user_id] => 571 [created_time] => 2016-02-10 11:52:24 [modified_user_id] => 571 [modified_time] => 2016-03-16 15:45:36 [hits] => 1107 [language] => * [numitems] => [childrennumitems] => [slug] => 9:random [assets] => [_parent:protected] => JCategoryNode Object *RECURSION* [_children:protected] => Array ( ) [_path:protected] => Array ( [0] => 9:random ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => 1 [_constructor:protected] => hwdMediaShareCategories Object *RECURSION* [_errors:protected] => Array ( ) [note] => [path] => uncategorised/test/uncategorised/random [version] => 1 ) ) [_checkedCategories:protected] => Array ( [9] => 1 ) [_extension:protected] => com_hwdmediashare [_table:protected] => #__hwdms_category_map [_field:protected] => category_id [_key:protected] => id [_statefield:protected] => state [_options:protected] => Array ( [table] => #__hwdms_category_map [extension] => com_hwdmediashare [field] => category_id [published] => 0 [access] => true ) ) [_errors:protected] => Array ( ) [note] => [path] => [version] => 1 ) Category ID number is : JCategoryNode
And when I try to echo my output to see my number I see this
Category ID number is : JCategoryNode
Can anyone see what I am doing wrong here and correct me.
It should say
Category ID number is : 9

PHP Multidimensional array combinations

Below is My Array. I want to make all possible combinations of array whose sum is equal to some number(e.g 20). The super parent array is continent and it's child array is it's countries and countries child array is its respective cities. i want to get all combinations of countries whose cities sum of days_range equal to some number(e.g 20). This is just a single continent array. it may be possible that the array is for other continents are also available.
Array
(
[1] => Array
(
[10] => Array
(
[0] => Array
(
[id] => 18
[city_name] => Baku
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
[2] => Array
(
[id] => 43
[city_name] => Lahıc
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
)
[23] => Array
(
[0] => Array
(
[id] => 42
[city_name] => Vientiane
[country_id] => 23
[country_name] => Laos
[days_range] => 7
[continent_id] => 1
)
[1] => Array
(
[id] => 47
[city_name] => Vang Vieng
[country_id] => 23
[country_name] => Laos
[days_range] => 3
[continent_id] => 1
)
)
[20] => Array
(
[0] => Array
(
[id] => 37
[city_name] => Tamsui District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 1
[continent_id] => 1
)
[1] => Array
(
[id] => 35
[city_name] => Taipei
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
[22] => Array
(
[2] => Array
(
[id] => 46
[city_name] => Maolin District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
[3] => Array
(
[id] => 36
[city_name] => Tainan
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
)
)
Below is my expected Output.
Array(
[0]=Array(
[0]=>[10],
[1]=>[23],
)
[1]=Array(
[0]=>[10],
[1]=>[20],
[2]=>[22],
)
[2]=Array(
[0]=>[23],
[1]=>[20],
[2]=>[22],
)
)

How to group PHP array result

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

Laravel Eloquent Get Append Attributes With Find

I created a model with 10 append attributes and I can't get them with find() method. But when I convert returned object from find() to an array with method toArray() I can see them but I need them like an object.
This object was printed when I use find():
School Object
(
[table:protected] => schools
[fillable:protected] => Array
(
[0] => name
[1] => type_id
[2] => description
[3] => info_specialties
[4] => contacts
[5] => cover_name
[6] => financing_id
[7] => city
)
[guarded:protected] => Array
(
[0] => id
)
[appends:protected] => Array
(
[0] => type
[1] => short_type
[2] => school_url
[3] => cover_photo_url
[4] => cover_photo_thumbnail_url
[5] => city
[6] => municipality
[7] => appended_district_id
[8] => district
[9] => description_without_tags
)
[district_id] =>
[cover_photo] =>
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
)
[original:protected] => Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
And when I use toArray:
Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
[type] => qdasdasd
[short_type] => asdasdasd
[school_url] => http://localhost:8000/school/24
[cover_photo_url] => http://localhost:8000/storage/cover_photos/SAbjfpe4m7.jpg
[cover_photo_thumbnail_url] => http://localhost:8000/storage/cover_photos/thumbnails/SAbjfpe4m7.jpg
[city] => sdasdasd
[municipality] => ÐÑкаква община2 от нÑкакъв облаÑÑ‚2
[appended_district_id] => 6
[district] => ÐÑкакъв облаÑÑ‚2
[description_without_tags] => asdadasdasdasdqwdqd\qw\[dqw\d
)
Here's an example of my class:
<?php
class School extends Eloquent {
protected $appends = array('type');
public function getTypeAttribute()
{
return Type::find($this->type_id)->name;
}
}
That's the nature of custom attributes that you get with an attribute accessor. They will only be evaluated when used (and then cached for later use). When you convert it to and array, all $appends properties are loaded.
If you have your model object you can just access them like any other attribute:
$school->type;

Getting the value of first array from the array

In php I have an array look like this.
Array ( [0] =>
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
[2] => Array ( [id] => 12 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
[3] => Array ( [id] => 14 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
)
From here I want to get the first value of array. For example I want to get the value of first array
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
So can someone kindly tell me how to get the value of 1st array?Any help and suggestions will be really appreciable. Thanks
say all your array was in a variable $myArray, then
myArray[1]
will give you your first array

Categories