Frage

I'm trying to get the tag-it jquery plugin to work with a json string. Currenttly i'm getting my values from the database like this:

$query = sprintf(
            'SELECT
                t.tag
            FROM
                tags AS t
            ');

    $row_set= array();

    if($result = mysqli_query($db, $query))
    {
        // fetch data
        while ($row = mysqli_fetch_assoc($result))
        {
            $row_set[] = $row;
        }

        // set the output
        echo json_encode($row_set);
    }

which gives the following output when called in AJAX:

[{"tag":"test"},{"tag":"tests"}]

But I have to output a JSON string in the following format:

["android-intent","animate","architecture","artificial-intelligence","attributes"]

How can i achieve this?

War es hilfreich?

Lösung

When you use row_set[] = $row; most likely $row looks like this $row['tag'] = 'test that is why you are having JSON object format;

Try

  $row_set[] = $row['tag']; 

Make sure you also set the seound parameter to true to make JSON always return array

   json_encode($row_set,true);

This would return the it as array not as a object

Andere Tipps

That should fix it, by adding string into you arrow instead of adding arrays into you array.

$row_set[] = $row[0]; or $row_set[] = $row['tag'];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top