duanmao7553 2012-10-14 22:09
浏览 20
已采纳

如何为jquery自动完成制作json代码? [关闭]

I need an json for jquery catcomplete and I would like you to help me...

I have this code

$( ".autocomplete" ).autocomplete({
     source: "files/news.php?listar=json"
});

And I would like "files/news.php?listar=json" to display something like this:

[ 
  { label: "title1", category: "category1" },
  { label: "title2", category: "category2" } 
]

Could you please help me?

@edit:

I tried this, but it didn't work:

if ($_GET['listar'] == 'json')
{
    echo '['."
";  
    $query = mysql_query("SELECT * FROM noticias WHERE deletada='0' ORDER BY id DESC");
    $a = 0;
    while ($noticia = mysql_fetch_array($query))
    {
        echo ' { label: "'.$noticia['titulo'].'", category: "'.ucwords(Categoria($noticia['categoria'])).'" }';
        $a++;

        if ($a < mysql_num_rows($query))
        {
            echo ",
";
        }
    }
    echo "
".']';

    exit;
}
  • 写回答

2条回答 默认 最新

  • doubeiji2602 2012-10-14 22:19
    关注

    You don't need writing own json, because it's already function for json encode(): http://php.net/manual/en/function.json-encode.php

    if ($_GET['listar'] == 'json')
    {
        $query = mysql_query("SELECT * FROM noticias WHERE deletada='0' ORDER BY id DESC");
        while ($noticia = mysql_fetch_array($query))
        {
        $results[] = array(
            'label' => $noticia['titulo'],
            'category' => $noticia['categoria']
        );
        }
    }
    
    $json = json_encode($results);
    header('Content-type: application/json');
    echo $json;
    exit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题