gravity form $_POST issue - php

I've been wrapping my head around this issue for at least a couple days now, searched online and even broke the code down to make sure I'm not missing anything.
The Goal: I'm simply grabbing the value from the Birthdate field, calculate it, then save it to the hidden field which is called 'Age'.
The Issue: I am able to grab the value and calculate just fine. However I am unable to store/save it to that specific field, which as you will see in the code is field id= 47. Whenever I run the app, fill out the form and checked the Entries, the #47 pops up (it is not related to the id#. I ran some test like saving it into another field and it was the same). I am able to 'echo' out the var $age and it gives me exactly what I want. However when I try to save it, it gives me the aforementioned '47'.
On the line that contains '$_POST['input_47'] = $age', that is where my issue is. I know I can pass integer and strings to the $_POST, but it will not accept the $age.
Please let me know if I haven't made my request clear. Thank you very much to anyone who can provide a hint or two to get this resolved!
Here's the code that I placed on functions.php *edited
add_filter( 'gform_pre_submission_filter_1', 'pre_submission', 10, 4 );
function pre_submission ($form) {
// Calculate the age based on incoming variable $fsmConvert
$ageTime = strtotime($_POST['input_26']);
$t = time(); // current timestamp
$ageConvert = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $ageConvert / $year; // if you want exact estimate, STOP HERE
$age = floor($ageYears); //this is calculated age
foreach($form['fields'] as $field2) {
if(strpos($field2['id'], 47) === false) {
continue;
$field2['defaultValue'] = $age;
}
}
// return $form;
return $form;
}

Instead of adding your $age variable in $_POST directly, you can try to add it in $value variable.
Note : By default $form contains whole Gravity form inputs and $value contains all the inputs that user passed.
Let me know if I can help you further....

Related

Woocommerce Order / AFC get_sub_field / get_sub_field_object (array)

Have setup ACF repeater field that stores various amount of tracking numbers in the order. Having 0 success with retrieving this information so need some advice.
Am using this to put information in subfields and it does the job
foreach ($base->DocumentLines->DocumentLine as $item) {
foreach ($item->MiscData as $misc) {
foreach ($misc->PackageNo as $package) {
$trackno = (string)$package->TrackingNo;
update_post_meta("$order_id", $field_rep, $count);
$sub = $count +1;
update_sub_field(array($field_key_rep, $sub, $field_key_sub), $trackno, "$order_id");
$count = $count + 1;
update_field($field_key, $trackno, "$order_id");
}
}
}
This works well, but then i need to retrieve this numbers and write em out. They are getting included in an email so need to retrieve the data outside of order.
Before rebuilding the function to be able to handle multiple numbers i did use a single field and could retrieve the information with
get_post_meta($order_id, 'tracking', true);
Feels like i have been trying everything now but got absolutely nothing.
Image from one of the orders, in this one it’s 10 tracking numbers but it varies from 1 to 20 if it's to any help.
The feeling when you realize after 10 hours that your missed a capital letter in sub field name.
Just wanted to submit my solution and hopefully it can help someone else having issues with ACF Repeater fields + Woocommerce
For my specific case i did make a function that could extract all the tracking numbers my function above did add from XML files.
$function trackingNo($postID) {
$field_rep = 'trackingNo';
$field_sub = 'no';
if (have_rows($field_rep, $postID)) {
$trackingNo = array();
// loop through the rows of data
while (have_rows($field_rep, $postID)):
the_row();
// Add to array
$trackingNo[] = get_sub_field($field_sub);
endwhile;
$foo = implode('&consignmentId=', $trackingNo);
$bar = 'urlzz/tracktrace/TrackConsignments_do.jsp?&consignmentId=';
$value = $bar . $foo;
return $value;
}
}
Any advice for improvement is always welcome, my PHP is so'n'so :)

Variable changing when used in a forumula in php

