I wanted to know if it is possible to list the records of a Mysql query into custom arrays
Like when I do
SELECT CAT.catgryname,count(JB.knifeid) AS total
FROM jbs AS JB
LEFT JOIN krgstrs AS KR ON KR.id = JB.knfid
LEFT JOIN accnts AS ACC ON ACC.job_id = JB.id
LEFT JOIN catgor AS CAT ON CAT.id = KR.subctid
WHERE JB.usrid =xxx
GROUP BY CAT.catgryname
it gives me
[0] => Array
(
[CAT] => Array
(
[categoryname] => Fridge Magnet
)
[0] => Array
(
[total] => 22
[MNTH] => Jan
)
)
I wanted to have a custom index instead of a 0 for the second array nested inside like
[0] => Array
(
[CAT] => Array
(
[categoryname] => Fridge Magnet
)
[DET] => Array
(
[total] => 22
[MNTH] => Jan
)
)
Note the DET instead of a 0 here.
I tried
SELECT CAT.catname,count(cat.id) as DET.total
But it throws an error as I think it interprets DET to be a table here.
Thanks All.
I wouldn't do this on DB-Side.
However i don't know how you get the array (using mysqli or mysql PHP extension). Regardless of that, just do that on the PHP-Side, as pointed out in the comments.
Try using this ($results is the array you have as described - with indexes CAT and 0)
$newArray = array();
foreach($results as $result) {
$newArray[] = array(
'CAT' => array(
'categoryname' => $result["CAT"]["categoryname"]
),
'DET' => array(
'total' => $result[0]["total"],
'MNTH' => $result[0]["MNTH"]
)
);
}
And there you go with $newArray
Related
I have been trying to work this out for two days now and am hitting a brick wall. I have a skyscanner array that has flight itineraries where I have the flight
Leg - being Itineraries -> OutboundLegId -
and also the legs which shows the flight number - being
Legs -> FlightNumbers -> FlightNumber.
What I am trying to achieve is to display the Itinerary and then join the Flight Number on to that. But no matter what I try I cannot get this to work. I have read all about Keys on here and tried loads of examples but am coming up with nothing. Can someone point me in the right direction please?
Example arrays below
[Itineraries] => Array
(
[0] => Array
(
[OutboundLegId] => 13542-1610140610-29-0-13445-1610141240
[InboundLegId] => 13445-1610211340-29-0-13542-1610211640
[PricingOptions] => Array
(
[0] => Array
(
[Agents] => Array
(
[0] => 2174187
)
[QuoteAgeInMinutes] => 31
[Price] => 200.98
[DeeplinkUrl] => http://partners.api.skyscanner.net/apiservices/deeplink/v2?_cje=5JlLCgyPUKY0hT8T0Ybh6dL0Xf0htAiHTFX7RU79eeI3XvrsxvEqP1QUJAoHiHRd&url=http%3a%2f%2fwww.apideeplink.com%2ftransport_deeplink%2f4.0%2fUK%2fen-gb%2fGBP%2fcook%2f2%2f13542.13445.2016-10-14%2c13445.13542.2016-10-21%2fair%2fairli%2fflights%3fitinerary%3dflight%7c-32294%7c1152%7c13542%7c2016-10-14T06%3a10%7c13445%7c2016-10-14T12%3a40%2cflight%7c-32294%7c1153%7c13445%7c2016-10-21T13%3a40%7c13542%7c2016-10-21T16%3a40%26carriers%3d-32294%26passengers%3d1%2c0%2c0%26channel%3ddataapi%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d200.98%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3dskyscanner_b2b%26request_id%3d3bc96bda-fd7c-403a-b841-2ccc3c26071d%26commercial_filters%3dfalse%26q_datetime_utc%3d2016-09-29T08%3a18%3a27
)
[Legs] => Array
(
[0] => Array
(
[Id] => 13542-1610140610-29-0-13445-1610141240
[SegmentIds] => Array
(
[0] => 1
)
[OriginStation] => 13542
[DestinationStation] => 13445
[Departure] => 2016-10-14T06:10:00
[Arrival] => 2016-10-14T12:40:00
[Duration] => 270
[JourneyMode] => Flight
[Stops] => Array
(
)
[Carriers] => Array
(
[0] => 105
)
[OperatingCarriers] => Array
(
[0] => 105
)
[Directionality] => Outbound
[FlightNumbers] => Array
(
[0] => Array
(
[FlightNumber] => 1152
[CarrierId] => 105
)
)
)
Assuming this is one big array and its called $data you can nest a couple of foreach loops.
I use foreach loops as I assume there are cases where this data structure get more complex than the one you show
foreach ( $data['Itineraries'] as $itin ) {
foreach ( $data['Legs'] as $legs) {
if ($legs['Id'] == $itin['OutboundLegId']) {
// we matched the itinerary with a leg
echo $legs['OutboundLegId'] . ' ' . $legs['FlightNumbers'][0]['FlightNumber'];
}
}
}
Use it as draft. Can't perform function without feedback.
Put proper arrays instead of {YOUR-ARRAY-WITH-LEGS} and {YOUR-ARRAY-WITH-ITINERARIES}
$sortedLegs = array_column('Id', {YOUR-ARRAY-WITH-LEGS});
$joinedArray = array_map(function($itinerary) use($sortedLegs){
if(array_key_exists($itinerary['OutboundLegId'],$sortedLegs)) {
$itinerary['legs'] = $sortedLegs[$itinerary['OutboundLegId']];
}
return $itinerary;
},{YOUR-ARRAY-WITH-ITINERARIES});
I have Array1 that pulls all of the entries for a Wordpress custom post type, and I have Array2 from another table that shows if that post type has been seen by a specific user. If the ID of Array1 & the PageID of Array2 are the same, then add the [Started] & [Finished] fields from Array2 to Array1. Basically I want to combine them to form Array3. I have tried many different solutions with array_combine but can't get the result I'm looking for.
Array1 (
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video )
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) )
Array2 (
[0] => stdClass Object ( [PageID] => 75 [Started] => 1 [Finished] => 1 )
[0] => stdClass Object ( [PageID] => 80 [Started] => 1 [Finished] => 0 ) )
Array3 (
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video [Started] => 1 [Finished] => 1 )
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 [Started] => 1 [Finished] => 0 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) )
Something like this?
$array3 = array();
foreach( $array1 as $arr1 )
{
foreach( $array2 as $arr2 )
{
if( $arr1["ID"] == $arr2["PageID"] )
{
$array3[] = array( $arr1["Started"], $arr2["Finished"] );
}
}
}
array_combine() is not the right function for this.
In your case, you need to use a foreach-loop to loop over the values of your first array and compare them with your second array.
You also need to rearrange array 2, so that you can easily access the values for started and finished using the pageID as key:
$pageMap = array();
foreach($array2 as $entry) {
$pageMap[$entry['PageID']] = array('started' => $entry['Started'], 'finished' => $entry['Finished']);
}
Then you can do:
$combined_array = array();
foreach($array1 as $post) {
if(!isset($pageMap[$post['ID']])) continue; // do nothing if there are no started/finished entries.
$combined_array[$post['ID']] = array_merge($post, $pageMap[$post['ID']]);
}
You could try a first loop to make your array "ID" match and then combine both arrays
foreach( $array2 as $key=>$value )
{
$value['ID'] = $value['PageId'];
unset($value['PageId']);
$array2[$key] = $value;
}
$array3 = array_merge_recursive($array1,$array2);
After trying the posted solutions I decided to make the change in my $wpdb->get_results sql statement instead of using 2 different sql statements and then combining the 2 different arrays. With help from this post - SQL query to join two tables despite the other table may not have values, I was able to use the following to get the desired result.
$oneBigQuery = $wpdb->get_results("SELECT a.ID, a.post_title, b.PageID, b.Started, b.Finished
FROM $wpdb->posts AS a
LEFT JOIN exampleTableName AS b
ON a.ID = b.PageID
AND b.UserID = 3
WHERE a.post_type = 'custom-post-type'
");
I have 4 lectures where each lecture has it's own name:
jbc2014_lezingen
Then for each lecture there will be questions, in this case 2 for each lecture:
jbc2014_vragen
I use this query:
$query = "SELECT * FROM jbc2014_lezingen
INNER JOIN jbc2014_vragen
ON jbc2014_lezingen.id=jbc2014_vragen.lezing_id";
This results in something like:
[0] => Array
(
[id] => 1
[lezing_naam] => lezing 1
[lezing_id] => 1
[vraag] => foobar?
)
[1] => Array
(
[id] => 2
[lezing_naam] => lezing 1
[lezing_id] => 1
[vraag] => foobar?
)
etc.
So the array size is based on the amount of the questions, not the amount of the lectures.
I would like something like:
[0] => Array
(
[id] => 1
[lezing_naam] => lezing 1
[lezing_id] => 1
[questions] Array (
[0] => [vraag] => foobar?
[1] => [vraag] => foobar?
)
)
Where the questions are in an array (vraag means question).
How can this be done?
Later on I need the multiple choice answers in an array inside the questions as well. But I think that won't be hard after having this one fixed.
The closest thing you could do is something like this -
SELECT jbc2014_lezingen.id as lezing_id,
GROUP_CONCAT(vraag) as vraags
FROM jbc2014_lezingen
INNER JOIN jbc2014_vragen
ON jbc2014_lezingen.id=jbc2014_vragen.lezing_i
GROUP BY jbc2014_lezingen.id
and then just get the values with
explode(",",$row['vraags']);
or create another array with something like this
foreach($raw_result as $key => $value){
$new_array[$key]['lezing_id'] = $value['lezing_id'];
$new_array[$key]['vraags'] = explode(",",$value['vraags']);
}
This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 5 months ago.
I have following array where I am trying to merge the elements which has shelf and weight value as duplicate and sum the value of piece key.
Array
(
[0] => Array
(
[shelf] => Left
[weight] => 10.000
[piece] => 1
)
[1] => Array
(
[shelf] => Right
[weight] => 04.000
[piece] => 12
)
[2] => Array
(
[shelf] => Right
[weight] => 04.000
[piece] => 4
)
[3] => Array
(
[shelf] => Right
[weight] => 07.000
[piece] => 8
)
)
Currently I am getting following desired output with help of following SQL statement by creating the temporary table with following fields shelf, weight and piece and inserting all the four values and parsing the result in PHP with following select query
SELECT shelf, weight, SUM(piece) FROM temp GROUP BY CONCAT(shelf, weight)
Array
(
[0] => Array
(
[shelf] => Left
[weight] => 10.000
[piece] => 1
)
[1] => Array
(
[shelf] => Right
[weight] => 04.000
[piece] => 16
)
[3] => Array
(
[shelf] => Right
[weight] => 07.000
[piece] => 8
)
)
However I am believe that this can be simply achieved by PHP, but can't get my head around. Can somebody please point what I might be missing ?
Note to Moderators and SO Vigilante
Please don't take it personal but before marking or even saying this is duplicate, read my question thoroughly and understand what I am trying to achieve and only then if you agree kindly explain in detail why do you think its duplicate, rather than simply arguing on base of question with similar title
I have gone through these questions, but they don't work in my scenario, as they try to merge array and sum value based on one specific element which is usually either ID, but in my case its uniqueness is judged on combination of elements (not one)
1, 2, 3
If you absolutely have to do this in PHP, then something like:
$data = array(
array(
'shelf' => 'Left',
'weight' => '10.000',
'piece' => 1,
),
array(
'shelf' => 'Right',
'weight' => '04.000',
'piece' => 12,
),
array(
'shelf' => 'Right',
'weight' => '04.000',
'piece' => 4,
),
array(
'shelf' => 'Right',
'weight' => '07.000',
'piece' => 8,
),
);
$newData = array();
$result = array_reduce(
$data,
function($newData, $value) {
$key = $value['shelf'].$value['weight'];
if (!isset($newData[$key])) {
$newData[$key] = $value;
} else {
$newData[$key]['piece'] += $value['piece'];
}
return $newData;
},
$newData
);
var_dump($result);
will work, but I really do believe that you're creating major performance problems for yourself with your whole approach to this problem
I am playing around with a quotes database relating to a ski trip I run. I am trying to list the quotes, but sort by the person who said the quote, and am struggling to get the paginate helper to let me do this.
I have four relevant tables.
quotes, trips, people and attendances. Attendances is essentially a join table for people and trips.
Relationships are as follows;
Attendance belongsTo Person hasMany Attendance
Attendance belongsTo Trip hasMany Attendance
Attendance hasMany Quote belongs to Attendance
In the QuotesController I use containable to retrieve the fields from Quote, along with the associated Attendance, and the fields from the Trip and Person associated with that Attendance.
function index() {
$this->Quote->recursive = 0;
$this->paginate['Quote'] = array(
'contain' => array('Attendance.Person', 'Attendance.Trip'));
$this->set('quotes', $this->paginate());
}
This seems to work fine, and in the view, I can echo out
foreach ($quotes as $quote) {
echo $quote['Attendance']['Person']['first_name'];
}
without any problem.
What I cannot get to work is accessing/using the same variable as a sort field in paginate
echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');
or
echo $this->Paginator->sort('Location', 'Attendance.Trip.location');
Does not work. It appears to sort by something, but I'm not sure what.
The $quotes array I am passing looks like this;
Array
(
[0] => Array
(
[Quote] => Array
(
[id] => 1
[attendance_id] => 15
[quote_text] => Hello
)
[Attendance] => Array
(
[id] => 15
[person_id] => 2
[trip_id] => 7
[Person] => Array
(
[id] => 2
[first_name] => John
[last_name] => Smith
)
[Trip] => Array
(
[id] => 7
[location] => La Plagne
[year] => 2000
[modified] =>
)
)
)
I would be immensely grateful if someone could suggest how I might be able to sort by the the first_name of the Person associated with the Quote. I suspect my syntax is wrong, but I have not been able to find the answer. Is it not possible to sort by a second level association in this way?
I am pretty much brand new with cakephp so please be gentle.
Thanks very much in advance.
I've had the similar problem awhile back. Not with sort though. Try putting the associated table in another array.
echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');
change to:
echo $this->Paginator->sort('Name', array('Attendance' => 'Person.first_name'));
Hope this helps
i'm also looking for help with this.
so far i've found that you can sort multi level associations in controller's pagination options after using the linkable plugin https://github.com/Terr/linkable.
but it breaks down when you try to sort form the paginator in the view. i'm using a controller for magazine clippings. each clipping belongs to an issue and each issue belongs to a publication.
$this->paginate = array(
"recursive"=>0,
"link"=>array("Issue"=>array("Publication")),
"order"=>array("Publication.name"=>"ASC",
"limit"=>10);
after debugging $this->Paginator->params->paging->Clipping in the view, you can see that the sort is described in two separate places, "defaults" and "options". the sort info needs to be present in both for it to work in the view.
here is after setting order in controller:
[Clipping] => Array
(
[page] => 1
[current] => 10
[count] => 6685
[prevPage] =>
[nextPage] => 1
[pageCount] => 669
[defaults] => Array
(
[limit] => 10
[step] => 1
[recursive] => 0
[link] => Array
(
[Issue] => Array
(
[0] => Publication
)
)
[order] => Array
(
[Publication.name] => ASC
)
[conditions] => Array
(
)
)
[options] => Array
(
[page] => 1
[limit] => 10
[recursive] => 0
[link] => Array
(
[Issue] => Array
(
[0] => Publication
)
)
[order] => Array
(
[Publication.name] => ASC
)
[conditions] => Array
(
)
)
)
and here is after using $this->Paginator->sort("Publication","Publication.name");.
notice the options array is empty.
[Clipping] => Array
(
[page] => 1
[current] => 10
[count] => 6685
[prevPage] =>
[nextPage] => 1
[pageCount] => 669
[defaults] => Array
(
[limit] => 10
[step] => 1
[recursive] => 0
[link] => Array
(
[Issue] => Array
(
[0] => Publication
)
)
[order] => Array
(
[Publication.name] => DESC
)
[conditions] => Array
(
)
)
[options] => Array
(
[page] => 1
[limit] => 10
[recursive] => 0
[link] => Array
(
[Issue] => Array
(
[0] => Publication
)
)
[order] => Array
(
)
[conditions] => Array
(
)
)
does one really need to modify the paginator class to make this work?
UPDATE:
i found out the problem:
in the core cake controller paginator merges default and options to create the find query.
but the options array is empty when using linkable to sort. because options is listed after default it overrides default and the empty array replaces the default array of options.
solution to this is extending the paginate function inside of app_controller.php and unsetting the options array order value if it is empty:
(line 1172 in cake/libs/controller/controller.php)
if(empty($options["order"])){
unset($options["order"]);
}
then the options will not be overwritten by thte blank array.
of course this should not be changed inside of controller.php, but put it in app_controller.php and move it to your app folder.
On CakePHP 3 this problem can be solved by adding 'sortWhitelist' params to $this->paginate on your controller.
$this->paginate = [
// ...
'sortWhitelist' => ['id', 'status', 'Attendance.Person.first_name']
];
And then in your view:
echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');
This is noted in the docs:
This option is required when you want to sort on any associated data, or computed fields that may be part of your pagination query:
However that could be easily missed by tired eyes, so hope this helps someone out there!