php - Inserting commas between city, state, country input depending on which locations are submitted -
php - Inserting commas between city, state, country input depending on which locations are submitted -
i have 3 (optionally entered) post variables: city, state, , country. i'm unsure how check see 3 not empty , insert commas between them accordingly. might come in city, come in city , state, city , country, etc. know there's easy way of doing i'm having problem doing without making many more lines of code need. example:
<?php $country = $_post['country']; $state = $_post['state']; $city = $_post['city']; if (!empty($city)){ $location = $city; } if (!empty($state) && !empty($city)){ $location .= ', ' . $state; } if (!empty($ state) ** !empty$country)){ $location .= ', '. $country; } echo $location; ?>
$location = array(); if(!empty($_post['country'])) $location['country'] = $_post['country']; if(!empty($_post['state'])) $location['state'] = $_post['state']; if(!empty($_post['city'])) $location['city'] = $_post['city']; $location = implode(', ', $location);
security precautions
1. if using generate database queries, please @ to the lowest degree utilize mysql_real_escape_string()
(e.g. mysql_real_escape_string($_post['country'])
), unless using parametriezed queries (e.g. pdo or mysqli).
2. if outputting string user utilize htmlentities()
(e.g. htmlentities($_post['country'])
).
php
Comments
Post a Comment