I hope that someone can help me figure this out because it is driving me crazy. First off some background and values of the variables below.
The $TritPrice variable fluctuates as it comes from another source but for an example, lets say that the value of it is 5.25
$RefineTrit is constant at 1000 and $Minerals[$oretype][0] is 333
When I first goto the page where this code is, and this function runs for some reason the $TritPrice var either get truncated to 5.00 or gets rounded down but only during the formula itself. I can echo each of variables and they are correct but when I echo the formula and do the math manually the $TritPrice is just 5 instead of 5.25.
If I put in $TritPrice = 5.25; before the if statement it works fine and after the form is submitted and this function is rerun it works fine.
The page that uses this function is at here if yall want to see what it does.
If ($Minerals[$oretype][1] <> 0) {
$RefineTrit = getmintotal($oretype,1);
if ($RefineTrit < $Minerals[$oretype][1]) {
$NonPerfectTrit = $Minerals[$oretype][1] +
($Minerals[$oretype][1] - $RefineTrit);
$Price = (($TritPrice * $NonPerfectTrit) / $Minerals[$oretype][0]);
} else {
$Price = $TritPrice * $RefineTrit / $Minerals[$oretype][0];
}
}
This is where the $TritPrice
// Get Mineral Prices
GetCurrentMineralPrice();
$TritPrice = $ItemPrice[1];
$PyerPrice = $ItemPrice[2];
$MexPrice = $ItemPrice[3];
$IsoPrice = $ItemPrice[4];
$NocxPrice = $ItemPrice[5];
$ZydPrice = $ItemPrice[6];
$MegaPrice = $ItemPrice[7];
$MorPrice = $ItemPrice[8];
and the GetCurrentMineralPrice() function is
function GetCurrentMineralPrice() {
global $ItemPrice;
$xml = simplexml_load_file("http://api.eve-central.com/api/marketstat?typeid=34&typeid=35&typeid=36&typeid=37&typeid=38&typeid=39&typeid=40&typeid=11399&usesystem=30000142");
$i = 1;
foreach ($xml->marketstat->type as $child) {
$ItemPrice[$i] = $child->buy->max;
$i++;
}
return $ItemPrice;
}
The problem is not in this piece of code. In some other part of the program, and I suspect it is the place where the values from the textboxes are accepted and fed into the formula - in that place there should be a function or code snippet that is rounding the value of $TritPrice. Check the place where the $_POST values are being fetched and also check if any javascript code is doing a parseInt behind the scenes.
EVE-Online ftw.
with that out of the way, it's possible that your precision value in your config is set too low? Not sure why it would be unless you changed it manually. Or you have a function that is running somewhere that is truncating your variable when you call it from that function/var
However, can you please paste the rest of the code where you instantiate $TritPrice please?

PHP array comparison with different lengths

I need to compare values from 2 arrays that are not always the same size, each containing either a login or a logout time. I need to get the time difference total or time logged in between these values. Where I am getting caught is the logout result is usually smaller then the login. I tried the following but it isn't accurate at all.
$offset = 0;
for($i=0;$i<sizeof($login_ary);$i++){
//unset($time_logged);
$time_logged = Array();
while( (int)$login_ary[$i] > (int)$logout_ary[$i] ) {
$offset++;
}
$time_logged['login'] = $login_ary[$i] + $offset;
$time_logged['logout'] = $logout_ary[$i];
$calc_ary[] = $time_logged;
}
$time_logged_in = "undefined";
End result I need to get either an array with lined up login/logoffs or a calculation of the total time difference between the login time and then logout time. But I have to exclude logins that don't have an associated logout.
EDITS
1) Times inside of the arrays are unix timestamps
2) the arrays aren't static as this is done inside a loop for each user in a result set so the var_dump would look something like the following
login_arr
Array([0] => '1385402632',[1] => '1385763384',[2] => '1387293992')
logout_arr
Array([0] => '1387294012')
** MY SOLUTION **
My solution is out of context of the original question which is why I posted it as an edit instead of an answer. But here is what I did, the simplicity of it makes me feel foolish that I didn't think of it originally. What I did, is write the session timeout into the logout_ary since the data was going to be unrepresented otherwise, we decided that fudging a time was better then completely avoiding it. So I took the login time and fudged a logout time and spliced it into the array at the same point. I still would like to know the true answer to this problem if there is one though.
for($i=0;$i<sizeof($login_ary);$i++){
//unset($time_logged);
$time_logged = Array();
if( (int)$login_ary[$i] > (int)$logout_ary[$i] ) {
array_splice($logout_ary,$i,0,$login_ary[$i]+$session_length);
$time_logged['login'] = $login_ary[$i];
$time_logged['logout'] = $logout_ary[$i];
} else {
$time_logged['login'] = $login_ary[$i];
$time_logged['logout'] = $logout_ary[$i];
}
$calc_ary[] = $time_logged;
}
Just read the comments, I think this might be what you're after comment and I can see if I can help more.
<?php
function calcTime($login_ary, $loutout_ary) {
$login_ary_length = sizeof($login_ary);
$logout_ary_length = sizeof($logout_ary);
if($login_ary_length != $logout_ary_length) {
$diff = $login_ary_length - $logout_ary_length;
//just find the code that suits your format and put it in the function strtotime()
$timestamp = strtotime('TODAYS DATE AND TIME');
//for the amount of diff we add todays date and time to the array so that they are eqal
for(i=0; i<=$diff; i++) {
array_push($logout_ary, $timestamp);
}
//I'm going to leave this for you to do.
return $total_hours;
}
}
?>
I hope this is what you're looking for.
What exactly is stored in the two arrays? Just time variables? How are you to know if say that login_ary[i] is the login time for the log_out[i] time? Also, why not just take the log_out time - the log_in time to get the time differential?

