How to generate a SEF alias for a DB String Value

293 Views Asked by At

I have a DB table named games which lets just say for simplification contains the columns "id" and "gametitle" and has roughly 7,000 rows already populated.

What I would like to do is have a piece of SQL that would generate a SEF alias for the gametitle that I can use in the URL (I have URL Rewriting successfully running on the site).

The new field would be entered into a new column named "gamealias".

So for instance, if I had a gametitle of "Halo: Combat Evolved" it would drop any special characters and produce something similar to "halo-combat-evolved". All the aliases in the table would have to be unique as they are going to be used in the games url... e.g. http://thegamesdb.net/game/halo-combat-evolved/

Also, it would be nice if upon insertion of a new row, the alias could be generated automatically from SQL without having to handle it in PHP (maybe using the sql fields default value somehow), although I don't mind having to do this in PHP if it's not possible magically in SQL.

1

There are 1 best solutions below

2
On

I think this could be done in MySQL using a trigger and a regex-replace UDF, but it would probably be a simpler route just using PHP. You could easily guarantee gamealias uniqueness if you integrated the title's primary key in with the string. For example, consider something that would output "halo-combat-evolved-321".