我有一个为页面标题创建唯一 slug 的功能。它检查页面表中是否有可用的 slug,然后通过相应地添加一个“-int”来创建一个唯一的 slug。该功能适用于前三个条目,例如,“test slug”输入三次将创建“test-slug-1”、“test-slug-2”和“test-slug-3”。然后,我收到第四个条目的错误“致命错误:超过 30 秒的最大执行时间”。逻辑应该有问题,谁能帮我找到它。下面是代码:
function createSlug($title, $table_name, $field_name) {
global $db_connect;
$slug = preg_replace("/-$/","",preg_replace('/[^a-z0-9]+/i', "-", strtolower($title)));
$counter = 1;
do{
$query = "SELECT * FROM $table_name WHERE $field_name = '".$slug."'";
$result = mysqli_query($db_connect, $query) or die(mysqli_error($db_connect));
if(mysqli_num_rows($result) > 0){
$count = strrchr($slug , "-");
$count = str_replace("-", "", $count);
if($count > 0){
$length = count($count) + 1;
$newSlug = str_replace(strrchr($slug , "-"), '',$slug);
$slug = $newSlug.'-'.$length;
$count++;
}else{
$slug = $slug.'-'.$counter;
}
}
$counter++;
$row = mysqli_fetch_assoc($result);
}while(mysqli_num_rows($result) > 0);
return $slug;
}