php - WHILE statement which requires multiple lines of code for conditional -
the next receives proposed username, , returns username if not used, or username appended smallest integer if used. works fine, can't help wondering if done single method. what's got me stumped while statement requires both $stmt->execute(...)
, return $stmt->fetchcolumn()
conditional. guess anonymous function work, don't think allow prepared statement used each call. thinking loop might work, don't have much experience them , reading http://www.php.net/manual/en/control-structures.do.while.php implies should "use goto operator instead of hack." please provide recommended way of doing this. thanks
<?php class myclass { public function createusername($name) { $i=''; while($this->openusername($name.$i)==false){$i=$i+1;} homecoming $name.$i; } private function openusername($name) { $sql='select id users username=?'; seek { $stmt = db::db()->prepare($sql); $stmt->execute(array($name)); homecoming $stmt->fetchcolumn()?false:true; } catch(pdoexception $e){die(library::sql_error($e,$sql));} } } ?>
personally i'd this:
function createusername($name) { // validate $name, instance restrict alphanumeric characters preg_match $sql = "select greatest(`username`) `out` `users` `username` regexp '^".$name."\d*$'"; $result = // whatever code takes pdo run above query if( !$result) homecoming $name; // nobody name yet $number = preg_replace("^.*(\d*)$","$1",$result['out']); if( !$number) homecoming $name."1"; homecoming $name.($number+1); }
php
No comments:
Post a Comment