How to assign tags for Tag Handler by ioncache from the database

378 Views Asked by At

I'm using this plugin called TagHandler. Link: http://ioncache.github.io/Tag-Handler/ May I know how can I assign tags from the database and not hard code to jquery? Example, to assign tags is

$("#array_tag_handler").tagHandler({
    assignedTags: [ 'C', 'Perl', 'PHP' ],
    availableTags: [ 'C', 'C++', 'C#', 'Java', 'Perl', 'PHP', 'Python' ],
    autocomplete: true
});

But I want it from mysql database.

They only gave example for available tags which is using the getData buildin function

$("#ajaxget_tag_handler").tagHandler({
    getData: { id: 'user123', type: 'user' },
    getURL: '/ajaxtest/get',
    autocomplete: true
});

I need the php example. I don't know how to retrieve data in JSON format.

From the website..."By supplying a "getURL" for the tags to be retrieved via AJAX. When using this method, the server must supply a JSON formatted array named "availableTags" and optionally an additional array named "assignedTags"."

1

There are 1 best solutions below

0
On BEST ANSWER

On the clientside you want to load the tag handler like so:

$(document).ready(function()
{
    $("#array_tag_handler").tagHandler({
        dataType: 'json',
        getURL: '/admin/tag/list',
        autocomplete: true
    });

});

This calls the '/admin/tag/list' route and expects json back. On the server side you want to retrieve the list of tags and pass them back in json format.

$result = getTags();  // Returns eg array('tag1', 'tag2', 'tag3', etc)

Then build your array with the correct indices according to the Tag Handler documentation:

$data = array('availableTags' => $result);

Note that if you want to preload some tags (eg tag1 and tag2) then just modify the array above so that it looks like this:

$data = array('availableTags' => $result, 'assignedTags' => array('tag1', 'tag2'));

Then you need to json encode this array before returning it to the client:

return json_encode($data);