In cakephp 2.6.8, I am trying to query a table named "mytable" in order to count rows having common values.
So, this is my query :
$zone = $this->MyModel->find('all', array(
'fields' => array('zone_id', 'COUNT(zone_id) as count'),
'group' => 'zone_id'
));
Cakephp returns this result :
zone => array(
0 => array(
MyModel => array("zone_id" = 1) ),
0 => array("count" = 77)
),
1 => array(
MyModel => array("zone_id" = 2) ),
0 => array("count" = 55)
),
2 => array(
MyModel => array("zone_id" = 3) ),
0 => array("count" = 23)
)
);
All I need is something like this :
zone = array(
0 => array("zone_id" => 1, "count" => 77),
1 => array("zone_id" => 2, "count" => 55),
2 => array("zone_id" => 3, "count" => 23)
);
How could I do to get this working without creating any Virtual Field?
Thank you for your help !
Related
Hello I m creating simple blog site here view blog at landing page but if user choose any category the blog under this category only listed . My problem is when user select category from right sidebar it return all table records.
When i print the query it return :
array(
'log' => array(
(int) 0 => array(
'query' => 'SELECT `Category`.`category_id`, `Category`.`name` FROM `cakeBlog`.`categories` AS `Category` WHERE 1 = 1',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
),
(int) 1 => array(
'query' => 'SELECT `Post`.`id`, `Post`.`category_id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id` FROM `cakeBlog`.`posts` AS `Post` WHERE 1 = 1 LIMIT 20',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
)
),
'count' => (int) 2,
'time' => (float) 0
)
Here is my controller file
PostsController.php
In index function I will check whether category id exist or not?
Please help me...
Thanks
Use the following block where you want to fetch categories
if(is_null($categoryId)) {
$this->Paginator->setting = array(
'limit' =>'10',
'order' => array('Post.id' =>'Desc')
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$this->set('posts', $arrPosts);
} else {
$condition = array('Post.category_id' => $categoryId,'limit' =>'10');
$da = $this->Paginator->setting = array(
'conditions' => $condition
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$log = $this->Post->getDataSource()->getLog(false, false); debug($log);
$this->set('posts', $arrPosts);
}
I have this php associative input array.
array(
(int) 0 => array(
'Data' => array(
'id' => '12',
'type_id' => '1',
'data_value' => '35.5000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
),
(int) 1 => array(
'Data' => array(
'id' => '11',
'type_id' => '1',
'data_value' => '33.7000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
)
I want to convert it to this output array;
array(
(int) 0 => array(
(int) 0 => array(
'v' => (int) 1
),
(int) 1 => array(
'v' => '35.5000'
)
),
(int) 1 => array(
(int) 0 => array(
'v' => (int) 2
),
(int) 1 => array(
'v' => '33.7000'
)
)
The element data_value is extracted from the input array into the output array.
The code I have written looks like this;
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
)
);
}
Unfortunately, this code does not work. The output from this code looks like this;
array(
(int) 0 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
),
(int) 1 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
)
What did I do wrong? Why do I get the error "maximum depth reached"? How can I retrieve the desired output array? I am actually doing this in cakephp.
Thank you very much for any help.
That is one wrapping array() to many:
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
);
}
You can see this working here. It is good use to prepare your code in services like ideome or plnkr in order to make it easier for people to help you with debugging.
As far as I know, the problem is related to a setting for PHP http://www.hardened-php.net/suhosin/configuration.html#suhosin.executor.max_depth . Could you check with your hosting provider?
I have an array:
$initialarray = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
2 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
),
3 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
4 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
);
What would be the best way to structure it (to group the resulting sub-arrays) depending first on the 'unit' field's values, and then depending on the 'class' field's values, like so:
$resultarray = array(
// array of all the sub-arrays of 'unit' = 1
$unit[1] = array(
// array of all the sub-arrays of 'unit' = 1 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
)
)
// array of all the sub-arrays of 'unit' = 1 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
)
)
)
// array of all the sub-arrays of 'unit' = 2
$unit[2] = array(
// array of all the sub-arrays of 'unit' = 2 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
)
// array of all the sub-arrays of 'unit' = 2 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
)
)
)
I have asked a similar question here and got a working answer for only one iteration, i.e. for only structuring the array by one of the fields. But I could not make the same solution work for multiple iterations, i.e. for more than one field.
Also, is there a solution to structure a multidimensional array depending on more than two fields?
I think it's not a way of asking the question. It is very simple , you can do this by playing with arrays,keys and etc.... So first you should try hard for the problem. After If you have any problem in the middle of your tries then you can ask that here. I have solved your problem here is the complete code , but next time please do some work and then only post the problem. Never ask for the code.
foreach ($initialarray as $key1=>$val1)
{
foreach ($val1 as $key2=>$val2)
{
if($key2=='unit')
{
$num=$val2;
if($val2!=$num)
$testarr['unit'.$val2]=array();
}
if($key2=='class')
{
$testarr['unit'.$num]['class'.$val2][]=$val1;
}
}
}
print_r($testarr);
I must offer a better way for you and future researchers...
You only need one loop, and you merely need to nominate the result array's key values before using [] to "push" new data into the deepest subarray.
*there is absolutely no need for any condition statements or a second loop.
Code: (Demo)
$initialarray = [
['unit' => 1, 'class' => 1, 'value' => 'string1'],
['unit' => 1, 'class' => 2, 'value' => 'string2'],
['unit' => 1, 'class' => 2, 'value' => 'string3'],
['unit' => 2, 'class' => 1, 'value' => 'string4'],
['unit' => 2, 'class' => 2, 'value' => 'string5']
];
foreach ($initialarray as $row) {
$result[$row['unit']][$row['class']][] = $row;
}
var_export($result);
Output:
array (
1 =>
array (
1 =>
array (
0 =>
array (
'unit' => 1,
'class' => 1,
'value' => 'string1',
),
),
2 =>
array (
0 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string2',
),
1 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string3',
),
),
),
2 =>
array (
1 =>
array (
0 =>
array (
'unit' => 2,
'class' => 1,
'value' => 'string4',
),
),
2 =>
array (
0 =>
array (
'unit' => 2,
'class' => 2,
'value' => 'string5',
),
),
),
)
If I may express myself in the following manner: I only see the front-end of your problem and know nothing about its back-end, e.g. "Where does the data come from?", "How is it collected and stored", etc. so my answer might not be a real help but still I'll give my "tuppence".
If you can store all that data in a relational database (in form of table(s)) it would be much more easier and faster(!) to select the needed data from the database instead of rearranging arrays, which will take some more time in comparison.
Just as an example you might then select (and store it into an array) all items which have unit = '1' and / or all items which have class = '2'. That would make life much more easier IMHO, than having all the data in a multidimensional array and then try to sort it / rearrange it. Especially if you do that based on more than one property.
I have the following array that I want to insert into the table with the fields groupi_id, application_id and grant_id
the array is
array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
)
I want to insert the rows that every sub array goes with array key. so there will be 2 rows inserted in above case like this
insert into table (group_id. application_id, grant_id) Values (72, 1, 56);
insert into table (group_id. application_id, grant_id) Values (72, 2, 57);
howd I do that?
You can use Cake's Set::classicExtract() to pull out the values. I assume you know how to then save them to the DB.
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html
In your case, something like (untested):
$result1 = Set::classicExtract($a, '{n}.{s}.{s}.0');
$result2 = Set::classicExtract($a, '{n}.{s}.{s}.1');
If you need the keys, you can extract those first:
$fields = Set::classicExtract($a, '{n}.{s}.{s}');
I have tried this one
$d = array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
);
for ($i=0; $i<count($d['ApplicationsGrant']['group_id']); $i++) {
echo $d['ApplicationsGrant']['group_id'][$i]."<br/>"; //outputs 72, 72
}
I am working on a web application and would like to improve my code a little bit.
This is the SQL table structure I made for the application:
Now my question: Is there an easy way in PHP to select (with join or whatever?) the information from one specific event_id on all tables and save them in an array with followed structure for example:
$events = array(
'event_id' => 1,
'event_name' => '{"de":"Weltwirtschaftsforum","fr":"Forum \u00e9conomique mondial","it":"Forum Economico Mondiale"}',
'event_description' => '{"de":"Description DE","fr":"Description FR","it":"Description IT"}',
'event_lastedit' => 2011-12-01 10:23:35,
'locations' => array(
array(
'location_id' => 1,
'location_name' => 'Bern',
'location_date' => '01.01.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 1,
'timestamp_time' => '10:30',
'timestamp_seats' => 30
),
array(
'timestamp_id' => 2,
'timestamp_time' => '16:30',
'timestamp_seats' => 40
)
)
),
array(
'location_id' => 2,
'location_name' => 'Davos',
'location_date' => '02.02.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 3,
'timestamp_time' => '12:30',
'timestamp_seats' => 50
),
array(
'timestamp_id' => 4,
'timestamp_time' => '15:30',
'timestamp_seats' => 60
)
)
)
)
);
I hope the question is clear enough.
Greetings
Spinne
Edit:
What i did so far:
$event = $event_db->querySingle("SELECT * FROM rf_events WHERE event_id={$event_id}", true)
$rf_locations = $event_db->query("SELECT * FROM rf_locations WHERE event_id={$event_id}");
$locations = array();
$timestamps = array();
$counter = 0;
// Loop sql query result and save entries in $events array.
while ( $row = $rf_locations->fetchArray() ){
$locations[$counter] = array(
'location_id' => $row['location_id'],
'location_name' => json_decode($row['location_name'], true),
'location_date' => $row['location_date'],
'location_deadline' => $row['location_deadline']
);
$rf_timestamps = $event_db->query("SELECT * FROM rf_timestamps WHERE location_id={$row['location_id']}");
$counter2 = 0;
while ( $row2 = $rf_timestamps->fetchArray() ){
$locations[$counter]['timestamps'][$counter2] = array(
'timestamp_id' => $row2['timestamp_id'],
'timestamp_time' => $row2['timestamp_time'],
'timestamp_seats' => $row2['timestamp_seats']
);
$counter2++;
}
$counter++;
}
SELECT * FROM rf_events, rf_locations, rf_timestamps WHERE
rf_locations.event_id = rf_events.event_id AND
rf_timestamps.location_id = rf_locations.event_id
Then, use add the data into php accordingly.
You can refer to: MYSQL JOIN