I want to sum the values of the 'countryamt' column and echo it out. The inputs are coming from the 'matconfirmation.php'. I stored all the inputs in an array, but i dont know how to sum the values up. The bold part of the code was what i tried doing, but didnt work.

// The Matcomfirmation.php page
<?php ob_start(); ?>
<?php
session_start();
$info="";
if (isset($_SESSION['matricnum'])){

    $matnumber = $_SESSION['matricnum'];
    $matno= strtoupper($matnumber);
    if (!$matno){
        $info= "Please Provide your Matriculation Number";
            }
            else{
            include ("conn.php");
            $sql= "SELECT * FROM tbsturec WHERE matno= '$matnumber'";
            $mat= mysql_query($sql);
        if ((mysql_num_rows($mat))>=1){
            while ($result= mysql_fetch_array($mat)){
                $Sname= $result["Surname"];
                $Oname= $result['Othernames'];
                $Dept= $result['Department'];

            }                       
    }


            }

    }else{

        header('location: userportal.php'); 
    }
session_destroy();
?>


<?php
    if (isset($_POST['continue'])){
        header('location:payment.php');
    }
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<title>Auchipoly Transcript</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/ajax/reload.js" type="text/javascript"></script>

<script>
  document.createElement('header');
  document.createElement('footer');
  document.createElement('section');
  document.createElement('nav');
</script>
<script>
function getFormData()
{
//sname=sname.innerHTML;
//oname=oname.innerHTML;
oname=tForm.onameTxt.value;
dept=tForm.deptTxt.value;
sname=tForm.snameTxt.value;
year=tForm.year.value;
clss=tForm.clss.value;
classType=tForm.classType.value;
country=tForm.country.value;
phone=tForm.phone.value;
address=tForm.address.value;
//matnum=matnum.innerHTML;
//window.alert(address);
if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
        //window.alert(xmlhttp.responseText);

        document.getElementById("mytable").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","processtable.php?sname="+sname+"&oname="+oname+"&dept="+dept+"&year="+year+"&clss="+clss+"&classType="+classType+"&country="+country+"&phone="+phone+"&address="+address,true);
        xmlhttp.send();



}
</script>

<!--[if lt IE 9]>
 <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<p>&nbsp;</p>
<div class="container">
  <header>
    <div id="logo"><a href="#"><img src="images/logo.png" alt="your logo here" width="78" height="63" border="0" class="imagedropshadow" /></a></div>
    <h2>Transcript Ordering System</h2> <nav>
      <div class="navbutton"><a href="contactus.php">contact us </a></div>
     <div class="navbutton"><a href="registration.php">register</a></div>
    <div class="navbutton"><a href="#">Payment opt</a></div>
      <div class="navbutton"><a href="index.php">Home Page </a></div>           
    </nav> 

    <div class="photo">
    <div class="content">
     </nav> 
     <h3>Transcript Processing</h3>
    </div>
    </div>

    <div id="login"><form method="post" action="http://YOUR_DOMAIN_HERE/login/login.php">
      <p>&nbsp;</p>
      <p><br />
      </p>
      <div>


  <input class="input-prepend" type="text" id="inputEmail" placeholder="Email">
<input type="password" class="input-prepend" placeholder="Password">
<button class="btn btn-warning" type="button">Login</button>
</div>

       <div>
        <h3>In order to gain access to the online transcript system, you will need to register.</h3>
        <div class="readbutton"><a href="registration.php">Register</a></div>
      </div>
    </form>
    </div>
<div id="main">
      <h2>Transcript Request</h2>
      <pre><strong><font color="#990000">Enter your MATRICULATION NUMBER. Please make sure the supplied MATRICULATION NUMBER is authentic, So as to avoid restriction to the application</font></strong></pre>

<div>
 <?php /*if (isset($_POST['proceed'])) { 
 if ($info){?>
    <div class="alert alert-error"> 
<?php
       echo $info;  
}}

*/
?> 

</div>



<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
    <div align="center" class="form-actions">
