webform_get_submissions() in Drupal 7 return all the Webform submissions, the problem is that I want to return the submission that falls between 2 date
ex:
get webform submissions that have been submitted between 16 April 2018 and 28 April 2018.
Let me share a workaround that I came up with, maybe it will help you to find a full-size solution.
You actually can use a filter in the function webform_get_submissions not only by nid or sid but apparently by any other field. In this case, there is possible to use a filter by a field named submitted:
$submissions = webform_get_submissions(array('nid' => $node->nid, "submitted" => "1503753434"));
It will result in recieiving a submission which field submitted is exactly equal to 1503753434. To get all submissions which are, for example, newer than 1503753434 one way is inside a file includes/webform.submissions.inc for the function webform_get_submissions_query change this part:
foreach ($filters as $column => $value) {
$pager_query->condition($column, $value);
}
to this part:
foreach ($filters as $column => $value) {
if ($column == "submitted") {
$pager_query->condition($column, $value, ">");
} else {
$pager_query->condition($column, $value);
}
}
This would result in getting submissions which are newer than 1503753434. By analogy we can add < filter or any other filters.
The problem with this workaround is that it requires to change a source file of Drupal and also it requires implementing a simple parsing by hand in case we want to have several conditions for one field name.
Related
I am using drupal and I have webform where there is field date of production and there is calendar in this field When I put date 2020-09-30 in this field and submit my form it save this field wrongly in salesforce like this:
6/9/2022 it changed to year 2022 and also changed month and day
following is the condition I have put in salesforce function
function ublox_salesforce_salesforce_webforms_save_submission_alter(&$fields, $context) {
foreach ($fields as $key => $value) {
if (in_array($key, ["Date_Prototype__c", "Date_Production__c", "NBIOT_Date_of_Field_Trial__c", "NBIOT_Date_Lab_Trial__c"])) {
$fields[$key] = trim($value);
if ($fields[$key] == 'Date_Production__c') {
$fields[$key] = format_date(strtotime($fields['Date_Production__c']), 'medium', 'm-d-Y');
}
}
You can see my if condition for 'Date_production__c' ( date of production start )
Where I am doing wrong? I am unable to find solution anyone can help me please.
I have also attached the screenshot of my field where I have put date 2020-09-30 but it reached differently like 6/9/2022
Thanks in advance
What's the outcome of this format_date(strtotime($fields['Date_Production__c']), 'medium', 'm-d-Y');, can you write it to log or echo or something?
Salesforce API expects dates in 2020-09-14 format (so that'd be Y-m-d?) and datetimes in 2020-09-14T18:07:30Z format. See https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
I have a project that I need to finish before July, it's for school.
It was all going well until I started having problems with an array that comes from my database.
Please keep in mind that this is my first time with PHP and I'm learning as I am doing:
My database has a table named "Pessoas" and another named "Pessoas_Pessoas". The second was to keep the record of relationships between entries on the "Pessoas" table ( for example, if the ID 1 had a relationship with the ID 2, the information would be kept on "Pessoas_Pessoas" ).
The tricky part is that I am supposed to use PHP to prevent the user from duplicating a relationship.
A good example would be Facebook's friend system:
If I would be friend's with John, it wouldn't appear his profile on the "People you might know" section.
Since I am using MVC and Slim Framework, I managed to get what I want from the database on the model, but I have this problem on the Controller:
This is an image from the table "Pessoas_Pessoas", being id_PessoasA the id of the profile being visualized and id_PessoasB the id of the profile I'm trying to make a relationship with
I managed to get 5 profiles to test this feature and I managed to get those relationships as well, but the problem is that when I try to verify if the id of each profile is the same has id_PessoasB to then unset from the array, but it only works one time even if the condition is true. Here's the code:
foreach ($resultadoRelacao as $key => $value) {
foreach ($resultadoRelacao0 as $key0 => $value0) {
if ($value0['id'] == $value['id_PessoasB']) {
$indice = array_search($value0['id'], $resultadoRelacao0);
unset($resultadoRelacao0[$indice]);
}
}
}
Thanks for your time and please ask anything if needed. I'm not really sure what's needed anymore, I'm stuck for 2 weeks.
As you are using the foreach() to go through the array and you have matched the element against the one your looking for, you can then use the key value (in this case $key0) of the foreach() to remove the element your currently looking at...
foreach ($resultadoRelacao as $key => $value) {
foreach ($resultadoRelacao0 as $key0 => $value0) {
if ($value0['id'] == $value['id_PessoasB']) {
unset($resultadoRelacao0[$key0]);
}
}
}
foreach ($resultadoRelacao as $key => $value) {
foreach ($resultadoRelacao0 as $key0 => $value0) {
if ($value0['id'] == $value['id_PessoasB']) {
if(($indice = array_search($value0['id'], $resultadoRelacao0)) !==false){
unset($resultadoRelacao0[$indice]);
}
}
}
}
In my table there are 5 rows and i want to get data from the table on the basis of inputs .the rows are name,age,height,weight and class.
If user inputs age= 12 and weight=40 then person with 12 age as well as 40 weight should be shown.
if user inputs only name=jack then persons with name jack should be shown.
if user inputs all five entries then all entries should be matching.
user can input one field or all five fields.
I suggest to use the inputs to build an associative array which contains the filters you want to apply.
$filters = array();
if (!empty($this->input->post('name')))
$filters['name'] = $this->input->post('name');
if (!empty($this->input->post('age')))
$filters['age'] = $this->input->post('age');
if (!empty($this->input->post('height')))
$filters['height'] = $this->input->post('height');
if (!empty($this->input->post('weight')))
$filters['weight'] = $this->input->post('weight');
if (!empty($this->input->post('class')))
$filters['class'] = $this->input->post('class');
Then, when you are building your query, you just need to iterate over the array:
foreach ($filters as $key => $value) {
$this->db->where($key, $value);
}
Note the use of empty() instead of isset or any manual comparison. Docs reference.
Again I find myself at the mercy of the stackoverflow community!
I've gone over to use CodeIgniter for my PHP projects and it's been a breeze so far, hoever I've gotten stuck trying to update a database field with some post data.
My array is the usual: array(name => value, name => value, name => value);
which again is populated from the submitted $_POST data.
Similarly to the array, I have a database table with 2 fields: setting and value, where the names under setting corresponds to the array keys and value to the array keys' value.
(Did I explain that right?)
Nonetheless, I've been trying for a little while now to get this to work as it should, but, I'm really just waving my hands around in the dark.
I hope some of you bright minds out there can help me with this annoying issue!
Edit:
Thanks to everyone who replied! I managed to produce the result that I wanted with the following code:
foreach ($form_data as $key => $val)
{
$this->db->where ('setting', $key);
$this->db->set ('value', $val);
$this->db->update ('recruitment');
}
Now, I tried following up with this by adding:
if ($this->db->affected_rows() >= '1') { return true; }
return false;
To my model, and
if ($this->RecruitmentSettings->Update($form_data) == TRUE)
{
redirect('recruitment/success');
}
to my controller, but it's not working as expected at all. Any ideas what I'm doing wrong?
There are a lot of questions here. Do you already have values in the database and you want to update them? Or do you want to put in new data every time? The answer depends on that.
What you want is the insert_batch() or update_batch() methods of the active record class (if that's what you're using for the db).
foreach($post_array as $key => $value)
{
$settings[] = array(
'setting' => $key,
'value' => $value
);
}
$this->db->insert_batch('your_db_table', $settings);
OR, for updates:
$this->db->update_batch('your_db_table', $settings, 'setting');
You could do a query to check for settings and do insert_batch or update_batch depending on if there are results. If you wanted to insert every time, you could delete the rows in the table before you do the insert. I wouldn't do that without a transaction, however.
So you want to store the array data in the database? You could do this
Model
foreach ($data as $key => $item)
{
$this->db->set ('setting', $key);
$this->db->set ('value', $item);
$this->db->insert ('table_name');
}
return ($this->db->affected_rows() > 0);
Controller
if ($this->RecruitmentSettings->Update($form_data))
{
redirect('recruitment/success');
}
else
{
echo "error";
}
Attached code taken from cakephp bakery, where someone uploaded a sample about custom validation rules.
class Contact extends AppModel
{
var $name = 'Contact';
var $validate = array(
'email' => array(
'identicalFieldValues' => array(
'rule' => array('identicalFieldValues', 'confirm_email' ),
'message' => 'Please re-enter your password twice so that the values match'
)
)
);
function identicalFieldValues( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
}
In the code, the guy used a foreach to access an array member which he had its name already!
As far as I understand - it's a waste of resources and a bad(even strange) practice.
One more thing about the code:
I don't understand the usage of the continue there. it's a single field array, isn't it? the comparison should happen once and the loop will be over.
Please enlighten me.
In the code, the guy used a foreach to access an array member which he had its name already! As far as I understand - it's a waste of resources and a bad(even strange) practice.
The first parameter is always an array on one key and its value, the second parameter comes from the call to that function, in a block named as the key... So, all you need is to send the key and no need to iterate
The code uses foreach to iterate through $field, which is an array of one key value pair. It all starts when the validation routine invokes identicalFieldValues, passing it two values - $field, which would be an array that looks like:
array (
[email] => 'user entered value 1'
)
The second parameter $compare_field would be set to the string confirm_email.
In this particular case, it doesn't look like it makes a lot of sense to use foreach since your array only has one key-value pair. But you must write code this way because CakePHP will pass an array to the method.
I believe the reason why CakePHP does this is because an array is the only way to pass both the field name and its value. While in this case the field name (email) is irrelevant, you might need in other cases.
What you are seeing here is one of the caveats of using frameworks. Most of the time, they simplify your code. But sometimes you have to write code that you wouldn't write normally just so the framework is happy.
One more thing about the code: I don't understand the usage of the continue there. it's a single field array, isn't it? the comparison should happen once and the loop will be over. Please enlighten me.
Indeed. And since there are no statements in the foreach loop following continue, the whole else block could also be omitted.
A simplified version of this would be:
function identicalFieldValues($field=array(), $compare_field=null)
{
foreach ($field as $field) {
$compare = $this->data[$this->name][$compare_field];
if ($field !== $compare) {
return FALSE;
}
}
return TRUE;
}
And I agree with you, the loop only goes through one iteration when validating the email field. regardless of the field. You still need the foreach because you are getting an array though.