Hi pain_is_relative,
I wish it could be that simple. But clients today demand SEO friendly URLs, and not URLs that simply work (and work well).
I figure I could ditch the loop, and change my code to something like this:
$safe_title = 'hello_world';
$results = $db->execute(
"SELECT * FROM `posts` WHERE `safe_title` REGEXP '^$safe_title(_[0-9]+)?$' LIMIT 1"
);
/**
* Check to see if we found another post with the same
* safe title
*/
if (count($results) > 0) {
preg_match('/^(.*?)(_([0-9
/**
* If the safe title already has a _[0-9] appended to
* it, we need to increment the [0-9] by one. Otherwise
* add _1 to the safe title.
*/
if (!empty($matches[2])) {
$safe_title = $matches[0] . '_' . ($matches[2] + 1);
} else {
$safe_title = $matches[0] . '_1';
}
}
$db->insert("INSERT INTO ....") // Insert post with safe title
That still doesn't solve the problem of another blog post with the same safe title being inserted microseconds before I call $db->insert(). Also I've never used regular expressions in a SELECT statement before, and I don't know what the performance would be like on large data sets.
Main Topics
Browse All Topics





by: pain_is_relativePosted on 2008-06-15 at 23:25:07ID: 21791106
you are painting yourself into a corner.
lets see...you could attach an md5 hash of the current date after the name.... ugly.
you could make the safe-title a unique primary key, but that could cause you problems... old posts getting over written, errors writing to table. etc.
Alternatively you could reference the page by its primary key ... (id), and let the posts share names. So your posts could be referebced by id=678, id=56789, id=3. that way you would not be limiting your posts to having unique names.