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
Neil_BradleyWeb UX/UI DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Beverley PortlockCommented:
If $mark is an array of values then

$totalMark = array_sum( $mark );

should do it. See http://www.php.net/array_sum
0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
No, I specifically need the sum of the $value inside the array
0
Beverley PortlockCommented:
"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"
            }

      }

Open in new window

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Neil_BradleyWeb 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
Ray PaseurCommented:
Test the script here:
http://www.laprbass.com/RAY_temp_neil_bradley.php

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

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

                              a) possible answer<br/>
                              b) possible answer<br/>
                              c) possible answer
<br/>
d) possible answer<br/> <select name="mark[1]">
                                                                <option value="">-- select answer --</option><option value="0">Answer a)</option><option value="0">Answer b)</option><option value="8.5">Answer c)</option><option value="0">Answer d)</option></select></label><div class="sep"></div><li class="required">

                                                                  <label><strong>2 Question 2</strong><br/>
                                                                  a) possible answer<br/>
                                                                  b) possible answer<br/>
                                                                  c) possible answer<br/>
                                                                  d) possible answer<br/><select name="mark[2]">
                                                                <option value="">-- select answer --</option><option value="8.5">Answer a)</option><option value="0">Answer b)</option><option value="0">Answer c)</option><option value="0">Answer d)</option></select>

                                                                </label>
                                                                </li>
</ul>
 <ul>
   <li class="required">
     <label> <strong>3 Question 1?</strong><br/>
       a) possible answer<br/>
       b) possible answer<br/>

       c) possible answer <br/>
       d) possible answer<br/>
       <select name="mark[3]" id="mark[3]">
         <option value="">-- select answer --</option>
         <option value="0">Answer a)</option>
         <option value="0">Answer b)</option>

         <option value="8.5">Answer c)</option>
         <option value="0">Answer d)</option>
       </select>
     </label>
     <div class="sep"></div>
   </li>
   <li class="required">
     <label><strong>4 Question 2</strong><br/>

       a) possible answer<br/>
       b) possible answer<br/>
       c) possible answer<br/>
       d) possible answer<br/>
       <select name="mark[4]" id="mark[4]">
         <option value="">-- select answer --</option>

         <option value="8.5">Answer a)</option>
         <option value="0">Answer b)</option>
         <option value="0">Answer c)</option>
         <option value="0">Answer d)</option>
       </select>
     </label>
   </li>

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Ray, as always you hit the nail on the head.
Thanks,
N
0
Neil_BradleyWeb 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.