Let’s say i have:
$string = '2Pac - Dear Mama';
and the db like this:
id - some number
artist_name - 2Pac
alternative_name - Tupac
artist_slug - 2pac
How to transform the string to something like:
<a href="//example.com/artist/2pac/">2Pac</a> - Dear Mama
even if the $string would be:
$string = 'Tupac - Dear Mama';
transform it to:
<a href="//example.com/artist/2pac/">Tupac</a> - Dear Mama
Can somebody help me? Thanks in advance.
// later edit
I already use this function but i was wondering how to check if the artist has more names and keep the same page
function artist_name($title){ global $site_url;
$words = explode("-", $title); $result = array();
foreach ($words as $word){
$word = trim($word);
$res = mysql_query("SELECT `artist_name` FROM `artists` WHERE `artist_name` = '" . mysql_real_escape_string($word) . "' LIMIT 0, 1;");
if (mysql_num_rows($res) > 0){$result[] = '<a href="'.$site_url.'artist/'.clean_slug_function($word).'/" title="'.$word.'">'.$word.'</a>';}
else{$result[] = $word;}}
$result = implode($result, " - ");
return $result;}
// i’ve done it like this:
function artist_name($title){ global $site_url;
$words = explode("-", $title); $result = array();
foreach ($words as $word){
$word = trim($word);
$sql = dbquery("SELECT `artist_slug` FROM `artists` WHERE `artist_name` = '".mysql_real_escape_string($word)."' OR `alternative_name` = '".mysql_real_escape_string($word)."' LIMIT 0, 1;");
while($row = dbarray($sql)){$artist_slug = $row['artist_slug'];}
if (mysql_num_rows($sql) > 0){$result[] = '<a href="'.$site_url.'artist/'.clean_slug_function($artist_slug).'/" title="'.$word.'">'.$word.'</a>';}
else{$result[] = $word;}}
$result = implode($result, " - ");
return $result;}
Example:
for
$title = 'Tupac - Dear Mama';
echo artist_name($title);
the result will be
<a href="http://example.com/artist/2pac/" title="Tupac">Tupac</a> - Dear Mama
*notice 2pac url slug vs. Tupac (not 2Pac) name
1.
” – ” can be used as delimiter to divide the string in 2 parts. Contents of the string are put into an array.
More on explode function – on php.net
2.
I assume you have 2 tables, one with artists, and one with songs.
artists:
songs:
Code:
3.