lindm Posted November 24, 2007 Author Share Posted November 24, 2007 The function works. Modified it shortly so the $file variable is directly within the function (see below). I have another problem however due to the import function on my page since I switched to php5 last week...my page is built up by the user either pressing a save button or an import button (the function you helped med with). When user presses the save button the form post value is saved and when the import button is pressed the import function executes. This is controlled by a function as follows: function sieorform($formname,$sievalue){if(isset($_POST["spara"])){return $_POST[$formname];} else if(isset($_POST["SIE"])){return $sievalue;}} this however returns an error when the user presses save: Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/writeform.php on line 109 Line 109 is the foreach of the import function: function calculate_range($cat, $bool, $range_min, $range_max) { $contents = file($_FILES['userfile']['name']); foreach($contents as $line_value) { list($_cat, $_bool, $_id, $num) = explode("\t", $line_value); $_cat = str_replace('#', '', $_cat); $number[$_cat][$_bool][$_id] = trim($num); } $total = 0; for($i = $range_min; $i <= $range_max; $i++) { if(isset($number[$cat][$bool][$i])) { $total += $number[$cat][$bool][$i]; } } return $total; } Here is an example of the whole code: sieorform(RRintNOcy,calculate_range("RES", 0, 3000, 3799) My guess is that calculate_range still is executed despite the save button (spara) is pressed and can't find a file to import so it returns an error? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 As you're passing calculate_range function as a parameter for the sieorform function PHP will run calculate_range function whether or not the form has been submitted or not. Quote Link to comment Share on other sites More sharing options...
lindm Posted November 25, 2007 Author Share Posted November 25, 2007 Hmm I thought the if function stopped after the first argument if this was fulfilled? On other words it would ignore executing the second part, but obviously not then? I there a simple way to achieve this? Thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 25, 2007 Share Posted November 25, 2007 Try the following: function sieorform($formname, $sievalue) { if(isset($_POST["spara"])) { return $_POST[$formname]; } elseif(isset($_POST["SIE"])) { return eval("calculate_range($sievalue);"); } } sieorform(RRintNOcy, "'RES', 0, 3000, 3799"); What I have done instead is pass just the arguments for the calculate_range function as a string to the sieorform function. Then if $_POST["SIE"] isset and and $_POST["spara"] is not it'll run the calculate_range function with the passed parameters. NOTE: You will have to enclose the the parameters in quotes for the second parameter when calling the sieorform function otherwise the code wont work. Example: Correct sieorform(RRintNOcy, "'RES', 0, 3000, 3799"); Incorrect sieorform(RRintNOcy, 'RES', 0, 3000, 3799); Quote Link to comment Share on other sites More sharing options...
lindm Posted November 25, 2007 Author Share Posted November 25, 2007 Not the ultimate solution for me. Been trying around a bit and was thinking that in the function calculate_range I add a condition that it continues to excute if if(isset($_POST["SIE"])). Currently no errors are produced if I press save or import but the save part with getting the info from the post paramater not working: Is this a possible solution? function calculate_range($cat, $bool, $range_min, $range_max) { if(isset($_POST["SIE"])){ $contents = file($_FILES['userfile']['name']); foreach($contents as $line_value) { list($_cat, $_bool, $_id, $num) = explode("\t", $line_value); $_cat = str_replace('#', '', $_cat); $number[$_cat][$_bool][$_id] = trim($num); } $total = 0; for($i = $range_min; $i <= $range_max; $i++) { if(isset($number[$cat][$bool][$i])) { $total += $number[$cat][$bool][$i]; } } return $total; } } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 25, 2007 Share Posted November 25, 2007 You could do. Quote Link to comment Share on other sites More sharing options...
lindm Posted November 26, 2007 Author Share Posted November 26, 2007 Works...now I just need to find a solution for giving an error if no file has been chosen and import chosen. Tried both javascript and php but nogo so far. Any easy solution? Great help btw! You deserve a helpfulness medal Quote Link to comment 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.