发布于 2024-11-15 17:49:27 字数 2977 浏览 2 评论 0 原文

I'm really sorry if this is too basic, but I really don't know how to do this.

I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

EDIT: This is the jquery code i use for the autocomplete:

$(function() {
        var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
                minLength: 2,
                queryRemote: true,
                remote: {url: 'autocomplete2.php'}
            }}});

The plugin uses a PHP for autocomplete, this is an example, it returns this output: "id, text, null (html I don't need), some html"

$response = array();
            $names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'Etc');

            // make sure they're sorted alphabetically, for binary search tests
            sort($names);

            $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

            foreach ($names as $i => $name)
            {
                if (!preg_match("/^$search/i", $name)) continue;
                $filename = str_replace(' ', '', strtolower($name));
                $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
            }

            header('Content-type: application/json');
            echo json_encode($response);

I need a similar PHP to process this results: http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

...being "beatles" the $search value, and getting this output:

guid,"name",null,"name<span>n:type name</span>"

So, the first result would be:

0,"The Beatles",null,"The Beatles<span>Band</span>"

Of course I would need to query freebase.com from that PHP. I mean:

        +---------------+         +-----------+        +------------+
        |               |         |           |        |            |
        |  TextboxList  +-------->|   PHP     +------->|  Freebase  |
        |               |         |           |        |            |
        +---------------+         +-----------+        +------+-----+
                                                              |
             JSON                     JSON                    |
          TextboxList   <--------+  freebase       <----------+

Is this possible?谢谢!

I'm really sorry if this is too basic, but I really don't know how to do this.

I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

EDIT: This is the jquery code i use for the autocomplete:

$(function() {
        var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
                minLength: 2,
                queryRemote: true,
                remote: {url: 'autocomplete2.php'}
            }}});

The plugin uses a PHP for autocomplete, this is an example, it returns this output: "id, text, null (html I don't need), some html"

$response = array();
            $names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'Etc');

            // make sure they're sorted alphabetically, for binary search tests
            sort($names);

            $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

            foreach ($names as $i => $name)
            {
                if (!preg_match("/^$search/i", $name)) continue;
                $filename = str_replace(' ', '', strtolower($name));
                $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
            }

            header('Content-type: application/json');
            echo json_encode($response);

I need a similar PHP to process this results: http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

...being "beatles" the $search value, and getting this output:

guid,"name",null,"name<span>n:type name</span>"

So, the first result would be:

0,"The Beatles",null,"The Beatles<span>Band</span>"

Of course I would need to query freebase.com from that PHP. I mean:

        +---------------+         +-----------+        +------------+
        |               |         |           |        |            |
        |  TextboxList  +-------->|   PHP     +------->|  Freebase  |
        |               |         |           |        |            |
        +---------------+         +-----------+        +------+-----+
                                                              |
             JSON                     JSON                    |
          TextboxList   <--------+  freebase       <----------+

Is this possible? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-11-22 17:49:27

试试这个:

$response = array();

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

$myJSON = file_get_contents('http://www.freebase.com/private/suggest?prefix=' . urlencode($search));

$musicObj = json_decode($myJSON); // Need to get $myJSON from somewhere like file_get_contents()

foreach ($musicObj->result as $item)
{
    $response[] = array($item->guid, $item->name, null, $item->name . '<span>'.$item->{'n:type'}->name.'</span>');
}

header('Content-type: application/json');
echo json_encode($response);

第一个 JSON 转义结果给出:

["#9202a8c04000641f800000000003ac10","The Beatles",null,"The Beatles<span>Band<\/span>"]

但是尽管如此,您实际上根本不需要使用 PHP 来执行此操作。您可以通过 JavaScript 完成这一切,并避免额外访问您的服务器。如果您向 freebase 提供回调参数,它可以创建 JSONP(这是使用您选择的函数名称包装在函数调用中的 JSON),您可以在 jQuery 中获取该 JSONP,然后进行进一步操作根据您的喜好在 JavaScript 中。但以上内容是按照您使用 PHP 的原始方法进行的。

Try this:

$response = array();

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

$myJSON = file_get_contents('http://www.freebase.com/private/suggest?prefix=' . urlencode($search));

$musicObj = json_decode($myJSON); // Need to get $myJSON from somewhere like file_get_contents()

foreach ($musicObj->result as $item)
{
    $response[] = array($item->guid, $item->name, null, $item->name . '<span>'.$item->{'n:type'}->name.'</span>');
}

header('Content-type: application/json');
echo json_encode($response);

The first JSON-escaped result then gives:

["#9202a8c04000641f800000000003ac10","The Beatles",null,"The Beatles<span>Band<\/span>"]

But despite all this, you really don't need to use PHP at all to do this. You can do this all from JavaScript and avoid an extra trip to your server. If you supply the callback argument to freebase, it can create JSONP (which is JSON wrapped in a call to a function, using a function name of your choice) which you can obtain in jQuery and then manipulate further in JavaScript to your liking. But the above is per your original approach in using PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文