xiao Posted April 4, 2008 Share Posted April 4, 2008 I get this error: Warning: array_sum() [function.array-sum]: The argument should be an array in ...\index.php on line 71 everything works until $totaal = array_sum($ECprijs) + array_sum($prijs); (= line 71) <?php //Extra componenten declareren function extraComponenten(){ $ECaantal = $_POST["aantalextra"]; for($i = 0;$i <= $ECaantal;$i++){ $extracomponent = $_POST["extracomponent".$i]; $ECtype[$i] = substr($extracomponent, 0, 6); $deel = explode(" - ", $extracomponent); $ECprijs[$i] = $deel[1]; } } //Prijs uit string halen function prijs(){ $product[0] = $_POST["processor"]; $product[1] = $_POST["moederbord"]; $product[2] = $_POST["hardeschijf1"]; $product[3] = $_POST["hardeschijf2"]; $product[4] = $_POST["geheugen"]; $product[5] = $_POST["cddvdstation1"]; $product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"]; $product[7] = $_POST["grafischekaart"]; $product[8] = $_POST["voeding"]; $product[9] = $_POST["tvkaart"]; $product[10] = $_POST["geluidskaart"]; $product[11] = $_POST["netwerkkaart"]; $product[12] = $_POST["diskettestation"]; $product[13] = $_POST["kaartlezer"]; $product[14] = $_POST["monitor"]; for($i = 0;$i < 15;$i++){ $deel = explode(" - ", $product[$i]); $prijs[$i] = $deel[1]; } $geheugenaantal = $_POST["geheugenaantal"]; $prijs[4] = $prijs[4] * $geheugenaantal; echo $prijs[4]; } //Totaal berekenen function totaalBerekenen(){ extraComponenten(); prijs(); $totaal = array_sum($ECprijs) + array_sum($prijs); echo $totaal; } if(isset($_POST["totaal"])){ totaalBerekenen(); } ?> Sorry for the dutch code. I tried using global, but that didn't help, or I'm doing it wrong. Both arrays are being filled with values. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 4, 2008 Share Posted April 4, 2008 Have your first two functions return their respective arrays. Otherwise the variables are undefined: <?php //Extra componenten declareren function extraComponenten(){ $ECaantal = $_POST["aantalextra"]; for($i = 0;$i <= $ECaantal;$i++){ $extracomponent = $_POST["extracomponent".$i]; $ECtype[$i] = substr($extracomponent, 0, 6); $deel = explode(" - ", $extracomponent); $ECprijs[$i] = $deel[1]; return $ECprijs; } } //Prijs uit string halen function prijs(){ $product[0] = $_POST["processor"]; $product[1] = $_POST["moederbord"]; $product[2] = $_POST["hardeschijf1"]; $product[3] = $_POST["hardeschijf2"]; $product[4] = $_POST["geheugen"]; $product[5] = $_POST["cddvdstation1"]; $product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"]; $product[7] = $_POST["grafischekaart"]; $product[8] = $_POST["voeding"]; $product[9] = $_POST["tvkaart"]; $product[10] = $_POST["geluidskaart"]; $product[11] = $_POST["netwerkkaart"]; $product[12] = $_POST["diskettestation"]; $product[13] = $_POST["kaartlezer"]; $product[14] = $_POST["monitor"]; for($i = 0;$i < 15;$i++){ $deel = explode(" - ", $product[$i]); $prijs[$i] = $deel[1]; } $geheugenaantal = $_POST["geheugenaantal"]; $prijs[4] = $prijs[4] * $geheugenaantal; echo $prijs[4]; return $prijs } //Totaal berekenen function totaalBerekenen(){ $ECprijs = extraComponenten(); $$prijs = prijs(); $totaal = array_sum($ECprijs) + array_sum($prijs); echo $totaal; } if(isset($_POST["totaal"])){ totaalBerekenen(); } ?> Quote Link to comment Share on other sites More sharing options...
xiao Posted April 4, 2008 Author Share Posted April 4, 2008 I still get the same error. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 4, 2008 Share Posted April 4, 2008 Sorry, there was a stray dollar sign in there. Try: <?php //Extra componenten declareren function extraComponenten(){ $ECaantal = $_POST["aantalextra"]; for($i = 0;$i <= $ECaantal;$i++){ $extracomponent = $_POST["extracomponent".$i]; $ECtype[$i] = substr($extracomponent, 0, 6); $deel = explode(" - ", $extracomponent); $ECprijs[$i] = $deel[1]; return $ECprijs; } } //Prijs uit string halen function prijs(){ $product[0] = $_POST["processor"]; $product[1] = $_POST["moederbord"]; $product[2] = $_POST["hardeschijf1"]; $product[3] = $_POST["hardeschijf2"]; $product[4] = $_POST["geheugen"]; $product[5] = $_POST["cddvdstation1"]; $product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"]; $product[7] = $_POST["grafischekaart"]; $product[8] = $_POST["voeding"]; $product[9] = $_POST["tvkaart"]; $product[10] = $_POST["geluidskaart"]; $product[11] = $_POST["netwerkkaart"]; $product[12] = $_POST["diskettestation"]; $product[13] = $_POST["kaartlezer"]; $product[14] = $_POST["monitor"]; for($i = 0;$i < 15;$i++){ $deel = explode(" - ", $product[$i]); $prijs[$i] = $deel[1]; } $geheugenaantal = $_POST["geheugenaantal"]; $prijs[4] = $prijs[4] * $geheugenaantal; echo $prijs[4]; return $prijs; } //Totaal berekenen function totaalBerekenen(){ $ECprijs = extraComponenten(); $prijs = prijs(); $totaal = array_sum($ECprijs) + array_sum($prijs); echo $totaal; } if(isset($_POST["totaal"])){ totaalBerekenen(); } ?> Quote Link to comment Share on other sites More sharing options...
xiao Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks, it works now I have one more problem. The array_sum function seems to ignore commas in numbers, so if I do for example: 1,5 + 2,9 + 3,7 it returns 6 instead of 8,1 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 4, 2008 Share Posted April 4, 2008 That's because the decimal separator that you need to use is a point and not a comma. PHP doesn't recognise the elements of your array as numbers, so tries to convert them to a number. What it ends up doing is taking everything up to the first non-numeric character as being the number. 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.