如何将两个php json文件集成到一个[autocomplete]?
我有两个json文件,我想把两个json文件合并成一个 Php如何将两个php json文件集成到一个[autocomplete]?,php,jquery,html,json,Php,Jquery,Html,Json,我有两个json文件,我想把两个json文件合并成一个 Php <input type="text" name="image" /> <input type="text" name="file" /> 第二次输入的json $('input[name=\'image\']').autocomplete({ minLength: 2, source: function( request, response ) { $.ge
<input type="text" name="image" />
<input type="text" name="file" />
第二次输入的json
$('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 );
};
},
});
$('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 );
};
},
});
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);
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文件集成到一个文件中?解决方案如下:
HTML:
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 );
}
};
函数拆分(val){
返回val.split(/,\s*/);
}
功能提取最后(学期){
返回拆分(term.pop();
}
$(“输入[name='image'],输入[name='file'])。自动完成({
最小长度:2,
来源:功能(请求、响应){
var thisType=this.element[0]。名称;
$.getJSON(“/test”{
期限:最后一个(请求期限),
类型:此类型
},回应);
}
}).autocomplete(“实例”)。\u renderItem=函数(ul,项){
var thisType=this.element[0]。名称;
如果(thisType=='image'){
返回$(“”)
.data(“item.autocomplete”,item)
.append(“”+item.label+“”)
.附录(ul);
}否则{
返回$(“”)
.data(“item.autocomplete”,item)
.append(“”+item.label+“”)
.附录(ul);
}
};
PHP:
将它们解码为PHP数组,然后进行合并,然后将它们编码回JSON。如何?我不明白,请给出一个代码示例。我会对其进行测试并让您知道。好的,但是当我添加$type=strtolower($\u GET[“type”])时,您能解释一下吗
和如果($type='image'){
则自动完成停止工作。您使用的是什么库/扩展,我收集了您的使用jQuery的用户界面,可能还有用于自动完成的jQuery用户界面。我已经开始工作了,检查PHP和javascript是否相同,我的示例是在Thank you bro。您太棒了。再次感谢:))
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
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);