</form>
          <p>&nbsp;</p>
  <form name="tForm" id="tForm" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
    <?php // if (isset($_POST['proceed'])) { ?>
        <?php if (!$info){ ?>
        <div class="navbar-inner">
            <br>
          <h4 align="left" class="alert alert-info">
          <div id="oname">Welcome! <?php echo $Oname; ?>, Please specify where you would like us to send your transcript to.</div></h4>
         <h2 align="center">  <div id="matno">Mat. Number:<?php echo $matno; ?></div> </h2>
         <table width="97%" height="230" border="0" class="table">
           <tr>
    <td width="12%"><strong>
      SURNAME :</strong></td>
    <td width="31%"><input id="snameTxt" type="text" value="<?php echo $Sname;?>" disabled/></td>
    <td width="20%"><strong>OTHERNAMES:</strong></td>
    <td width="37%"><input id="onameTxt" type="text" value="<?php echo $Oname;?>" disabled/></td>
  </tr>
  <tr>
    <td><strong>Department</strong></td>
    <td><input id="deptTxt" type="text" value="<?php echo $Dept;?>" disabled/></td>
    <td><strong> Class </strong></td>
    <td><select class="span3" name="clss" id="clss">
        <option value="HND">HND</option>
        <option value="OND">OND</option>

    </select></td>
  </tr>
  <tr>
            <td><strong>Study Mode</strong></td>
            <td>
                    <select class="span3" name="classType" id="classType">
                        <option value="Morning">Morning</option>
                        <option value="Evening">Evening</option>

                    </select>
            </td>
            <td><strong>Year of Graduation</strong></td>
            <td><input id="year" class="span3 input-mini" name="zipcode" type="text"></td>
  </tr>
  <tr>
    <td><strong>Address</strong></td>
    <td>

      <textarea class="span3" name="address" id="address" cols="45" rows="3"></textarea></td>
    <td><strong>Country</strong></td>
    <td><select class="span3" name="country" id="country"> 
          <option value="AF">Afghanistan</option> 
          <option value="AL">Albania</option> 
          <option value="DZ">Algeria</option> 
          <option value="AS">American Samoa</option> 
          <option value="AD">Andorra</option> 
          <option value="AO">Angola</option> 
          <option value="AI">Anguilla</option> 
          <option value="AQ">Antarctica</option> 
          <option value="AG">Antigua and Barbuda</option> 
          <option value="AR">Argentina</option> 
          <option value="AM">Armenia</option> 
          <option value="AW">Aruba</option> 
          <option value="AU">Australia</option> 
          <option value="AT">Austria</option> 
          <option value="AZ">Azerbaijan</option> 
          <option value="BS">Bahamas</option> 
          <option value="BH">Bahrain</option> 
          <option value="BD">Bangladesh</option> 
          <option value="BB">Barbados</option> 
          <option value="BY">Belarus</option> 
          <option value="BE">Belgium</option> 
          <option value="BZ">Belize</option> 
          <option value="BJ">Benin</option> 
          <option value="BM">Bermuda</option> 
          <option value="BT">Bhutan</option> 
          <option value="BO">Bolivia</option> 
          <option value="BA">Bosnia and Herzegowina</option> 
          <option value="BW">Botswana</option> 
          <option value="BV">Bouvet Island</option> 
          <option value="BR">Brazil</option> 
          <option value="IO">British Indian Ocean Territory</option> 
          <option value="BN">Brunei Darussalam</option> 
          <option value="BG">Bulgaria</option> 
          <option value="BF">Burkina Faso</option> 
          <option value="BI">Burundi</option> 
          <option value="KH">Cambodia</option> 
          <option value="CM">Cameroon</option> 
          <option value="CA">Canada</option> 
          <option value="CV">Cape Verde</option> 
          <option value="KY">Cayman Islands</option> 
          <option value="CF">Central African Republic</option> 
          <option value="TD">Chad</option> 
          <option value="CL">Chile</option> 
          <option value="CN">China</option> 
          <option value="CX">Christmas Island</option> 
          <option value="CC">Cocos (Keeling) Islands</option> 
          <option value="CO">Colombia</option> 
          <option value="KM">Comoros</option> 
          <option value="CG">Congo</option> 
          <option value="CD">Congo, the Democratic Republic of the</option> 
          <option value="CK">Cook Islands</option> 
          <option value="CR">Costa Rica</option> 
          <option value="CI">Cote d'Ivoire</option> 
          <option value="HR">Croatia (Hrvatska)</option> 
          <option value="CU">Cuba</option> 
          <option value="CY">Cyprus</option> 
          <option value="CZ">Czech Republic</option> 
          <option value="DK">Denmark</option> 
          <option value="DJ">Djibouti</option> 
          <option value="DM">Dominica</option> 
          <option value="DO">Dominican Republic</option> 
          <option value="TP">East Timor</option> 
          <option value="EC">Ecuador</option> 
          <option value="EG">Egypt</option> 
          <option value="SV">El Salvador</option> 
          <option value="GQ">Equatorial Guinea</option> 
          <option value="ER">Eritrea</option> 
          <option value="EE">Estonia</option> 
          <option value="ET">Ethiopia</option> 
          <option value="FK">Falkland Islands (Malvinas)</option> 
          <option value="FO">Faroe Islands</option> 
          <option value="FJ">Fiji</option> 
          <option value="FI">Finland</option> 
          <option value="FR">France</option> 
          <option value="FX">France, Metropolitan</option> 
          <option value="GF">French Guiana</option> 
          <option value="PF">French Polynesia</option> 
          <option value="TF">French Southern Territories</option> 
          <option value="GA">Gabon</option> 
          <option value="GM">Gambia</option> 
          <option value="GE">Georgia</option> 
          <option value="DE">Germany</option> 
          <option value="GH">Ghana</option> 
          <option value="GI">Gibraltar</option> 
          <option value="GR">Greece</option> 
          <option value="GL">Greenland</option> 
          <option value="GD">Grenada</option> 
          <option value="GP">Guadeloupe</option> 
          <option value="GU">Guam</option> 
          <option value="GT">Guatemala</option> 
          <option value="GN">Guinea</option> 
          <option value="GW">Guinea-Bissau</option> 
          <option value="GY">Guyana</option> 
          <option value="HT">Haiti</option> 
          <option value="HM">Heard and Mc Donald Islands</option> 
          <option value="VA">Holy See (Vatican City State)</option> 
          <option value="HN">Honduras</option> 
          <option value="HK">Hong Kong</option> 
          <option value="HU">Hungary</option> 
          <option value="IS">Iceland</option> 
          <option value="IN">India</option> 
          <option value="ID">Indonesia</option> 
          <option value="IR">Iran (Islamic Republic of)</option> 
          <option value="IQ">Iraq</option> 
          <option value="IE">Ireland</option> 
          <option value="IL">Israel</option> 
          <option value="IT">Italy</option> 
          <option value="JM">Jamaica</option> 
          <option value="JP">Japan</option> 
          <option value="JO">Jordan</option> 
          <option value="KZ">Kazakhstan</option> 
          <option value="KE">Kenya</option> 
          <option value="KI">Kiribati</option> 
          <option value="KP">Korea, Democratic People's Republic of</option> 
          <option value="KR">Korea, Republic of</option> 
          <option value="KW">Kuwait</option> 
          <option value="KG">Kyrgyzstan</option> 
          <option value="LA">Lao People's Democratic Republic</option> 
          <option value="LV">Latvia</option> 
          <option value="LB">Lebanon</option> 
          <option value="LS">Lesotho</option> 
          <option value="LR">Liberia</option> 
          <option value="LY">Libyan Arab Jamahiriya</option> 
          <option value="LI">Liechtenstein</option> 
          <option value="LT">Lithuania</option> 
          <option value="LU">Luxembourg</option> 
          <option value="MO">Macau</option> 
          <option value="MK">Macedonia</option> 
          <option value="MG">Madagascar</option> 
          <option value="MW">Malawi</option> 
          <option value="MY">Malaysia</option> 
          <option value="MV">Maldives</option> 
          <option value="ML">Mali</option> 
          <option value="MT">Malta</option> 
          <option value="MH">Marshall Islands</option> 
          <option value="MQ">Martinique</option> 
          <option value="MR">Mauritania</option> 
          <option value="MU">Mauritius</option> 
          <option value="YT">Mayotte</option> 
          <option value="MX">Mexico</option> 
          <option value="FM">Micronesia, Federated States of</option> 
          <option value="MD">Moldova, Republic of</option> 
          <option value="MC">Monaco</option> 
          <option value="MN">Mongolia</option> 
          <option value="MS">Montserrat</option> 
          <option value="MA">Morocco</option> 
          <option value="MZ">Mozambique</option> 
          <option value="MM">Myanmar</option> 
          <option value="NA">Namibia</option> 
          <option value="NR">Nauru</option> 
          <option value="NP">Nepal</option> 
          <option value="NL">Netherlands</option> 
          <option value="AN">Netherlands Antilles</option> 
          <option value="NC">New Caledonia</option> 
          <option value="NZ">New Zealand</option> 
          <option value="NI">Nicaragua</option> 
          <option value="NE">Niger</option> 
          <option value="NG">Nigeria</option> 
          <option value="NU">Niue</option> 
          <option value="NF">Norfolk Island</option> 
          <option value="MP">Northern Mariana Islands</option> 
          <option value="NO">Norway</option> 
          <option value="OM">Oman</option> 
          <option value="PK">Pakistan</option> 
          <option value="PW">Palau</option> 
          <option value="PA">Panama</option> 
          <option value="PG">Papua New Guinea</option> 
          <option value="PY">Paraguay</option> 
          <option value="PE">Peru</option> 
          <option value="PH">Philippines</option> 
          <option value="PN">Pitcairn</option> 
          <option value="PL">Poland</option> 
          <option value="PT">Portugal</option> 
          <option value="PR">Puerto Rico</option> 
          <option value="QA">Qatar</option> 
          <option value="RE">Reunion</option> 
          <option value="RO">Romania</option> 
          <option value="RU">Russian Federation</option> 
          <option value="RW">Rwanda</option> 
          <option value="KN">Saint Kitts and Nevis</option> 
          <option value="LC">Saint LUCIA</option> 
          <option value="VC">Saint Vincent and the Grenadines</option> 
          <option value="WS">Samoa</option> 
          <option value="SM">San Marino</option> 
          <option value="ST">Sao Tome and Principe</option> 
          <option value="SA">Saudi Arabia</option> 
          <option value="SN">Senegal</option> 
          <option value="SC">Seychelles</option> 
          <option value="SL">Sierra Leone</option> 
          <option value="SG">Singapore</option> 
          <option value="SK">Slovakia (Slovak Republic)</option> 
          <option value="SI">Slovenia</option> 
          <option value="SB">Solomon Islands</option> 
          <option value="SO">Somalia</option> 
          <option value="ZA">South Africa</option> 
          <option value="GS">South Georgia and the South Sandwich Islands</option> 
          <option value="ES">Spain</option> 
          <option value="LK">Sri Lanka</option> 
          <option value="SH">St. Helena</option> 
          <option value="PM">St. Pierre and Miquelon</option> 
          <option value="SD">Sudan</option> 
          <option value="SR">Suriname</option> 
          <option value="SJ">Svalbard and Jan Mayen Islands</option> 
          <option value="SZ">Swaziland</option> 
          <option value="SE">Sweden</option> 
          <option value="CH">Switzerland</option> 
          <option value="SY">Syrian Arab Republic</option> 
          <option value="TW">Taiwan, Province of China</option> 
          <option value="TJ">Tajikistan</option> 
          <option value="TZ">Tanzania, United Republic of</option> 
          <option value="TH">Thailand</option> 
          <option value="TG">Togo</option> 
          <option value="TK">Tokelau</option> 
          <option value="TO">Tonga</option> 
          <option value="TT">Trinidad and Tobago</option> 
          <option value="TN">Tunisia</option> 
          <option value="TR">Turkey</option> 
          <option value="TM">Turkmenistan</option> 
          <option value="TC">Turks and Caicos Islands</option> 
          <option value="TV">Tuvalu</option> 
          <option value="UG">Uganda</option> 
          <option value="UA">Ukraine</option> 
          <option value="AE">United Arab Emirates</option> 
          <option value="GB" selected="selected">United Kingdom</option> 
          <option value="US">United States</option> 
          <option value="UM">United States Minor Outlying Islands</option> 
          <option value="UY">Uruguay</option> 
          <option value="UZ">Uzbekistan</option> 
          <option value="VU">Vanuatu</option> 
          <option value="VE">Venezuela</option> 
          <option value="VN">Viet Nam</option> 
          <option value="VG">Virgin Islands (British)</option> 
          <option value="VI">Virgin Islands (U.S.)</option> 
          <option value="WF">Wallis and Futuna Islands</option> 
          <option value="EH">Western Sahara</option> 
          <option value="YE">Yemen</option> 
          <option value="YU">Yugoslavia</option> 
          <option value="ZM">Zambia</option> 
          <option value="ZW">Zimbabwe</option> 
        </select> &nbsp;</td>
  </tr>
  <tr>
    <td><strong>Phone Number</strong></td>
    <td><input class="span3 input-mini" id="phone" name="phone" type="text"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
         <br>

<table width="200" class="table" border="0">
  <tr>
    <td align="center" class="span3">
    <pre><font color="#990000"><strong>By Clicking on the "Add to Cart" Button, You have agreed to the terms and conditions of E-Transcript.</strong></font></pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" name="proceed2" id="proceed2" class="btn btn-info" onClick="getFormData()">+ Add to Cart</button>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button type="submit" name="continue" id="checkout" class="btn btn-info">Continue</button></td>

  </tr>
</table>

</div>

<?php } ?>

  </form>

