Gravity forms - Get entry data without entry numbers - php

I need to grab data of entry after post submit, i.e:
function update_campaign_amount($entry, $form) {
$donate_to_personal_camp_form = get_option('als_donate_to_personal_camp_form');
$main_campaign_form = get_option('pb_main_campaign');
if ( $form['id'] == $donate_to_personal_camp_form ) {
$campaign_id = $entry['55'];
$user_donation = $entry['4'];
$total_donations = get_post_meta($campaign_id, 'campaign_current_amount', true);
if (is_numeric($user_donation)) {
update_post_meta($campaign_id, 'campaign_current_amount', $total_donations + $user_donation);
}
}
}
add_action( 'gform_after_submission', 'update_campaign_amount', 10, 2 );
As you can see in the code the two variable $campaign_id and $user_donation gets the value of a specific entries, it works but it doesn't good enough, because today the number of the entry is 55, tomorrow it could be something else.. Is there any other options to get entry value? For example, I have only one field on that form with the type "total", and I need it's value, so I want to do something like:
$entry['type'] == 'numer'
But I could find anyway to get entry data without the use of absolute numbers.
Anyone know more flexible way?

$entry will contain an associative array of all the Field IDs and values for the current form submission. So if your Total field has an ID of 20 you can access the total amount for the current submission like this: $entry['20']. The IDs of the fields don't change so $entry['20'] will always return the total. Further details here:
https://www.gravityhelp.com/documentation/article/entry-object/
If you have lots of different forms and the Field IDs are different for each form then you can use this:
$fields = GAPI::get_fields_by_type( $form, $type );
So if you know that that you only have one number field you can get the ID like this:
$number_fields = GAPI::get_fields_by_type( $form, 'number' );
$my_number_field = $number_fields[0];
$field_id = $my_number_field->id;

Related

Forminator API - Get total value of submission data

I'm attempting at pulling data from my Forminator submissions, but with no joy.Here's what I've got so far;
<?php
$form_id = 43;
$elem_id = 'currency-1';
$fields = Forminator_API::get_form_field( $form_id, $elem_id, $to_array );
var_dump(get_object_vars( $fields ));
currency-1, accepts a value from your input 100, 200 etc. What i'm trying to do is, add all those value together and show frontend.
Any advice is greatly appreachiate, i've spent around 4 hours looking at the api and not worked it out.
It looks like you are looking at the wrong object, in order to pull those number you need to look at the submissions data which is
The below should give you a list of all the entries, you can loop through those and add the numbers together.
<?php
$form_id = 43;
$entries = Forminator_API::get_entries( $form_id );
foreach($entries as $key => $value){
//add all values of currency-1 together
}
?>

How to delete item ID from array used as session shopping cart

