Jump to content

Need to separate query result by comma


sfraise

Recommended Posts

I need to separate multiple field values with commas but I can't seem to get it to work, I've been trying to use .split(',') but maybe I can't do it this way.

 

Here's the part of the script I think this should go in:

    // Do the actual search
    function run_search(query) { 
        var cached_results = cache.get(query);
        if(cached_results) {
            populate_dropdown(query, cached_results);
        } else {
      var queryStringDelimiter = settings.url.indexOf("?") < 0 ? "?" : "&";
      var callback = function(results) {
        if($.isFunction(settings.onResult)) {
            results = settings.onResult.call(this, results);
        }
              cache.add(query, settings.jsonContainer ? results[settings.jsonContainer] : results);
              populate_dropdown(query, settings.jsonContainer ? results[settings.jsonContainer] : results);
            };

            if(settings.method == "POST") {
          $.post(settings.url, settings.queryParam + "=" + query, callback, settings.contentType);
        } else {
            $.get(settings.url + queryStringDelimiter + settings.queryParam + "=" + query, {}, callback, settings.contentType);
        }
        }
    }
};

// Really basic cache for the results
$.TokenList.Cache = function (options) {
    var settings = $.extend({
        max_size: 50
    }, options);

    var data = {};
    var size = 0;

    var flush = function () {
        data = {};
        size = 0;
    };

    this.add = function (query, results) {
        if(size > settings.max_size) {
            flush();
        }

        if(!data[query]) {
            size++;
        }

        data[query] = results;
    };

    this.get = function (query) {
        return data[query];
    };
};

The full script is at www.erecoverydev.com/autocomplete2/js/jquery.tokeninput.js

 

My php code is:

<?
mysql_pconnect("localhost", "myuser", "mypass") or die("Could not connect");
mysql_select_db("mydb") or die("Could not select database");
$param = mysql_real_escape_string ($_GET["q"]);

$query = sprintf("SELECT DISTINCT cb_activities FROM jos_comprofiler WHERE cb_activities REGEXP '^$param'");
$arr = array();
$rs = mysql_query($query);

while($obj = mysql_fetch_object($rs))
{
    $arr[] = $obj;
}

echo json_encode($arr);
?>

Link to comment
https://forums.phpfreaks.com/topic/220804-need-to-separate-query-result-by-comma/
Share on other sites

you are getting a json object from your php code, not a strong. Thus, it cannot be split by comma.

what you're getting is something like this:

{0:"test",1:"test2",2:"test3"}

 

http://www.google.com/search?client=safari&rls=en&q=javascript+how+to+handle+json&ie=UTF-8&oe=UTF-8

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.