<div id="mytable"></div>

    <footer>
        <div align="center">
          <p>&nbsp;</p>
          <p>&copy;2013 Auchi Polytechnic Transcript Unit</p>
          <p>&nbsp;</p>
        </div></footer>    



</body> 
</html>

//The table where the values are to show up.

<?php
//include ("conn.php");
$sname=$_GET['sname'];
$oname=$_GET['oname'];
$year=$_GET['year'];
$dept=$_GET['dept'];
$clss=$_GET['clss'];
$classType=$_GET['classType'];
$country=$_GET['country'];
$phone=$_GET['phone'];
$address=$_GET['address'];
//echo $phone . $sname . $oname . $year . $clss . $classType . $country . $address;
if ($country=='NG'){
    $countryamt= '2000';
}else{
    $countryamt= '5000' ;}

$tableDataNew=array('sname' => $sname,
         'oname' => $oname,
         'dept' => $dept,
        'year'=>$year,
         'clss' => $clss,
          'classType' => $classType,
         'country' => $country,
          'phone' => $phone,
          'address' => $address,
          'countryamt'=>$countryamt,
        );

session_start();
$_SESSION['tb']= $tableDataNew['countryamt'];
echo  current($tableDataNew['countryamt']);

** $sum = 0;

** foreach($_SESSION['tb'] as $key=>$value) {
if ($key=='countryamt') {
$sum += $value;
}
}
echo $sum**
//$tbb[]= $_SESSION['tb']

