I have a PHP form which posts any number of records for the following:
'asset_ID', 'percent_owner_<?=$members['member_ID']?>'
The PHP page receiving the post separates the percent_owner and member_ID using the following:
foreach ($_POST AS $input => $value) {
if (stristr($input, 'percent_owner_')) {
$memberID = str_replace('percent_owner_', '', $input);
$data[$memberID] = array(
'memberID' => $memberID,
'percentOwner' => $value
);
}
}
The next part of the script enters whatever number of records into MySQL using the following:
foreach ($data AS $share) {
$asset_ID = $_GET['asset_ID'];
$query = "INSERT INTO shares (asset_ID, member_ID, percent_owner)
VALUES ('$asset_ID', '{$share['memberID']}', '{$share['percentOwner']}')";
$add_shares = $db->exec($query);
}
All of that works just fine. What I am having trouble with now is before the MySQL insert above, I would like to validate that all the percentages total up to 100%. For example, if there are 3 members with 25%, 25%, and 25%, I want to generate an error message and not do the insert. Right now, you have to manually type in the percentages and total them to 100 yourself. The database will accept less or more than 100% which breaks the math.
I was playing around with the following command before the insert to no avail:
foreach ($data AS $share => $value) {
$total = array_sum($value);
}
include('./view/temp.php');
I am very new to PHP so please bear with my ignorance.
Thanks!
Something with the following flow will suffice your need i think.