I'm converting a simple accommodation listing web site to be database driven. The visitor is to be able to select accommodation by six types and six locations, plus some other criteria. I did this with 'Select' boxes, but have been asked to change this to checkboxes so the visitor can select more than one accommodation type.
I've named the six accommodation type checkboxes 'ac0', ac1', etc. (ac0 = all types). I can handle this so long as the number remains six, using a 'for' loop:
This gives me a valid 'where' clause for MySQL.Code:$where = "WHERE "; if (isset($_GET) [other conditions]) { // If ac0 is set, no need to filter on accn_type for ($i = 1; $i < 6; $i++) { if (isset($_GET['ac'.$i])) { $where .= "(accn_type = '" . $_GET['ac'.$i] . "' or "; } } // Remove trailing 'or' and excess leading brackets from where clause $where = substr($where, 0, -4) . ")"; $where = str_replace("or (", "or ", $where); // echo $where; // exit(); }
Now here's my problem:
If I were to add or remove an accommodation type I'd have to change the '$i < 6' in the 'for' loop, and similar fixed integers further on in the script. It would be better to have '$i < count(something)'.
I'm trying to find a way to count the number of parameters in the $_GET with key 'ac?' where '?' is an integer (currently 0-5).
I could run another 'for' loop, but I was hoping to find something smarter. I'll have to do the same for the locations too.
Any suggestions, please ?





Bookmarks