Forminator API - Get total value of submission data - php

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
}
?>

Related

ACF make top 10 based on field value

I'm using ACF for a while now and I thought this would be easier but cant figure out how to do this properly...
I'm trying to create some kind of trophy cabinet. So every company has a score that is stored inside a ACF called "company_score".
For example we have companies called Microsoft, Facebook and Twitter. They all have a score:
Facebook = 200000
Microsoft = 900000
Twitter = 100000
So the top 3 wil be like
1) Microsoft
2) Facebook
3) Twitter
I know how to display the value of an ACF but how can I compare all the scores that are stored and when a company has the best score it will display a gold medal. When a company has the second best score it will display a silver medal and so on.
I all had it figured it out in my head but a bit stuck here how to do this.
There could be several approaches to handle this. The first one, which comes in my mind is very simple.
Try these steps:
Pull all fields
Store in an array
Sort the array in ascending order
Display the results.
I hope, you've some understanding of the code but here are the coded guidelines...
<?php
$fields = get_fields();
$company_details = array();
if( $fields ):
foreach( $fields as $name => $value ):
$company_details[ $name ] = $value;
endforeach;
endif;
//to sort by company score
arsort( $company_details );
//loop through the array and display results, like
foreach( $company_details as $name => $value):
echo "Company name is: " .$name. ". Company Score: " . $value;
endforeach;
PS: The code is not tested because the purpose was to share logic with you. So, ignore if there's any error.

Gravity forms - Get entry data without entry numbers

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;

PHP - Pulling array and splitting the strings

Not sure how to title this properly but here's the issue I am running into currently. I built a cart and checkout system and it loads all the data into a database when it finalized the order. To save some space, I stored just the item IDs originally but then I ran into the issue of if I deleted the item from the database (because it was discontinued or whatever) then it wouldn't return the info I needed. And if they ordered more then 1 item the database record would be wrong. So I stored the data like so:
Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2
OR
1:3:20.00:Flower Hat, 2:1:17.75:diamonds
The issue I have right now that I need help with is this. I need to seperate the four values into variables like $price, $item, $id, $ammount so I can display them on the order history page and I need to loop through all items on the array so I can print a row for each item with all four fields respective to that item.
I use strpos already to get the shipping info from the same database field which is formatted as METHOD:Price but since I have 3 :'s on my string I'm not sure how to go through each one. Thanks.
Here's a function
function parseItems($dbjunk){
$cart = array();
$items = explode(",",$dbjunk);
foreach($items as $i){
$chunks = explode(":", $i);
$cart[] = array(
"ItemID" => $chunks[0] ,
"Quantity" => $chunks[1] ,
"Price" => $chunks[2] ,
"name" => $chunks[3]
);
}
return $cart;
}
Example usage:
$dbjunk = "Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2";
$parsed = parseItems($dbjunk);
print_r($parsed);
See: https://3v4l.org/rBkXF
If you need variables instead of an array you can use list(), like this..
$dbjunk = "Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2";
$parsed = parseItems($dbjunk);
foreach($parsed as $p){
list($itemID, $Quantity, $Price, $name) = array_values($p);
var_dump($itemID, $Quantity, $Price, $name);
}
see: https://3v4l.org/l4vsn
You should not physically delete items from your database. Instead, put a new column named 'is_active' or something like that to indicate whether the product is active/non-deleted.
Answering your question, here is my suggestion:
$orderString = '1:3:20.00:Flower Hat, 2:1:17.75:diamonds';
$items = array();
foreach(explode(', ', $orderString) as $itemString) {
$itemData = explode(':', $itemString);
$items[] = array(
'id' => $itemData[0],
'amount' => $itemData[1],
'value' => $itemData[2],
'description' => $itemData[3]
);
}
with this code, you will obtain an array with the data of all the items in the string, no matter how much items are in the string
try something like
$data = 1:3:20.00:Flower Hat, 2:1:17.75:diamonds
list($price, $item, $uid, $id, $ammount) = explode(":", $data);
echo $user;
echo $item;
Read about First Normal Form. Basically, you want to store one value in one field. So, instead of this:
shipping = "method:price"
You want something like this:
shipping_method = "method"
shipping_price = "price"
Don't concern yourself with space -- it's essentially free nowadays.
Regarding your deleted items dilemma, your initial implementation was the way to go:
I stored just the item IDs originally
In addition to reverting to this technique, I would recommend two things:
Add a boolean field to your item table to represent if the item is currently available or not. This gives you the additional feature of being able to toggle items on/off without having to delete/insert records and change ids.
Before deleting an item, check to see if it's ever been ordered. If not, it's ok to delete. If so, instead just deactivate it.

