【问题标题】:how to generate unique title like wordpress?如何生成像wordpress这样的独特标题?
【发布时间】:2011-02-17 01:39:04
【问题描述】:

嘿,我是玛雅人, 我需要生成像 wordpress 这样的独特标题。 如果标题 hello-world 存在,下一个标题将是 hello-world-2

谢谢

【问题讨论】:

  • 这看起来很简单,你在找什么语言?您所要做的就是检查该名称是否存在于您的数据库中,如果确实检查末尾是否有数字,是否将数字加一并使其成为您的标题,否则将“ - 2”添加到标题。
  • 噗,为什么要使用有意义的标题?只需添加一天中的时间并使用没有冲突的完美摘要函数并使用它。 :-P

标签: php wordpress stub


【解决方案1】:
  1. 检查服务器以查看该标题是否已存在

  2. 如果是,请附加 -2 并转到步骤 1。如果您已经附加了 -2 并且标题已经存在,请将其替换为 -3,依此类推。

  3. 如果没有,请插入文件。

第 1 步的示例:

MySQL:

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

文件(PHP):

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

步骤 2 的示例:

$iteration = 2;
while($exists){ 

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

}
else{
    // Add the file here
}

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2011-09-09
    • 2019-11-17
    相关资源
    最近更新 更多