开发者

how to generate unique title like wordpress?

开发者 https://www.devze.com 2022-12-29 08:00 出处:网络
hye im maya, i need to generate unique title like wordpress. if title hello-world 开发者_如何学运维is exist,the next title will be hello-world-2

hye im maya, i need to generate unique title like wordpress. if title hello-world 开发者_如何学运维is exist,the next title will be hello-world-2

thanks


  1. Check the server to see if that title already exists

  2. If it does, append a -2 and go to step 1. If you have already appended a -2 and the title already exists, replace it with a -3, and so on.

  3. If it doesn't, insert the file.

Example for step 1:

MySQL:

SELECT `title` FROM `table` WHERE `title` = '{$title}';

File (PHP):

$exists = file_exists("path/to/file");

Example for Step 2:

$iteration = 2;
while($exists){ 

      $newname = $file . '-' . $iteration;
      $iteration++;
      // Check to see if the file exists here

}
else{
    // Add the file here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消