php - jQuery tag suggestion

468

I'm try include this plugin on my site, it's working fine but I am having some problems with PHP.

When I write the PHP like this:

$default_tags = 'Avanture, Giorgio, Armani, Depeche, Mode, Pevanje, Francuska, usluživanje, Pravo, Menadžer, prodaje, Advokat'; 

        if ([email protected]$_SESSION['existing_tags']) {
            $_SESSION['existing_tags'] = $default_tags;
        }

        $existing_tags = $_SESSION['existing_tags'];
        $tags = split(' ', $default_tags);

         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && @$_GET['tag']) {
            $match = array();
            foreach ($tags as $tag) {
                if (stripos($tag, $_GET['tag']) === 0) {
                    $match[] = $tag;
                }
           }
        echo json_encode($match);

            }
        exit;
} 

it works fine, but when I try to get a result from the database I have problems.

I have tried:

$query = mysql_query("SELECT * FROM  tags");
while($row = mysql_fetch_array($query)) {
$default_tags = ''.$row['keyz'].', '; 

if ([email protected]$_SESSION['existing_tags']) {
        $_SESSION['existing_tags'] = $default_tags;
    }

    $existing_tags = $_SESSION['existing_tags'];
    $tags = split(' ', $default_tags);

     if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && @$_GET['tag']) {
        $match = array();
        foreach ($tags as $tag) {
            if (stripos($tag, $_GET['tag']) === 0) {
                $match[] = $tag;
            }
       }
    echo json_encode($match);

        }
    exit;
} 

And this method is not working for me. Also, here is a screenshot from my database table tags. What is wrong with the above code?

230

Answer

Solution:

Your problem is you keep overriding $default_tags variable over and over again. At the end of your while loop, all you have is your last row with a comma at the end.

Basically you're not storing anything but the last row in that variable.

If you do the following you would have something similar what you're trying to do:

$default_tags_arr = array();
while($row = mysql_fetch_array($query)) {
    array_push($default_tags_arr, $row["keyz"]);
}

$default_tags = join(", ",$default_tags_arr);
912

Answer

Solution:

You should describe exactly what the problem is. Do you get an error message?

One thing I'm seeing that seems wrong to me is the fetch from the database:

while($row = mysql_fetch_array($query)) { 
$default_tags = ''.$row['keyz'].', ';  

For every row you are overriding$default_tags completely. I think maybe you wanted to say:

while($row = mysql_fetch_array($query)) //where does the curly brace closes in the original code?
    $default_tags .= ''.$row['keyz'].', ';  //notice the . before =

to concatenate the tags.

Aside from this I'm having trouble understanding the code:

$existing_tags = $_SESSION['existing_tags'];           
$tags = split(' ', $default_tags);    

Here you are assigning the variable$existing_tags which you don't use later in the code. Did you want to use it in the next line instead of$default_tags? What is the code supposed to do exactly?

People are also looking for solutions to the problem: Odbc with php, c#, preg_match is this code possible

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.