I've added a field in my admin view to upload a file. The upload part is working fine but the form doesn't add the file's name to the database on saving. I've seen a similar problem here on stack overflow that has been answered but they are using JRequest::setVar which is now deprecated: How to Save Uploaded File's Name on Database
I need to add to the "jform" array but when I use: $jinput->set('jform',array('foo' => 'bar'));
it overwrites the existing 'jform' array that contains the other data entered in the form:
JInput Object (
[data:protected] => Array (
[jform] => Array (
[foo] => bar
...
Does anyone have any advice on how to add to the jform array and not overwrite it?
Ok so I feel a little foolish - I've solved my own question!
What I did was to get all the other jform data with: $jinput->get('jform', NULL, NULL); and then used array_merge to add the other array.
I then used: $jinput->post->set('jform',$mergedArray); to put the array back into the POST.
Hope this helps someone
Related
i'm struggling with a tricky problem.
I'm creating a new extension for typo3, this extension need to retrive all the news matching the demand-array.
Actually my code looks like this:
$demand = $this->objectManager->get(NewsDemand::class);
$demand->setActionAndClass(__METHOD__, __CLASS__);
$demand->setStoragePage(18);
$res = $this->findDemanded($demand);
the main problem is that
$res->count()
this code retrieve correctly the number of news matching the array but if i try with
$res->getFirst()
this return nothing.
In theory the code $this->findDemanded($demand); will return an array with all the news matching the demand-array but the results is empty.
If i use $this->findDemandedRaw($demand); it returns the correct plain query (tested in phpmyadmin and returns the correct values).
I'm pretty new on TYPO3 and i don't know how it works really well, some one has any hints about that?
The news extension key is: news
Thanks in advance!
Fixed:
The returned value was an object not an array, so now i'm able to iterate every item inside the object and return it as a json.
My bad, sorry :)
I am trying to make a custom voting plugin for wordpress.
I want to use post_meta to store the data.
It should have two sets of informations (user_id and a timestamp) for every vote in every post.
So I set up an array with the data:
$up_vote = Array(
'ls_user_id' => $current_user->ID,
'ls_timestamp' => current_time('mysql')
);
The data inside the post_meta should be an Array of these Arrays.
To do this, I first create an empty Array, get the old data, and then push my first array inside like this:
$ls_up_votes = Array();
$ls_up_votes = get_post_meta(get_query_var('ls_id'), 'ls_up_votes');
array_push($ls_up_votes, $up_vote);
And later I store it like this:
update_post_meta(get_query_var('ls_id'), 'ls_up_votes', $ls_up_votes);
Now the problem:
Always when I try to get the data (and work with it), it gives me an Array which has only one element (sizeof() returns 1).
How can I store a multi-dimensional Array here?
What am I doing wrong?
thanks for your time! :)
[Updated]
I just noticed that you're using array_push; instead you should be using array_merge. Otherwise you're going to push an array into the existing array making it multi-dimensional, i.e. see note.
You'll also want to make sure you're using the correct params in get_post_meta, see below.
Make sure to use return meta as an array and not a serialized string, get_post_meta has a third paramater that should be set to TRUE if you want an the array returned.
Example:
get_post_meta( $post_id, 'ls_up_votes', TRUE );
Finally, a simple, straight-answer question :D
Background: My company has changed the way that that they handle when certain sites are used. Previously, certain sites were only delivered to on certain weeks; now, every site delivers every week. They have asked me to get rid of the weeks field on their "add new site" form. The link between week and site is still necessary for the code to work, however, so I am trying to hide the field and populate it with every single week.
Auto-populating it is simple. I just do this:
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5')));
And when I debug the data getting sent from the form, I get the following, which is exactly the way I want it to work.
'Week' => array(
'Week' => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5'
)
So the next step is simply hide that input. Easy, right? I just do one of the following two things to the form:
echo $this->Form->hidden('Week', array('value'=>array('1','2','3','4','5')));
or
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'), 'type'=>'hidden'));
All I have done is change the type to hidden. But now, the data returned from debugging the data from the form looks like this:
'Week' => array(
'Week' => '1 2 3 4 5'
So my question is, what is the difference in the way data is handled between a normal "input" field and a "hidden" field. Why does that difference occur, and why is it important? And for this particular issue, how do I get hidden data to behave in the same way as normal input data?
As you said in the comments, Week is a multiple choice input, so that's why the difference in value handling. If the input is something that allows multiple choice (like multiple select or checkboxes), then it's logical all values comes as an array. But the hidden type is basically a hidden text input (ok, maybe not, but I'm simplifying). Look, if you put
echo $this->Form->input('Week', array('type'=>'text', 'value'=>array('1','2','3','4','5')));
you'll get the same string as you have with the input field, so it isn't a matter of if it's hidden or not.
Now, for your case, you can add the week values as an string and change the way you handle the value on post, as a string instead of an array.
But, if by some reason that's not a possibility (too much code to change, etc), then keep the same code you have now, and hide it with css. Something like
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'),
'div'=>array('style'=>'display:none')));
should do the trick (maybe a few more styles needs to be added, try with just that first).
I don't like the css solution that much, browsers can interpret css different ways and etc etc... But it fits your needs and you don't have to change anything else.
EDIT
On second though, it is possible to achieve the same array output with just hidden fields. It isn't pretty though. You have to change the name of every input to tell php it's an array, like this
echo $this->Form->input('Week1', array('type'=>'hidden', 'value'=>1, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week2', array('type'=>'hidden', 'value'=>3, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week3', array('type'=>'hidden', 'value'=>4, 'name'=>'data[Week][Week][]'));
The important part is the name option for every input, see the array reference? Specially the last empty []. This is actually how cakephp sends all data as an array. Have you tried to create a form without the FormHelper in cake? You wouldn't get the post data in $this->request->data['form_name'] unless you set the input names for all your form fields.
Cake sends all post data inside the $data array by naming all the inputs with <input name="data[ModelName][field_name]" and for multiple inputs is <input name="data[ModelName][field_name][]" (with the empty array at the end, so it gets numerically indexed). You can see that using any html inspector on any other form created with the formhelper in cake.
I hope it's clear... I'll stick with the css solution here. The multiple hidden inputs seems messier.
SOLVED!
I`m trying save data from array in variable. I have in controller:
$data = array('upload_data' => $this->upload->data());
and I know that in this array are data about uploading file. One with this date is "file_name", and I want to save this value in controller at variable. I try with:
$image_name= $data['file_name'];
But this not working. I use CodeIginter 2.1.3 framework.
Good solution is: $data['upload_data']['file_name'];
Thanks for help!
$data = $this->upload->data();
then $data['file_name'] will work. The way you're doing it you're burying an array inside another array. Pretty sure you could call it with the following but it's still pointless
$data['upload_data']['file_name']
What version of CodeIgniter are you using?
With 2.1.3 it should work - Check out the last few paragraphs of the documentation at: http://codeigniter.com/user_guide/libraries/file_uploading.html
The $data['file_name'] will give you the file name of the uploaded file if it has been successful. To get the full name/path use the other fields in the array.
Hi I am having an incredibly hard time with what should be a simple issue.
As of CodeIgniter 1.7, '$this->input->post();' supported arrays but I cannot for get the values into the array for some reason. I have 7 check boxes that store into an array 'services[]' as you can see by this example view:
<?php $servicesdata = array (
'name' => 'services[]',
'value' => 'in_home_care',
);
echo form_checkbox($servicesdata, set_checkbox('services[]', 'in_home_care', FALSE)); ?>
I'm quite certain this is the correct fashion because the forms do validate nicely if something goes wrong. Now I start to have issues when storing the values. I have 7 columns that need to have some sort of value... at this point I don't care but ideally it would be a boolean (a binary would work okay too). Here is what I have so far in my controller that everyone claims should work but just does not:
$c = new Client($servicesdata);
$c->first_name = $this->input->post('first_name', TRUE);
$c->in_home_care = $this->input->post('services[in_home_care]');
You can see the string I put for an example that works perfectly and inserts into a VARCHAR type while the array won't go into the database whatsoever. I feel as if that I am missing something here - namely the 'value' in the array but I'm just not sure where to go from here. Any help would be much appreciated because the only method I can get to work sacrifices my checkbox validation! :(
If your checkbox is unchecked, the value of the checkbox will not be entered into the $_POST array. This is probably where you are having problems.
There are two ways around this. Either have one box checked by default, or use HTML like the following (which may or may not be best practice/valid, but has worked for me in the past).
<input type="hidden" name="services" value="foo" />
<input type="checkbox" name="services" value="in_home_care" />
In the event that the box is not checked, the value "foo" for the name value "services" will be passed to the $_POST array.
First of all, there is some redundancy in your form (I think). You can set the attributes of your checkbox in the array, including whether it is checked or not:
$servicesdata = array (
'name' => 'services[]',
'value' => 'in_home_care',
'checked' => FALSE,
);
echo form_checkbox($servicesdata);
secondly, because you're naming it in an array, the object needs to be accessed after being assigned to another variable:
$checkbox_array = $this->input->post('services');
$service_type=$checkbox_array[0];//will give you 'in_home_care', [1] would be next in array and so on
A way to store those choices in the db is to create a string out of them and store that.
$this->input->post('colors') ? $colors=implode('-',$this->input->post('colors')) : $colors='';
That checks to see if anything was actually checked in the colors array. If so, create a string out of the array values seperated by dashes. Else assign empty to colors.
Then after you read the db string back:
$profile_data['colors']=explode('-',$row->colors);
Then you can feed those values back into the form inputs.