如何将两个php json文件集成到一个[自动完成]中? [英] How to integrate two php json file into one [autocomplete]?

查看:107
本文介绍了如何将两个php json文件集成到一个[自动完成]中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个json文件,我想将两个JSON文件合并为一个.

I have two json files and i want to merge two JSON files into one.

Php

<input type="text" name="image" />
<input type="text" name="file" />

首次输入的Java脚本

$('input[name=\'image\']').autocomplete({ 
      minLength: 2,
      source: function( request, response ) {
            $.getJSON( "files/", {
                term: extractLast( request.term )
                }, response );
                },
       create: function () {
            $(this).data("autocomplete")._renderItem = function(ul, item) {
               return $("<li></li>")
            .data("item.autocomplete", item)
            .append('<a><img src="files/' + item.label + '.jpg' />
                     ' + item.label + '</c></a>')
            .appendTo( ul );
                  };
            },

});

用于第二次输入的JavaScript

$('input[name=\'file\']').autocomplete({ 
      minLength: 2,
      source: function( request, response ) {
            $.getJSON( "files/", {
                term: extractLast( request.term )
                }, response );
                },
        create: function () {
            $(this).data("autocomplete")._renderItem = function(ul, item) {
               return $("<li></li>")
            .data("item.autocomplete", item)
            .append('<a>' + item.label + '</c></a>')
            .appendTo( ul );
                  };
            },

});

用于首次输入的json

if (empty($_GET['term'])) exit ; 
   $q = strtolower($_GET["term"]);
   if (get_magic_quotes_gpc()) $q = stripslashes($q);
      $files = array(); 
   foreach(glob('image/*.jpg*', GLOB_BRACE) as $key=>$file) { 
     $files[] = substr($file, 0, -4); }
      $files = array_unique($files); 
    $files = array_combine($files, $files);
      $result = array(); 
foreach ($files as $key=>$value) { 
    if (strpos(strtolower($key), $q) !== false) { 
        array_push($result, 
        array("id"=>$value, "label"=>$key, 
            "value" => strip_tags($key))); }
      if (count($result) > 11) break; } 
 echo json_encode($result);

用于第二次输入的json

if (empty($_GET['term'])) exit ; 
   $q = strtolower($_GET["term"]);
   if (get_magic_quotes_gpc()) $q = stripslashes($q);
      $files = array(); 
   foreach(glob('image/*.txt*', GLOB_BRACE) as $key=>$file) { 
     $files[] = substr($file, 0, -4); }
      $files = array_unique($files); 
    $files = array_combine($files, $files);
      $result = array(); 
foreach ($files as $key=>$value) { 
    if (strpos(strtolower($key), $q) !== false) { 
        array_push($result, 
        array("id"=>$value, "label"=>$key, 
            "value" => strip_tags($key))); }
      if (count($result) > 11) break; } 
 echo json_encode($result);

如何将所有元素融合为一体? json存在问题,因为首先输入仅显示带有预览的图像文件名,然后第二个仅显示文本文件名,然后如何将json文件集成到其中?

How to mearge all into one? There has a problem with json beacause first input only show image files name with preview and second one only show text files name then how to integrate json files into one?

推荐答案

此处为解决方案:

HTML:

<input type="text" name="image" />
<input type="text" name="file" />

JavaScript:

Javascript:

function split( val ) {
    return val.split( /,\s*/ );
}
function extractLast( term ) {
    return split( term ).pop();
}
$("input[name='image'], input[name='file']").autocomplete({
    minLength: 2,
    source: function(request, response) {
        var thisType = this.element[0].name;
        $.getJSON("/test", {
            term: extractLast(request.term),
            type: thisType
        }, response );
    }
}).autocomplete( "instance" )._renderItem = function( ul, item ) {
    var thisType = this.element[0].name;
    if (thisType == 'image') {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append('<a><img src="files/' + item.label + '.jpg" />' + item.label + '</a>')
            .appendTo( ul );
    } else {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append('<a>' + item.label + '</a>')
            .appendTo( ul );
    }
};

PHP:

<?php
if (empty($_GET['term'])) exit;

$q = strtolower($_GET["term"]);

$type = strtolower($_GET["type"]);

if (get_magic_quotes_gpc()) $q = stripslashes($q);

$files = array();

if ($type == 'image') {
    foreach(glob('image/*.jpg*', GLOB_BRACE) as $key=>$file) {
        $files[] = substr($file, 0, -4);
    }
} else {
    foreach(glob('image/*.txt*', GLOB_BRACE) as $key=>$file) {
        $files[] = substr($file, 0, -4);
    }
}

$files = array_unique($files);
$files = array_combine($files, $files);

$result = array();
foreach ($files as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
    }
    if (count($result) > 11) break;
}

echo json_encode($result);

这篇关于如何将两个php json文件集成到一个[自动完成]中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