我有2个PHP函数,一个用于生成5个字符的随机字符串,另一个用于检查MySQL数据库中是否存在随机字符串。现在我想运行函数ShortURL()
,直到函数CheckShortURL()
返回FALSE,然后继续执行脚本。非常感谢。
function ShortURL() {
$Length = "5";
$RandomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $Length);
return $RandomString;
}
function CheckShortURL($RandomString) {
try {
$SQL = "SELECT ShortURL FROM Images WHERE ShortURL = :ShortURL";
$SQL = $CONN->prepare($SQL);
$SQL->execute(array("ShortURL" => $ShortURL));
$CountShortURL = $SQL->rowCount();
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
exit();
}
if($CountShortURL == "1") {
return TRUE;
} else {
return FALSE;
}
}
更新:
我可以使用此代码执行while
循环,直到我获得唯一的5个字符sting:
$ShortURL = ShortURL();
while(CheckShortURL($ShortURL, $CONN) == TRUE) {
$ShortURL = ShortURL();
}
更新2:
$ShortURL_Found = "0";
while(!$ShortURL_Found) {
$ShortURL = ShortURL();
if(CheckShortURL($ShortURL, $CONN) == FALSE) {
$ShortURL_Found = "1";
}
}
答案 0 :(得分:0)
快速伪代码循环示例:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let delete = ....
let edit = UITableViewRowAction(style: .Normal, title: "Edit") { action, index in
//OPEN UIPresentationController HERE
}
return [delete, edit]
}