SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String Manipualtion + MySQL ID's

    Hi all,

    I am trying to get unique titles in article posts in a system. Whilst I've been developing it, I know that people may re-use article titles, so I'm trying to develop a way around it. At the minute, I'm down to this:
    PHP Code:
    $title_lower strtolower($title);
    $title_sanitized preg_replace("/[^a-zA-Z0-9[:blank:]s]/"""$title_lower);
    $title_exploded_url explode(" "$title_sanitized);
    $url_name implode("-"$title_exploded_url); 
    But, what happens if something with the same url_name exists? What is the most efficent way of tagging a number onto the end? It could loop through the strings, but, would it be better to put the next available ID at the end of the title? This raises another question, how can I find out what the next ID is actually going to be for MySQL with PHP? If the record was deleted from the db, the latest record + 1 will not give me the answer?

    Any ideas?

    Thanks in advance,

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't try to pedict what the next auto increment id is or you will have multiple other problems to overcome. Just create the record, and then retrieve the id for what you just created. For example, using mysql_insert_id(). You can always insert it incomplete and then later update it if needed.

    All urls could then just have the id as part of them, making every url guaranteed unique. A lot of websites use this approach. If desired, you could safely omit the id from the url if it's the first time the title is being used. You just need to check if the title is duplicated when generating a url, and if so, the url gets generated with the id to avoid ambiguity.

    Some websites use the date to avoid ambiguity, but this assumes the same title won't occur multiple times on a single date.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,217
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    the simplest approach is not allow two articles with the same title

    after that, anything you do is going to look like an ugly hack

    appending a number is one of the ugliest

    just my opinion

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •