#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    159
    Rep Power
    31

    Question Alternative to nesting replaces


    Code:
    mysql_query("SELECT name, replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(name,' ','-'),'&','and'),'&','and'),',',''),'.',''),'?',''),';',''),'/','or'),'\'',''),'\\\\','') as urlfriendlytitle FROM tblknowledgebasecats WHERE id = '" . $cats['id'] ."'");
    Please tell me there is a better way to do this than nesting replaces.
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,178
    Rep Power
    4274
    Originally Posted by Nullified
    Please tell me there is a better way to do this than nesting replaces.
    can't, because there isn't

    at least, not with sql

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,785
    Rep Power
    348
    Originally Posted by Nullified
    Please tell me there is a better way to do this than nesting replaces.
    You could nest the SQL instead:

    Code:
    select name,
           replace(urlfriendlytitle,',','') as urlfriendlytitle
    from (
        select name, 
               replace(urlfriendlytitle,'&','and') as urlfriendlytitle
        from (
            select name,
                   replace(urlfriendlytitle, '&','and') as urlfriendlytitle
            from (
                select name, 
                       replace(name,' ','-') as urlfriendlytitle
                from tblknowledgebasecats
                where id = 42
            ) t1
        ) t2
    ) t3
    But that doesn't really make it better.

    The best way to do it, is to write a function that will do all the replacements and then call that function whenever you need a friendlyurl in your SQL queries. That way you also have the logic to generate a friendlyurl in only a single piece of code.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    159
    Rep Power
    31

IMN logo majestic logo threadwatch logo seochat tools logo