Creating a different array related to different times a loop happens php

there. I'm having a problem with creating arrays in certain conditions in php, i'll try to explain. Here's my code:
for ($i = 1; $i < $tamanho_array_afundamento; $i++) {
if ($array_afundamento[$i] - $array_afundamento[$i - 1] > 1) {
$a = $array_afundamento[$i - 1];
$con->query('CREATE TABLE IF NOT EXISTS afunda_$a
SELECT (L1_forma_tensao_max + L1_forma_tensao_min)/2 as L1_forma_tensao, (L2_forma_tensao_max + L2_forma_tensao_min)/2 as L2_forma_tensao, (L3_forma_tensao_max + L3_forma_tensao_min)/2 as L3_forma_tensao
FROM afundamento
WHERE id > $prevNum AND id < $a');
$tabelas_intervalos_afunda1 = ($con->query("SELECT * FROM afunda_$a");
while ($row = $tabelas_intervalos_afunda->fetch(PDO::FETCH_ASSOC)) {
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
$prevNum = $a;
}
}
So as u can see, i have an if statement in a for loop, what i'm wishing to do is to create
one set of:
{
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
every time the if statement is runned. I was trying replacing this in the original code:
{
$array_forma_onda_fase1_afund_$a[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_$a[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_$a[] = $row['L3_forma_tensao'];
}
so as $a is changed everytime the if statement is accessed, i could have a different set of these arrays for everytime the if statement is accessed, but php doesn't accept this and i wouldn't have a very good result, though if i can reach it i would be pleased.
But my goal is to get:
{
$array_forma_onda_fase1_afund_1[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_1[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_1[] = $row['L3_forma_tensao'];
}
{
$array_forma_onda_fase1_afund_2[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_2[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_2[] = $row['L3_forma_tensao'];
}
...
where the last number represents the array retrieved for the n-th time the if statement runned. Does someone have a tip for it?
Thanks in advance! Would appreciate any help.
EDIT
As asked, my real world terms is as follows:
I have a table from which i need to take all the data that is inside a given interval. BUT, there's a problem, my data is a sine function whose amplitude may change indefinite times (the data bank is entered by the user) and, when the amplitude goes inside that interval, i need to make some operations like getting the least value achieved while the data was inside that interval and some other parameters, for each interval separately, (That's why i created all those tables.) and count how many times it happpened.
So, in order to make one of the operations, i need an array with the data for each time the databank entered by the user goes in that interval (given by the limits of the create query.).
If i were not clear, just tell me please!
EDIT 2
Here's the image of part of the table i'm working with:
http://postimg.org/image/5vegnk043/
so, when the sine gets inside the interval i need, it can be seen by the L1_RMS column, who accuses it, so it's when i need to get the interval data until it gets outside the interval. But it may happens as many times as this table entered by the user brings it on and we need to bear in mind that i need all the intervals separately to deal with the data of each one.
Physics uh?
You can do what you wanted with the arrays, it's not pretty, but it's possible.
You can dynamically name your arrays with the _$a in the end, Variables variables, such as:
${"array_forma_onda_fase3_afund_" . $a}[] = "fisica é medo";

Add/Replace URL GET parameter depending on current URL

I’ve tried for some time now to solve what probably is a small issue but I just can’t seem get my head around it. I’ve tried some different approaches, some found at SO but none has worked yet.
The problem consists of this:
I’ve a show-room page where I show some cloth. On each single item of cloth there is four “views”
Male
Female
Front
Back
Now, the users can filter this by either viewing the male or female model but they can also filter by viewing front or back of both gender.
I’ve created my script so it detects the URL query and display the correct data but my problem is to “build” the URL correctly.
When firstly enter the page, the four links is like this:
example.com?gender=male
example.com?gender=female
example.com?site=front
example.com?site=back
This work because it’s the “default” view (the default view is set to gender=male && site=front) in the model.
But if I choose to view ?gender=female the users should be able to filter it once more by adding &site=back so the complete URL would be: example.com?gender=female&site=back
And if I then press the link to see gender=male it should still keep the URL parameter &site=back.
What I’ve achived so far is to append the parameters to the existing URL but this result in URL strings like: example.com?gender=male&site=front&gender=female and so on…
I’ve tried but to use the parse_url function, the http_build_query($parms) method and to make my “own” function that checks for existing parameters but it does not work.
My latest try was this:
_setURL(‘http://example.com?gender=male’, ‘site’, ‘back’);
function _setURL($url, $key, $value) {
$separator = (parse_url($url, PHP_URL_QUERY) == NULL) ? '?' : '&';
$query = $key."=".$value;
$url .= $separator . $query;
var_dump($url); exit;
}
This function works unless the $_GET parameter already exists and thus should be replaced and not added.
I’m not sure if there is some “best practice” to solve this and as I said I’ve looked at a lot of answers on SO but none which was spot on my issue.
I hope I’ve explained myself otherwise please let me know and I’ll elaborate.
Any help or advice would be appreciated
You can generate the links dynamically using the following method:
$frontLink = (isset($_GET['gender'])) ? 'mydomain.com?gender='.$_GET['gender'].'&site=front':'mydomain.com?site=front';
$backLink = (isset($_GET['gender'])) ? 'mydomain.com?gender='.$_GET['gender'].'&site=back':'mydomain.com?site=back';
This is a 1 line if statement which will set the value of the variables $frontLink and $backlink respectively. The syntax for a 1 line if statement is $var = (if_statement) ? true_result:false_result; this will set the value of $var to the true_result or false_result depending on the return value of the if statement.
You can then do the same for the genders:
$maleLink = (isset($_GET['site'])) ? 'mydomain.com?gender=male&site='.$_GET['site']:'mydomain.com?gender=male';
$femaleLink = (isset($_GET['site'])) ? 'mydomain.com?gender=female&site='.$_GET['site']:'mydomain.com?gender=female';
Found this by searching for a better solution then mine and found this ugly one (That we see a lot on the web), so here is my solution :
function add_get_parameter($arg, $value)
{
$_GET[$arg] = $value;
return "?" . http_build_query($_GET);
}
<?php
function requestUriAddGetParams(array $params)
{
$parseRes=parse_url($_REQUEST['REQUEST_URI']);
$params=array_merge($_GET, $params);
return $parseRes['path'].'?'.http_build_query($params);
}
?>
if(isset($_GET['diagid']) && $_GET['diagid']!='') {
$repParam = "&diagid=".$_GET['diagid'];
$params = str_replace($repParam, "", $_SERVER['REQUEST_URI']);
$url = "http://".$_SERVER['HTTP_HOST'].$params."&diagid=".$ID;
}
else $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&diagid=".$ID;

Categories