How to integrate two php json files into one [autocomplete]?

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

Php

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

      

Javascript for first input

$('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 for second input

$('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 for first input

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 for second login

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);

      

How to bring everything into one? There is a problem with json beacause, the first input only shows the name of the preview image file, and the second only shows the name of the text files, then how to integrate the json files into one?

+3


source to share


1 answer


Here's the solution:

HTML:

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

      

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);

      

+2


source







All Articles