Last question of the day i promise!
Well, i have a search stock form. Users check a checkbox next to a product they want, and then enter a quantity for each one.
I have validated to ensure that for each checked product a quantity has been entered:
// Check that for each of the parts selected (the id array) there is a corresponding quantity
foreach($_POST['id'] as $key=>$value){
$quantity_value = $_POST['quantity'][$key];
if(trim($quantity_value) == '') {
$error['quantity'][$key] = 'You didn\'t enter a <b>Quantity</b> for: Item number ' . ($_POST['id'][$key] + 1) . ' - Part number <b>' . $_POST['part-number'][$key] .'</b>.';
} elseif(preg_match($badwords, trim($quantity_value)) !== 0 || preg_match($exploits, trim($quantity_value)) !== 0) {
$error['quantity'][$key] = 'You entered a <b>Quantity</b> which contains unacceptable or explicit words for ' . $_POST['part-number'][$key];
} else {
$quantity_value = trim(filter_var($_POST['quantity-value'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES));
unset($error['quantity']);
}
}
Now i am trying the converse - checking that for each quantity entered a product has been selected, and im running into issue.
The two arrays, id and quantity looks like this, for example:
Array ( [0] => 0 [1] => 1 )
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => )
So you can see that only two products have been selected (2 values in id array), but three quantities have been entered (10, 20 and 30) - the product corresponding to a quantity of 30 hasnt been checked. How do i code this? Ive tried (and failed):
// Check that for each of the parts selected (the id array) there is a corresponding quantity
foreach($_POST['quantity'] as $key=>$value){
$part_value = $_POST['id'][$key];
if(array_key_exists($key, $_POST['quantity']) && !is_null($key) && !array_key_exists($key, $_POST['id'])) {
$error['part-value'][$key] = 'You didn\'t select a <b>Part Number</b> for: Item number ' . ($_POST['id'][$key] + 1) . ' - Quantity <b>' . $_POST['quantity'][$key] .'</b>.';
} else {
unset($error['part-value']);
}
}
Im really scratching my head on this one, i can check that the array keys do and dont exist, but i need to deal with some of the values of quantity not being set. Any ideas?
Thanks a lot