Jump to content

Need Help with PHP Function - newbie here


anand0412
Go to solution Solved by mac_gyver,

Recommended Posts

Hi all. I have no formal education in programming and learning everything through internet. Forgive me if this is too basic.

I am creating a automated document from gravity forms in wordpress. This is the example of code that works.

if ( is_array( $form_data['field'][77] ) AND  count( $form_data['field'][77] ) == 1 ) {
	$v_bop1 = ' to ' . (implode(', ', $form_data['field'][77])) . '.';	
} elseif ( is_array( $form_data['field'][77] ) AND  count( $form_data['field'][77] ) > 1 ) { 
    $v_bop1 = (implode(', ', $form_data['field'][77]));	
	$v_bop1 = 'to ' . substr_replace($v_bop1, ' and', strrpos($v_bop1, ','), 1) . ' equally.';
}

As you may have guessed, I am trying to get some data from gravity form and reformat it to suit my document. However, in order to work, I need to duplicate this code for each and every entry which seems inefficient. 

I tried to create a function using this code:-

function GetBeneficiaries ( $benfield ) {
	if ( is_array( $form_data['field'][$benfield] ) AND  count( $form_data['field'][benfield] ) == 1 ) {
	$beneficiaries = ' to ' . (implode(', ', $form_data['field'][benfield])) . '.';	
    } elseif ( is_array( $form_data['field'][benfield] ) AND  count( $form_data['field'][benfield] ) > 1 ) { 
    $beneficiaries = (implode(', ', $form_data['field'][benfield]));	
	$beneficiaries = 'to ' . substr_replace($beneficiaries, ' and', strrpos($beneficiaries, ','), 1) . ' equally.';
	echo $beneficiaries;
    }
}

I should then be able to call the function with 

GetBeneficiaries ( 77 );

However, my function is not returning any data / value. What am I doing wrong?

Link to comment
Share on other sites

13 minutes ago, dodgeitorelse3 said:

First thing I see is your benfield throughout your function needs the $ in front of it.

Thank you for pointing it out. 

 

6 minutes ago, mac_gyver said:

you would need to return $beneficiaries;, rather than to echo it, and then use the returned value in the calling code.

Thanks for the suggestion.

I have implemented both your suggestions. For some reason I am still not getting any result.

my new code is now 

function GetBeneficiaries ( $benfield ) {
	if ( is_array( $form_data['field'][$benfield] ) AND  count( $form_data['field'][$benfield] ) == 1 ) {
	$beneficiaries = ' to ' . (implode(', ', $form_data['field'][$benfield])) . '.';	
	return $beneficiaries;
    } elseif ( is_array( $form_data['field'][$benfield] ) AND  count( $form_data['field'][$benfield] ) > 1 ) { 
    $beneficiaries = (implode(', ', $form_data['field'][$benfield]));	
	$beneficiaries = 'to ' . substr_replace($beneficiaries, ' and', strrpos($beneficiaries, ','), 1) . ' equally.';
	return $beneficiaries;
    }
}

echo GetBeneficiaries ( 77 );

I have been at this for 2 days now. I appreciate your help

Link to comment
Share on other sites

  • Solution

$form_data doesn't exist inside the function (there would be php errors.) 

you could actually just define the function to accept an array of data as the input and call the function with $form_data['field'][77] as the input parameter. this will allow the processing to be applied to any array of data, not hard-coded for $form_data['field']...

  • Like 1
Link to comment
Share on other sites

13 minutes ago, mac_gyver said:

$form_data doesn't exist inside the function (there would be php errors.) 

you could actually just define the function to accept an array of data as the input and call the function with $form_data['field'][77] as the input parameter. this will allow the processing to be applied to any array of data, not hard-coded for $form_data['field']...

Awesome. That worked. Thank you very very much. I can finally sleep peacefully tonight.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.