I have a piece of code that I’m not happy with at all:
// Search for checkboxes ('chk-1','chk-2','chk-3'...)
foreach ($_POST as $key => $val) {
if (substr($key, 0,3) == 'chk') {
$appObj->cats[] = intval(substr($key,4,strlen($key)));
}
I really don’t like this as $_POST can contain upwards of 40+ variables and I am already applying logic to all other posted vars.
Has anyone got any ideas / solutions to achieving the above without cycling through every $_POST var?
The only way how you could avoid cycling trough all items and using
strncmp,strposorsubstris building check-boxes as one array (lets have$key = 7as example):And php:
This would would, but it may create key gaping:
Which may cause problems when someone will try processing data with
forloop.Better approach is this one:
And php: