# array_sum

Hopefully a simple one..
How do I calculate the sum of \$value?
foreach(\$mark as \$ExamNo => \$value) {

if(\$value != "") {
echo '<li>'.\$ExamNo.' = '.\$value.'</li>';
echo "the sum of \$value"
}

}
Thanks,
N
LVL 5
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
If \$mark is an array of values then

\$totalMark = array_sum( \$mark );

should do it. See http://www.php.net/array_sum
0
Web UX/UI DeveloperAuthor Commented:
No, I specifically need the sum of the \$value inside the array
0
Commented:
"I specifically need the sum of the \$value inside the array"

I don't undertand what you mean by this. Do you mean
``````\$sum_value = 0;

foreach(\$mark as \$ExamNo => \$value) {

if(\$value != "") {
echo '<li>'.\$ExamNo.' = '.\$value.'</li>';

\$sum_value += \$value;
echo "the sum of \$sum_value"
}

}
``````
0
Web UX/UI DeveloperAuthor Commented:
I have mocked up what I am trying to achieve here: http://www.statusconsultinglimited.com/questions.html
This should explain the aim of my question..
<?php
\$mark = \$_POST["mark"];

foreach(\$mark as \$ExamNo => \$value) {

if(\$value != "") {
echo '<li>'.\$ExamNo.' = '.\$value.'</li>';
}

}
echo"<br/>
You score was";
?>
0
Commented:
Test the script here:

Outputs something like:
THE SUM IS float(8.5)
``````<?php // RAY_temp_neil_bradley.php
error_reporting(E_ALL);

// SHOW HOW TO SUM AN ARRAY OF DATA FROM A POSTED FORM

// WHAT ARRIVED IN THE POST ARRAY?
if (isset(\$_POST))
{
// ACTIVATE THIS CODE TO SHOW THE POST ARRAY
// echo "<pre>";
// var_dump(\$_POST);
// echo "</pre>";

// SUM THE "mark" SUB-ARRAY IN THE POST ARRAY
\$sum = array_sum(\$_POST["mark"]);
echo "THE SUM IS ";
var_dump(\$sum);
}

// END OF PHP - USE THE HTML FROM THE LINK AT EE

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<body>
<form method="post">
<ul>
<li class="required">
<label>
<strong>1 Question 1?</strong><br/>

<br/>

<label><strong>2 Question 2</strong><br/>

</label>
</li>
</ul>
<ul>
<li class="required">
<label> <strong>3 Question 1?</strong><br/>

<select name="mark[3]" id="mark[3]">

</select>
</label>
<div class="sep"></div>
</li>
<li class="required">
<label><strong>4 Question 2</strong><br/>

<select name="mark[4]" id="mark[4]">

</select>
</label>
</li>

<li class="required">
<input type="submit" name="submit" id="submit" value="Submit" />
</li>
</ul>
</form>
</body>
</html>
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Web UX/UI DeveloperAuthor Commented:
Ray, as always you hit the nail on the head.
Thanks,
N
0
Web UX/UI DeveloperAuthor Commented:
Thanks for that..
Cheers,
N
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.