php - Codeigniter - check to see if the value is unique -
php - Codeigniter - check to see if the value is unique -
i trying check if value unique. have function, how can check again.
this function (it working):
public function is_unique($field, $val) { $this->db->select($field)->where($field, $val); $result = $this->get(); $result = $result['data']; homecoming $bool = count($result) == 0 ? $val : false; }
this check value see if unique:
$unique_code = random_string('alnum', 6); $unique_code = $this->posts->is_unuque('unique_code', $unique_code) != false ? $unique_code: random_string('alnum', 6);
how can create new value if function returned false , check new value (make loop until value unique)?
try this;
function is_unique($field, $val = null) { if ( is_null($val)) { // no $val supplied, generare new one. $val = random_string('alnum', 6); } $this->db->select($field)->where($field, $val); $result = $this->db->get('table'); if ( $result->num_rows() > 0 ) { // found match // seek again.... $this->is_unique($field); } else { // no match homecoming $val; } }
if don't supply $val, function generate new random string, check unique. if it's not, tries again.
php codeigniter codeigniter-2
Comments
Post a Comment