0

Let's say i have:

$title = "Rihanna - Man Down";

and a database multiple rows including one with a field called "name" with the "Rihanna" value

now... how can i check if in $title exists "Rihanna" and if it does how to add a link on "Rihanna" like this:

with other words check if one of the expresions from $title exists the database

  • i think this second formulation it is better
<a href="artist?id=Rihanna">Rihanna</a> - Man Down

I want to do something like youtube does at some of it;s music videos to be more clear

1
  • 3
    Please rephrase your question it's a bit tough to understand. Commented Nov 24, 2011 at 11:14

3 Answers 3

3

If I understood correct, this can do the work:

Then first search in DataBase, your query must be something like this:

//Search for word in DataBase on filed "name"

$title = "Rihanna - Man Down";
$arr = explode('-', $title); 
$title = $arr[0];

$result = mysql_query("SELECT name FROM table WHERE name LIKE '%" . mysql_real_escape_string($title) . "%'");

//If find results, then we replace and generate links in this loop

while($row = mysql_fetch_array( $result )) {
    $str = str_replace($title, '<a href="artist?id='.$title.'">'.$title.'</a>', $row['name']);
    echo $str; //<a href="artist?id=Rihanna">Rihanna</a> - Man Down
}
Sign up to request clarification or add additional context in comments.

6 Comments

thank you, this works but that was just an example... i need to check if a word from $title exists in the dataabse and put a link on that word if it exists. please think at the youtube example.
Look at modified answer, try this.
thanks, this really works ; but it works when $title = "Rihanna"; ; when $title = "Rihanna - Man Down"; (or with other words, when it contains something more than the artist name it doesn't work)
If your Artist is always first word of $title, then you can use this: $arr = explode(' ', $title); $title = $arr[0];
Or this is better: $arr = explode('-', $title); $title = $arr[0];
|
1
$words = explode("-", $title);
$result = array();

foreach ($words as $word)
{
    $word = trim($word);
    $res = mysql_query("SELECT name FROM table WHERE name='" . mysql_real_escape_string($word) . "'");

    if (mysql_num_rows($res) > 0)
    {
        $result[] = '<a href="artist?id=' . $word . '">' . $word . '</a>';
    }
    else
    {
        $result[] = $word;
    }
}

$result = implode($result, " - ");

This will however also link to "artist?id=Pink" if a song from another artist is named Pink, but you have the artist Pink in your database.

6 Comments

i've tried this but i have this error: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given" on the line with if (mysql_num_rows($res) > 0)
@m3tsys then start debugging. Probably there's an error in the query. Enable error reporting, print the query.
error reporting is on: i get the same error ; when i echo $result i get - sign
@m3tsys I accidentally swapped the parameters for explode(). Should work now.
thanks a lot! that worked... now i have a question... Let's say $title = "justin bieber will be father"; is there posible to do the same thing ? i mean to work with or without that - sign?
|
1

Split your title into words, and for each word check if your database contains row with that name. If yes, replace occurences of the word with link.

foreach (preg_split("/\s+/", $title) as $word) {
    if (word_in_database($word)) {
        $title = str_replace($word, "<a href=\"artist?id=$word\">$word</a>", $title);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.