Jump to content

[SOLVED] array problem


xiao

Recommended Posts

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.

Link to comment
Share on other sites

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();	
}
?>

Link to comment
Share on other sites

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();	
}
?>

Link to comment
Share on other sites

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.

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.