OK, I've got a simple form submitted to my controller.
In my controller:
$thedata = $this->request->data;
debug($thedata);
Results in:
[
'number' => '102',
'color' => 'blue',
'size' => 'large'
]
I want to extract from this result an array that is the input field names. The result should be
$thearray = ['number','color','size'];
What is the best way to do this in CakePHP? I'm using using 3.5.2.
Will Hash::extract do this? If so, how would that work?
Thanks in advance for any advice.
D.
This solved my issue:
$thearray = array_keys($thedata);
Related
To begin with, sorry for my bad English. I have column:
$table->json('images')->nullable();
Table is called albums. In images I would like to store an array with image names, but how can I every time add to that json? My code right now:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$album->update([
'images' => $request->image->name <= tried to do something.. My uploader everytime calls this function, so I need to get current IMAGES column value, and add new. Like ['first image name', 'second image name'], after update this must be ['first image name', 'second image name', 'third image name'] not just ['third image name']
]);
$album->save();
I need to do like laravel increment function, but which works only with int looks like. How can I do that? Thanks in advance!
I think its not possible to directly change the JSON as your images data would return as a String.
First get the JSON as a string, then update the Object and update the JSON.
So something like:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$images = json_decode($album->images);
array_push($images, $request->image->name);
$album->update(['images' => $images]);
$album->save();
Note: I didn't check this code, but this should give you an idea of how to handle this
Good luck
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$arra = json_decode($album->images);
$arra[] = $request->image->name;
$album->update([
'images' => $arra
]);
$album->save();
I hope help you
I am using scotthuangzl\googlechart\GoogleChart to make dashboard.
I want it to be dynamic and draw a pie chart using count of my records.
but when I pass value using variable to piechart it does not show required output.
$contacts = Contacts::find()->count();
$companies = Companies::find()->count();
$user = User::find()->count();
use scotthuangzl\googlechart\GoogleChart;
echo GoogleChart::widget(array('visualization' => 'PieChart',
'data' => array(
array('Task', 'Hours per Day'),
array('companies', $companies),
array('contacts', $contacts),
array('user', $user),
),
and it gives output as=
it only shows small angle with gray color .. and dont know from where this "other" field came from..
can anyone help to solve this?
in Yii2 the count() return a "string"
please try to parse the result using (int)
echo GoogleChart::widget(array('visualization' => 'PieChart',
'data' => array(
array('Task', 'Hours per Day'),
array('companies', (int)$companies),
array('contacts', (int)$contacts),
array('user', (int)$user),
),
Already received a great answer at this post
Laravel Query using GroupBy with distinct traits
But how can I modify it to include more than just one field. The example uses pluck which can only grab one field.
I have tried to do something like this to add multiple fields to the view as such...
$hats = $hatData->groupBy('style')
->map(function ($item){
return ['colors' => $item->color, 'price' => $item->price,'itemNumber'=>$item->itemNumber];
});
In my initial query for "hatData" I can see the fields are all there but yet I get an error saying that 'colors', (etc.) is not available on this collection instance. I can see the collection looks different than what is obtained from pluck, so it looks like when I need more fields and cant use pluck I have to format the map differently but cant see how. Can anyone explain how I can request multiple fields as well as output them on the view rather than just one field as in the original question? Thanks!
When you use groupBy() of Laravel Illuminate\Support\Collection it gives you a deeper nested arrays/objects, so that you need to do more than one map on the result in order to unveil the real models (or arrays).
I will demo this with an example of a nested collection:
$collect = collect([
collect([
'name' => 'abc',
'age' => 1
]),collect([
'name' => 'cde',
'age' => 5
]),collect([
'name' => 'abcde',
'age' => 2
]),collect([
'name' => 'cde',
'age' => 7
]),
]);
$group = $collect->groupBy('name')->values();
$result = $group->map(function($items, $key){
// here we have uncovered the first level of the group
// $key is the group names which is the key to each group
return $items->map(function ($item){
//This second level opens EACH group (or array) in my case:
return $item['age'];
});
});
The summary is that, you need another loop map(), each() over the main grouped collection.
I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs
I am trying to extract some data from an array with the following syntax:
#entries_from_db = XrEntry.find(:all, :conditions => [:FeedURI => uri ], :select => 'json')
The :FeedURI is the record that contains an array with uri's ["123456", "23345", "4453"]
The uri is the variable wich contains the current uri.
The statement I'm trying to make is 'select JSON from XrEntry where FeedURI contains uri'
Im stuck on the part to access the array and always get several error msg's when I'm trying different code.
Does anyone has an idea?
Thanks!
I solved it with this syntax
#entries_from_db = XrEntry.find(:all, :conditions => ["FeedURI like ?", "%#{uri}%"] , :select => 'json')
the "%#{your_rails_variable}%" is needed to read in an array
You seem to have switched the condition syntax. you chould start with the db attribute and then the variable.
#entries_from_db = XrEntry.find(:all,
:conditions => { :uri => FeedURI },
:select => 'json')
That will return an array of XrEntry objects with only the json attribute present. To get an array of only the json data you could map it like this:
#json_array = #entries_from_db.map(&:json)