I'd do it with bit flags, it allows advanced reporting and simplifies database structure.
PHP Code:
define('ELECTRICITY_INCLUDED', 0x1);
define('ELECTRICITY_COIN', 0x2);
define('ELECTRICITY_CHARGED', 0x4);
Give each checkbox the same name e.g.
PHP Code:
<input type="checkbox" value="<? echo ELECTRICITY_INCLUDED; ?>" name="features[]" />
<input type="checkbox" value="<? echo ELECTRICITY_COIN; ?>" name="features[]" />
<input type="checkbox" value="<? echo 'ELECTRICITY_CHARGED; ?>" name="features[]" />
then, to store the data:
PHP Code:
$features = 0;
if (is_array($_POST['features')) {
foreach ($_POST['features'] as $feature) {
$features |= $feature;
}
}
Now store $features (if they're all checked it will be 7 in this example).
And then for querying based on features you can do this:
PHP Code:
$mysqli->query('SELECT PlaceName FROM places WHERE ' . ELECTRICITY_INCLUDED . ' & features');
Which will bring back any place with electricity included as a feature.
It also allows for easy negative reporting:
PHP Code:
$mysqli->query('SELECT PlaceName FROM places WHERE ' . ELECTRICITY_INCLUDED . ' & ~features');
(All places which don't have electricity included)
Which isn't easily possible with the table method (you'd either need a sub query or an extra on/off field in the features table).
Bookmarks