Strange variable overwrite in foreach loop

This must be something ugly obvious, but I'm stucked on this and can't solve it for past two hours.
I have this piece of code:
foreach($idMap as $menuId=>$pageId)
{
echo('$this->update("menus_items", SET "link = /content/show?id='.$pageId.'" WHERE id = '.$menuId.');'."\n");
$this->update
(
'menus_items',
array('link'=>'/content/show?id='.$pageId),
array('id = '.$menuId)
);
}
echo part works as expected ($pageId is different for each item, taken from $idMap), while Yii's CDbCommand::update() gets wako and have $pageId equal to it's last value for all loop iterations.
In other words, if I have 20 menu items and last item in result set has pageId = 18, then when using CDbCommand::update(), I'm getting all menu items set to that last value.
There must be some variable overwriting here, but I can't find it for past two hours, especially, that echo put just one line above works great. Can someone help here.
Guessing, but does $this->update() expect a single array for its bind arguments?
$this->update
(
'menus_items',
array(
'link' => '/content/show?id='.$pageId,
'id' => $menuId
)
);

multi dimension session array

I am using a form that is pulling data from two MySQL databases into a single dynamic page. When a user clicks add to cart I want to store that data in a multi dimensional session array to call up later when they click view cart. I am wondering how to auto increment the subset identifier(array key?) of the item when a new item is added from the add to cart form. This is what I have so far:
$newitem = array ($row_getimages['icon'],$row_getimages['title'],$row_getshoppingcart['medium'],$row_getshoppingcart['size'],$row_getshoppingcart['price'],$row_getshoppingcart['shipping']);
session_start();
if(isset($_SESSION['item'][1]))
$_SESSION['item'][1] = $_SESSION['item'][1]+ 1;
else
$_SESSION['item'][1] = 1;
Also any help for calling out the data later would be appreciated. As a user may have 1 or 20 items stored in the session I am not sure how to make sure all items would be echoed no matter how many they have added.
This is my first time at a multi dimensional array and sessions. Obviously because the image page is dynamic and purchase price is based on several factors, just using a MySQL database of available items as I have in the past is out of the question.
Thank you in advance for your time.
$newitem = array ('id' => $row_getshoppingcart['id'] , 'icon' => $row_getimages['icon'],'title' => $row_getimages['title'],'medium' => $row_getshoppingcart['medium'],'size' => $row_getshoppingcart['size'],'price' => $row_getshoppingcart['price'],'shipping' => $row_getshoppingcart['shipping']);
session_start();
$_SESSION['item'][] = $newitem;
That is all you have to do, if I understand your system correctly.
UPDATE
I updated the $newitem array to include array keys. You can reference the new item info with arrays like this:
$_SESSION['item'][(num)]['id']
Or you can loop through the results like this:
foreach ( $_SESSION['item'] AS $item )
{
echo 'id: ' . $item['id'] . '<br />';
echo 'title: ' . $item['title'];
// and so on
}
If it is not important for the numbers to be sequential, you can use:
$_SESSION['item'][] = array('a','b','c','d','e','f');
Using [] will simply add a new element to the end of the array.
However, I would probably use a product ID for the key.

Categories