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?
 
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.