Jump to content

function help


kproctor

Recommended Posts

Hi,

 

Below is two blocks of code that I have been trying to convert to a function. Both sections are very similar. The below code does work. 


// get current bank details
$fi_key_exisiting = $_SESSION['existing_FI'];

if (empty($fi_key_existing)) {
    $fi_key_existing = 0;
} else {
    $fi_key_existing = $fi_key_existing;
}
 
$selected_lender = ($lender_list[$fi_key_exisiting]);

foreach($selected_lender as $item) {
    
	$fi_name = $item[0];
	$variable = $item[1];
	$six_months = $item[2];
	$one_year = $item[3];
	$two_year = $item[4];
	$three_year = $item[5];
	$four_years = $item[6];
	$five_years = $item[7];


}

$comparisonRate = array('variable' =>$variable, '6' =>$six_months, '12' =>$one_year, '24'=>$two_year, '36'=>$three_year, '48'=>$four_years, '60'=>$five_years);

?>

<?php

// get new bank details
$fi_key_new = $_SESSION['new_FI'];

if (empty($fi_key_new)) {
    $fi_key_new = 0;
} else {
    $fi_key_new = $fi_key_new;
}
 
$selected_lender = ($lender_list[$fi_key_new]);

foreach($selected_lender as $item) {
    
	$new_fi_name = $item[0];
	$new_variable = $item[1];
	$new_six_months = $item[2];
	$new_one_year = $item[3];
	$new_two_year = $item[4];
	$new_three_year = $item[5];
	$new_four_years = $item[6];
	$new_five_years = $item[7];

}

$newComparisonRate = array('variable' =>$new_variable, '6' =>$new_six_months, '12' =>$new_one_year, '24'=>$new_two_year, '36'=>$new_three_year, '48'=>$new_four_years, '60'=>$new_five_years);

Link to comment
Share on other sites

“Working” is a meaningless term. The code is clearly neither tested nor peer-reviewed, so all you're saying is that you haven't looked for errors yet. Congratulations. That doesn't mean they don't exist, though:

  • You access a session parameter which may not exist, and then you check if it exists afterwards. This is obviously nonsense and will lead to PHP notices (unless your error reporting is deactivated).
  • You assign a variable to itself, which doesn't make any sense either.
  • Your loop is entirely pointless, because it just overwrites the same variables over and over again.
  • You're still running around with your “exisiting” typo.

However, since nobody has any idea what you're even trying to do, it's impossible to tell you the best approach. All I can say is that the code you've shown in your recent threads looks very odd.

Link to comment
Share on other sites

Alright, I'll try to explain better. Below is code that I put together with the help of google and fine folks on this form.

 

This code goes to a website and gets information and stores the values in an array. I store the search values in a $_session variable. 

 

 

Problem: The form submits with the correct information the first time around, but when the user clicks the browser back button and selects a new value from a drop down and clicks submit the select options does not pass the correct select option values on the second submit. If the user clicks back again and changes nothing and submits the form again the values update. 

 

** If I hit the refresh button after the second submit the field taking information from the drop down menu will update. 

 

The user form is saved in a separate file from the below code

// Generates the select option

lenderOptions = "";
//Iterate through the array of lenders
foreach($lender_list as $value => $label)
{
    //Trim the value
   $label = trim($label['name']);
    //If label is empty skip this record
    if(empty($label)) { continue; }
    //Create new option for the current lender
    $lenderOptions .= "<option value='{$value}'>{$label}</option>\n";


//////// HTML form info
	<select name="existing_FI" class="field-style field-full align-none" required>
   		<option>Current FI</option>
    	<?php echo $lenderOptions; ?>
		</select>



$html = file_get_contents('https://www.website.com'); //get the html returned from the following url

$lender_doc = new DOMDocument();

libxml_use_internal_errors(TRUE); //disable libxml errors

if(!empty($html)){ //if any html is actually returned

    $lender_doc->loadHTML($html);
    libxml_clear_errors(); //remove errors for yucky html

    $lender_xpath = new DOMXPath($lender_doc);

    //get each tr from table
    $lender_row = $lender_xpath->query('//tr');

    if($lender_row->length > 0){
        foreach($lender_row as $row){
            //echo $row->nodeValue . "<br/>";
        }
    }
}
?>

<?php
$lender_list = array();

$lender_and_type = $lender_xpath->query('//tr');

if($lender_and_type->length > 0){  

    //loop through all the lenders
    foreach($lender_and_type as $pat){

        //get the name of the lender
        $name = $lender_xpath->query('td[@align="left"]', $pat)->item(0)->nodeValue;

        $fi_types = array(); //reset $fi_types for each lender
        $types = $lender_xpath->query('td', $pat);

        //loop through all the types and store them in the $fi_types array
        foreach($types as $type){
            $fi_types[] = $type->nodeValue; //the lender type
        }

        //store the data in the $lender_list array
        $lender_list[] = array('name' => $name, 'types' => $fi_types);

    }
}

if(empty($_SESSION['existing_FI'])){
	
	$fi_key_new = 0;
	
}else{


// get current bank details
$fi_key_exisiting = $_SESSION['existing_FI'];

if (empty($fi_key_existing)) {
    $fi_key_existing = 0;
} else {
    $fi_key_existing = $fi_key_existing;
}
 
$selected_lender = ($lender_list[$fi_key_exisiting]);

foreach($selected_lender as $item) {
    
	$fi_name = $item[0];
	$variable = $item[1];
	$six_months = $item[2];
	$one_year = $item[3];
	$two_year = $item[4];
	$three_year = $item[5];
	$four_years = $item[6];
	$five_years = $item[7];


}

$comparisonRate = array('variable' =>$variable, '6' =>$six_months, '12' =>$one_year, '24'=>$two_year, '36'=>$three_year, '48'=>$four_years, '60'=>$five_years);
}
?>

<?php

if(empty($_SESSION['new_FI'])){
	
	$fi_key_new = 0;
	
}else{

// get new bank details
$fi_key_new = $_SESSION['new_FI'];

if (empty($fi_key_new)) {
    $fi_key_new = 0;
} else {
    $fi_key_new = $fi_key_new;
}
 
$selected_lender = ($lender_list[$fi_key_new]);

foreach($selected_lender as $item) {
    
	$new_fi_name = $item[0];
	$new_variable = $item[1];
	$new_six_months = $item[2];
	$new_one_year = $item[3];
	$new_two_year = $item[4];
	$new_three_year = $item[5];
	$new_four_years = $item[6];
	$new_five_years = $item[7];

}

$newComparisonRate = array('variable' =>$new_variable, '6' =>$new_six_months, '12' =>$new_one_year, '24'=>$new_two_year, '36'=>$new_three_year, '48'=>$new_four_years, '60'=>$new_five_years);
}

?>
Edited by kproctor
Link to comment
Share on other sites

Let's go back. "Trying to convert to a function". One could simply put a header on a block of code, wrap the code in braces and voila! You did it. But - a function is more than that. It is a simple set of code that does a certain "function" for you. You have something that works great and you need to use it in multiple places so you 'package' it and use it as an include module and simply call that function 'name' when you want that certain action to happen.

 

Obviously you have to ensure that the contents of that function can stand on their own and not rely on multiple 'outside' processes, vars, values, etc. If it does, perhaps the function is not that isolated, meaning it does too much perhaps. When you get the code down to its bare minimum you will have code that relies on just a few (2-3?) variables that you then place into the function header and supply to the function when you call it.

 

So hopefully what I've just described is the same thing you are trying to accomplish. The question now is: what about this is being so difficult? Show us something that you have created and tell us what it "isn't doing".

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.