views:

60

answers:

3

My application requires the user to enter their business name, which the application will automatically create into a unique identifier to be used in URLs, ie

"Bob's Cafe" will become "bobs-cafe"

But if there are duplicate names I would like the application to add a number so if there is already a "bobs-cafe" we will use "bobs-cafe-1" and likewise if there is already a "bobs-cafe-1" we will use "bobs-cafe-2"

Ive used explode and also looked at a regular expressions but I dont know the best way to approach this.

Im stuck in being able to grab the number and incrementing it and returning the string

+4  A: 

Adding to Sarfraz's answer, you might want to find it using a LIKE statement

SELECT `urlIdentifier` FROM `businesses` WHERE `urlIdentifier` LIKE `bobs-cafe%`

which will get all the bobs-cafe items - that way, if you get 5 rows you know you have

bobs-cafe
bobs-cafe-1
bobs-cafe-2
bobs-cafe-3
bobs-cafe-4

and that you'll need to add bobs-cafe-5

EDIT - Or this:

SELECT count(*)  as `howMany` FROM `businesses` WHERE `urlIdentifier` LIKE `bobs-cafe%`

Now your result object ( or array ) will have the total number:

echo $resultObject->howMany; // number of bobs-cafe sql found

Dan Heberden
throw in an ORDER BY DESC and limit 1 to get the max at once
Col. Shrapnel
Though that would require analyzing the string returned to grab the number. Adding on to my answer for another option..
Dan Heberden
Thanks for your responses. I think using the id and the identifier in the URL is a great idea.Further to the above though, that's not the issue, it's more how I determine from the string the current increment number then increasing it
Tim
@user see an update to my answer please
Col. Shrapnel
This is a good idea, but you should separate the actual business ID (bobs-cafe) from the number somehow. Your code would fail if someone tried to register "Bob's Cafe and Bakery" because "bobs-cafe-and-bakery" is `LIKE` "bobs-cafe%".
musicfreak
Good point musicfreak - using a different delimiter for the id like bobs-cafe::1 so you could do LIKE 'bobs-cafe::%' to specify it's the entire 'business-name'
Dan Heberden
A: 

Why not to add an autoincrement number to every identifier in the URL?
Just like SO does:

stackoverflow.com/questions/2895334/php-application-check-name-is-unique-if-not-append

so, you have both unique identifier and a business name.

This is even better because they are free to change their business name, without changing an identifier.

As for your question it's very simple. Just for the PHP practice:

if (/* you've found the name is already non unique and have the max one in the $id */) {
  $parts = explode("-",$id);
  if (isset($parts[1])) $newid = $parts[0]."-".($parts[1]+1);
  else $newid = $parts[0]."-1";
}
Col. Shrapnel
I love the voting :) That's the picture of the usual SO audience...
Col. Shrapnel
wtf with the voting. this is exactly what I needed. thanks.
Tim
+1 This is the best approach if this sort of URL structure is allowed. Otherwise, force the user to provide a unique business name, or cause the URL to be unique by putting the business's city and/or state in the URL as well.
Justin Johnson
Oh god, I hate how you do conditionals. :)
musicfreak
@user Though it won't work in your case, I just notice that you're using same delimiter for the name and for the number, it is better to have different, so it not to be mixed. Though it's just a example to the method.
Col. Shrapnel
+1  A: 

Assuming $user is already in the form bobs-cafe

function username_exists ( $user ) {

      $result = mysql_query("SELECT name FROM table WHERE $name LIKE '$user%' ");
      $count = mysql_num_rows($result);

        if ( $result ) {

            $num = $count+1;
            return username_exists ( $user.'-'.$num ) ;

        } else {

            return $user;
        }

}
aSeptik
See my comment on @Dan Heberden's solution above: This is a good idea, but you should separate the actual business ID (bobs-cafe) from the number somehow. Your code would fail if someone tried to register "Bob's Cafe and Bakery" because "bobs-cafe-and-bakery" is `LIKE` "bobs-cafe%".
musicfreak
i think the whole approch here is wrong, anyway the function assume you have only one separator! a good id can't be abstract-abstract-1 must be IMHO a number or at least something like firstname_lastname#
aSeptik