1

我无法从数据库中获取我的值以进入 jquery 标签插件,任何帮助都会很棒:

PHP:

case 'get_tags':
        $id         = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
        $query      = "SELECT description FROM `tags` WHERE page_id = '?';";
        $query_p    = $db->prep_query($query,array($id));
        $results    = $db->queryMakearray($query_p);

        $tags = array_values($results);
        $response   = json_encode($results);
break;

JS

$(document).ready(function()
{
    var _id = parseInt($.url_param('id'), 10);
    var _tag_items = $.get('inc/ajax.php?type=get_tags&id='+_id);
    $('#tags')
        .textext({
        plugins : 'tags',
        tagsItems: _tag_items
    });
});

回复:

"[{\"description\":\"test\"},{\"description\":\"test2\"},{\"description\":\"test3\"}]"

我需要它看起来像这样:

["test","test2","test3"]

编辑

PHP:

$id         = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
        $query      = "SELECT description FROM `tags` WHERE page_id = '?';";
        $query_p    = $db->prep_query($query,array($id));
        $results    = $db->query2array($query_p);

        $tags   = array_values($results);
        $response = sanitize_output($tags);
4

3 回答 3

0

您必须将 JSON (_tag_items) 转换为数组。你可以这样做:

var finalArray = [];
for(var i in _tag_items) {
    if(_tag_items.hasOwnProperty(i) && !isNaN(+i)) {
        finalArray[+i] = _tag_items[i];
    }
}
于 2012-11-03T18:38:06.707 回答
0

您可以通过删除密钥在php中解决它。

    foreach($results => $value);
    {
        $results[] = $value['description'];
    }
于 2012-11-03T18:39:46.243 回答
0
$(document).ready(function()
{
    var _id = parseInt($.url_param('id'), 10);
    var _tag_items = $.get('inc/ajax.php?type=get_tags&id='+_id);
    var _newTag = [];
    for(var i=0; i<_tag_items.length; i++)
    {
        _newTag.push(_tag_items[i].description);
    }
    $('#tags')
        .textext({
        plugins : 'tags',
        tagsItems: _newTag
    });
});

演示

于 2012-11-03T18:50:13.167 回答