//session_destroy();
if (isset($_SESSION['tableData'])){
$tableData=$_SESSION['tableData'];
$tableData[]=$tableDataNew;
$_SESSION['tableData']=($tableData);

?>
<table border="0" class="table table-bordered">
<tr><td width="81"><b>SURNAME</b></td><td width="64"><b>OTHER NAMES</b></td><td width="115"><b>DEPARTMENT</b></td><td width="113"><b>GRADUATION YEAR</b></td><td width="75"><b>ADDRESS</b></td><td width="73"><b>AMOUNT</b></td></tr>

<?php foreach($tableData as $key=>$value){ ?>
<tr>
<?php

foreach ($value as $key2=>$val2){ ?>
<?php if ($key2=='sname'){ echo "<td>" . $val2 . "</td>";} ?>
<?php if ($key2=='oname'){ echo "<td>" . $val2 . "</td>";} ?>
<?php if ($key2=='dept'){ echo "<td>" . $val2 . "</td>";} ?>
<?php if ($key2=='year'){ echo "<td>" . $val2 . "</td>";} ?>
<?php if ($key2=='address'){ echo "<td>" . $val2 . "</td>";} ?>
<?php if ($key2=='countryamt'){echo "<td>" . $val2. "</td>";}?>

<?php }
?>
</tr>
<?php }?>
<tr class="success">
    <td colspan="5" ><i><b><font color="#990000"> Total Amount:</font></b></i></td>
    <td align="right"><font color="#990000"><b>
    <?php ?></b></font></td>
</tr>

</table>
<?php
//print_r($tableData);
}
else{

$tableData[]=$tableDataNew;
$_SESSION['tableData']=($tableData);

} 

//$newData=$_SESSION['tableData'];

//print_r($newData);
//empty($_GET);
?>
Member Avatar for diafol
$sum = 0;
foreach($_SESSION['tb'] as $key=>$value) {
    if ($key=='countryamt') {
        $sum += $value;
    }
}
echo $sum;

THAT'S IT?? What's with all the other stuff?

If you want to add, why not...

$sum = 0;
if(isset($_SESSION['tb']['countryamt'])) $sum += intval($_SESSION['tb']['countryamt']);
echo $sum;

Thanks.. I just posted it so you know where the inputs are coming from and whats in the array.. i just tried the code you posted.. it prints 5. I dont know where thats coming from

Member Avatar for diafol

So the problem's not with the code highlighted, but with the input to SESSION['tb']['countryamt'] ? If so, you need to track down the value of that variable at the point at which you set it (using echo and possible exit) - sorry I'm not going to wade through almost 600 lines of mixed php/html/js.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.