I am looking for the right syntax how to sum up a specific variable that I looped. basically, I made a loop. This is the source code.
<?php
$n = $_POST['n'];
for ($i=1;$i<=$n;$i++) {
?>
<h3 style="text-align:center"><b>ITEM <?php echo $i?>:</b></h3>
<p>DESCRIPTION:</p>
<input type="text" class="form-control" name="<?php echo $i.'description'; ?>" value="" autofocus>
<p>PROGRAM UNIT:</p>
<input type="text" class="form-control" name="<?php echo $i.'prog_unit'; ?>" value="" autofocus>
<p>PROGRAM QUANTITY:</p>
<input type="number" class="form-control" name="<?php echo $i.'prog_quantity'; ?>" min="0" value="0" step= ".001" autofocus>
<p>PROGRAM UNIT COST:</p>
<input type="number" class="form-control" name="<?php echo $i.'prog_unitcost'; ?>" min="0" value="0" step= ".00001" autofocus>
<p>QUANTITY ACCOMPLISHED PREVIOUS:</p>
<input type="number" class="form-control" name="<?php echo $i.'q_prev'; ?>" min="0" value="0" step= ".01" autofocus>
<p>QUANTITY ACCOMPLISHED THIS REPORT:</p>
<input type="number" class="form-control" name="<?php echo $i.'q_report'; ?>" min="0" value="0" step= ".01" autofocus>
<p>QUANTITY ACCOMPLISHED TOTAL TO DATE:</p>
<input type="number" class="form-control" name="<?php echo $i.'q_total_date'; ?>" min="0" value="0" step= ".01" autofocus>
<p>PERCENT ACCOMPLISHED PREVIOUS:</p>
<input type="number" class="form-control" name="<?php echo $i.'p_prev'; ?>" min="0" value="0.00" step= ".01" autofocus>
<p>COST INCURRED:</p>
<input type="number" class="form-control" name="<?php echo $i.'cost'; ?>" min="0" value="0" step= ".01" autofocus>
<input type="hidden" name="n" value="<?php echo $n; ?>"><br>
<?php }?>
This is the output of the code above. enter image description here
And then, this is the syntax where I insert the looped values in to the database.
if(isset($_POST['submit']))
{
$n = $_POST['n'];
for($i=1;$i<=$n;$i++)
{
$project = $_POST["project"];
$description = $_POST[$i."description"];
$prog_unit = $_POST[$i."prog_unit"];
$prog_quantity = $_POST[$i."prog_quantity"];
$prog_unitcost = $_POST[$i."prog_unitcost"];
$prog_total = ($prog_quantity * $prog_unitcost);
$q_prev = $_POST[$i."q_prev"];
$q_report = $_POST[$i."q_report"];
$q_total_date = $_POST[$i."q_total_date"];
$p_prev = $_POST[$i."p_prev"];
$p_report = ($q_total_date/$prog_quantity)*100;
$cost = $_POST[$i."cost"];
mysqli_query($bd,"insert into detailed_rpapi (project,description,prog_unit,prog_quantity,prog_unitcost,prog_total,q_prev,q_report,q_total_date,p_prev,p_report,cost)
values ('$project','$description','$prog_unit','$prog_quantity','$prog_unitcost','$prog_total','$q_prev','$q_report','$q_total_date','$p_prev','$p_report','$cost')");
}
$sum = $prog_total + $prog_total;
echo $sum;
echo "Data Added Successfully ...";
}
I want to sum all the value from $prog_total. Please help me huhuhu