开发者

Need to comma delimit json encoded query

I'm trying to add an autocomplete tokenizer script to some form fields and am having an issue with delimiting the values at the commas.

The way it is now if a person adds multiple values to the field the autocomplete suggestions will display them as one long value instead of each as an individual value. I first tried to explode the value before encoding it but it doesn't put it in the right format. Here's how it displays now:

[{"cb_activities":"Kicking Cats,"},{"cb_activities":"baseball,hockey,"}]

but I need it to display as:

[{"cb_activities":"Kicking Cats,"},{"cb_activities":"baseball,"},{"cb_activities":"hockey,"}]

Here's my php file:

<?
mysql_pconnect("localhost", "myuser", "mypass") or die("Could not connect");
mysql_select_db("mydb") or die("Could not select database");
$param = mysql_real_escape_string ($_GET["q"]);

$query = sprintf("SELECT DISTINCT cb_activities FROM jos_comprofi开发者_JS百科ler WHERE cb_activities REGEXP '^$param'");
$arr = array();
$rs = mysql_query($query);

while($obj = mysql_fetch_object($rs))
{
    $arr[] = $obj;
}

echo json_encode($arr);
?>

Here's the jquery script: http://www.erecoverydev.com/autocomplete2/js/jquery.tokeninput.js

I haven't done much with json so please be specific lol.


Unless im missing something you should be able to simply adjust your iteration:

$activities = array();
while($obj = mysql_fetch_object($rs))
{
    $activities[] = array('cb_activities' => $obj->cb_activities);
}

However, I would just make cb_activities be an array and then join or split the array as necessary on the js side. for example:

$activities = array();

while($obj = mysql_fetch_object($rs))
{
    $activities[] = $obj->cb_activities;
}

echo json_encode(array('cb_activities' => $activities));

Then in your js you could to:

// assume data is the json returned by the server

var activities = data.cb_activities.join(',');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