I have a SESSION['cart'] with ID numbers only. I have a form passing the ID with a remove button. Once passed to my controller, I cannot figure out how to write the code that uses the ID ($_POST['id']) to delete the item from the SESSION['cart'].
I can loop through and display the array contents, but I cannot figure out how to delete based on ID passed from the form.
How do I loop through the SESSION['cart'] array to find a match with the ID passed from my delete form, and then delete that ID? I know that unset($_SESSION['cart'][X] deletes the ID at index X, but I cannot figure out how to loop through all the elements to find a match.
I have read a number of related issues in this forum but have been unable to apply any of those solutions to resolve this challenge. Any assistance is appreciated.
The way you have your values ($products = array(3,7,99,152)) isn't a very good method. Every time you want to perform an action, you have to loop through the array, you don't want that. Apart from that, how do you store quantity? Or variations like e.g. size or color?
if your structure is $array[ ID_OF_PRODUCT ], you can simply do this:
unset( $_SESSION['cart'][$_POST['id']] ); // Instant access via the key!
This should be the method to use. This allows you to create an array like this, with advanced info, but with easy access (42/63 are example id's)
$_SESSION['cart']['products'][42] = array(
'quantity' = 11,
'size' = 'large',
'color' = 'blue'
);
$_SESSION['cart']['products'][63] = array(
'quantity' = 9,
'size' = 'small',
'color' = 'red'
);
This way you can access a lot of info with the product ID, and now also see which size and color (both just examples) the user selected. You may not have need for this now, but you will further down the road :)
As you might see, you can easily do stuff with the item:
isset($_SESSION['cart'][$_POST['id']]); // check if the product exists
unset($_SESSION['cart'][$_POST['id']]); // remove the product
echo $_SESSION['cart'][$_POST['id']]['quantity']; // get the quantity.
Not a loop in the code. You should only use loops when you have to, try to somewhat avoid them because often their slow. Say you have an extreme case of 1000 items in your shop, and you need to delete no999... That'll take a noticable moment.
Here is the code to do it right:
$id = $_POST['id'];
$items = $_SESSION["cart"];
if(($key = array_search($id, $items)) !== false) {
unset($items[$key]);
}
$_SESSION["cart"] = array_values($items);
Advice
Beside item ID, you can also sve item count in SESSION array because user can add several times same item into cart. In that case your $_SESSION["card"] should be structured like:
array(
'1'=>12,//Item with ID=1 is added 12 times in shopping cart
'17'=>2,//Item with ID=17 is added 2 times in shopping cart etc.
'32'=>12,
)

Only outputting specific fiels from Podio Item Filter

We need to extract specific fields, no matter if they have data or not.
If I f.ex. want an putput with only the field with external id : 'logo' i am trying this:
$limit = 10;
$app_id = xxxxxxx;
$items = PodioItem::filter($app_id,array('Limit' => $limit,'external_id' => 'logo'));
Within podio not all the fields are filled in, and the result is that we do not get a return value for field 'logo'.
When we don't get a return from field logo - the array output is not structured in a way so we can grab the data we need.
How do we achieve this ?
Added text
Rough Example on print output:
items[0] = Companyname,Logo,Address,Phone,Country
(has data in Logo)
items[1] = Companyname,Address,Phone,Country,ContactPerson
(no data in Logo)
items[2] = Companyname,Address,Phone,Country
PROBLEMS ARISING
items[2][4] is not existing (but i won't know this)
items[0][2] is Address Field (but i won't know this)
items[1][2] is Phone Field (but i won't know this)
Looking for Company Contact Person [x][4] accross items will end in
[0] = Wrong (country)
[1] = Right data (ContactPerson)
[2] = PHP error
Like in SQL, i would take the coloumn_name_1,coloumn_name_2, etc.. and grab data from only these fields.
PodioItem objects have a collection of item fields. As you've noted only fields that have values are included (since including empty fields is redundant). As you've noted you can't reliably access the fields by their array offset.
What you should do it access it by field_id or external_id. These are the unique identifiers. You can use the field method on the PodioItem object for this purpose:
$items = PodioItem::filter(...);
foreach ($items['items'] as $item) {
// Get field with external_id "logo". You can also pass in field_id
$field = $item->field('logo');
if ($field) {
print "Found a logo field!";
}
}

Return results when search parameters is null i Zend Lucene

I have multiple fields in search form.Every field could be empty.
I build query like this:
$search_title = trim($_POST["search_title"]);
$search_skill = trim($_POST["search_skill"]);
$search_company = trim($_POST["search_city"]);
$search_country_id = trim($_POST["search_county_id"]);
$hits = $index->find("title:$search_title and skill:$search_skill and city:$search_city and country_id:$country_id");
User can only fill title or skill or city etc. but if some field is empty i have no result.
I have result only if all fields filled and matched.
I wont results if only one field is filled,if is null ignore that field:
$hits = $index->find("title: and skill: and city: and country_id:$country_id");
you might try something like this:
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost)) {
//get filtered and valid values from search form.
//filter the array for set values
$data = array_filter($form->getValues());
//extract key => vaules as $variable = values
extract($data);
$query1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
//test for variable isset and add term to query
if (isset($search_title)){
$query1->addTerm(new Zend_Search_Lucene_Index_Term($search_title, 'title'));
}
if (isset($search_skill)){
$query1->addTerm(new Zend_Search_Lucene_Index_Term($search_skill, 'skill'));
}
if (isset($search_city)){
$query1->addTerm(new Zend_Search_Lucene_Index_Term($search_city, 'city'));
}
if (isset($search_country_id)){
$query1->addTerm(new Zend_Search_Lucene_Index_Term($search_country_id, 'country_id'));
}
//This should give the AND you are looking for...I hope
$query= new Zend_Search_Lucene_Search_Query_Boolean(array($query1), array(TRUE));
//get result set from query
$hits = $index->find($query);
}
}
if you use the StringTrim filter in your forms you won't need to use the trim() function on your data. The $_POST array is dangerous with user supplied data, ZF provides the getValues() series of methods to provide data from the Request Object (POST and GET) the have had filters and validators you specify applied.
I used the extract() function in this instance because I used getValues() so the data has been filtered and validated. There are of course other valid ways to assign key => value pairs to variables. Use your favorite.

how to populate a value in some element using zend_form

i have some fields in my database table,and a field with phone name , i save value to this filed like this 111-222-5555
now i want to read all of my fields , and populate to my form , i like populate phone filed to 3 elements(as text element)
when i try this code
$id = $this->_request->getParam ( 'id' );
$values = $cutomModel->findCustomerById($id);// return array of row
$frm->populate($values);
all fields show in form except phone field ,
how can i populate phone field to 3 elements
thanks
If I understand your question correctly, firstly you need to create 3 separate form fields (something along the lines of phone_1, phone_2 and phone_3) to hold the 3 separate phone segments. Then before populating the fields, separate the database output of 1 field into 3 fields.
$id = $this->_request->getParam ( 'id' );
$values = $cutomModel->findCustomerById($id);// return array of row
// split phone number into 3 values
list($values['phone_1'], $values['phone_2'], $values['phone_3']) = explode('-', $values['phone'], 3);
$frm->populate($values);
Finally, before inserting form input into your database you must combine the input from the 3 fields into 1 field.
$values = $frm->getValues();
// combine phone number into 1 value
$values['phone'] = implode('-', $values['phone_1'], $values['phone_2'], $values['phone_3'])
$cutomModel->insert($values);
From what I (hardly) understood, you are trying to fill 3 elements of a form with the content of one field (phone) in your database table.
To achieve this you can use the setDefault() method of the Zend_Form object. Something like this:
$frm->setDefault('field_name', $values->phone);
Do this after populate() and for every field that you want a customized value to be filled.

Categories