views:

56

answers:

1

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  A: 
  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
}
waiwai933