kproctor Posted March 19, 2017 Share Posted March 19, 2017 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); Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/ Share on other sites More sharing options...
benanamen Posted March 19, 2017 Share Posted March 19, 2017 For starters, your logic is off. Where does the data for $lender_list come from? A database? Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544386 Share on other sites More sharing options...
kproctor Posted March 19, 2017 Author Share Posted March 19, 2017 It is coming from a function that scrapes another website. If my logic is off, why is it working? Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544389 Share on other sites More sharing options...
requinix Posted March 19, 2017 Share Posted March 19, 2017 If you're having problems putting that code into functions then we need to see the version where you put that code into functions. Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544390 Share on other sites More sharing options...
Jacques1 Posted March 19, 2017 Share Posted March 19, 2017 “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. Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544391 Share on other sites More sharing options...
kproctor Posted March 21, 2017 Author Share Posted March 21, 2017 (edited) 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 March 21, 2017 by kproctor Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544428 Share on other sites More sharing options...
ginerjm Posted March 21, 2017 Share Posted March 21, 2017 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". Quote Link to comment https://forums.phpfreaks.com/topic/303499-function-help/#findComment-1544448 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.