Perform Function on SQL Query Lookup? - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Perform Function on SQL Query Lookup? (/showthread.php?tid=76907)
Perform Function on SQL Query Lookup? by Dempsey on 08-21-2007 at 12:48 PM
Yea I know the title's confusing so I'll try and explain what I mean.
In the MPSounds.net DB the soundpack names are stored as 'The Simpsons Movie' etc, but I am now making the urls to pack like /soundpacks/the-simpsons-movie.
One solution would be to have a 2nd field in the table with the url friendly version of the name(the-simpsons-movie instead of The Simpsons Movie) but I'm just wondering if it's possible to do something along the lines of:
SELECT FROM blah WHERE MyFunction(title) = 'the-simpsons-movie'
where MyFunction does the lowercasing, removing non alphanumerics etc. Just wondering if this is possible at all, or do I need to add a new field?
Thanks for any replied
RE: Perform Function on SQL Query Lookup? by Veggie on 08-21-2007 at 01:41 PM
cant you just use php for the function? convert to lower case and string replace the spaces, then use the output in the SQL select query.
RE: Perform Function on SQL Query Lookup? by surfichris on 08-21-2007 at 01:42 PM
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.
|