Dragan A.
Dragan A.

Reputation: 51

jQuery tag suggestion

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 (!@$_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 (!@$_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?

Upvotes: 0

Views: 199

Answers (2)

aKzenT
aKzenT

Reputation: 7895

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?

Upvotes: 0

Selçuk
Selçuk

Reputation: 176

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);

Upvotes: 1

Related Questions