I'll save you the trouble.
I use this in my CMS which is floating around on the MyBB site, MyBB ideas, new MyBB Mods site, and all of my client sites.
This function is directly from the CMS module which builds the friendly page URLs, checking for duplicates, appending numbers etc.
Modify as needed:
code:
function build_friendly_title($title, $page_id=0, $parent_id=0)
{
global $db;
$friendly_title = strtolower($title);
$friendly_title = preg_replace("#\s#", "-", $friendly_title);
$friendly_title = preg_replace("#([^a-zA-Z0-9-_])#", "", $friendly_title);
$test_title = $friendly_title;
$i = 1;
if($page_id > 0)
{
$page_id_add = " AND page_id != '$page_id'";
}
do
{
$query = $db->query("SELECT friendly_title FROM pages WHERE friendly_title='$test_title' $page_id_add AND parent_id='".intval($parent_id)."' ORDER BY page_id ASC LIMIT ".($i-1).", 1");
$existing_page = $db->fetch_array($query);
if($existing_page['friendly_title'])
{
$i++;
$test_title = $friendly_title."-".$i;
}
}
while($existing_page['friendly_title']);
return $test_title;
}
You can, however, also define functions in MySQL - but they won't work with PHP callbacks etc.