1

I've got the following code which works only for 70 items. the moment I added the 71st item to the DB it fails to work.

$('document').ready(function() {

      //autoComplete() returns a php array with all the products
    <?php $productArray = autoComplete();?>

    var js_products_array = <?php echo json_encode($productArray); ?>;

    var result;

     for(var i=0;i<js_products_array.length;i++){
        result += js_products_array[i]  + ', ';
    }
     //printing js_products_array 

    document.write(result);


    $( "#autocompleteID" ).autocomplete({
                    source: js_products_array
                 });

...

I can see that js_products_array holds all the values but the autocomplete feature fails to work. The moment i remove the 71st item from the DB it works again.

I'm puzzled as to what's causing this. Would appreciate some help, cheers.

2
  • what is your 71 st item ???? Commented Nov 19, 2012 at 6:29
  • hi, its just a string. I have tried multiple different strings but it doesn't seem to make a difference. Commented Nov 19, 2012 at 6:42

2 Answers 2

1

hmmm, try to use

json_last_error


var last_error = <?php echo json_last_error(); ?>;

http://php.net/manual/en/function.json-last-error.php

to check the problem

or

limit the number of records , it is easy to limit the number of records in database level

UPDATE

try

in your server function autoComplete() before returning the result to the view use this function to utf-8 encode the array

utf8_encode_deep($result_from_db);

function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            utf8_encode_deep($input->$var);
        }
    }
}

then you can use

json_encode($productArray);

Sign up to request clarification or add additional context in comments.

3 Comments

'or limit the number of record' I don't see how that will work since i need all the products to be loaded so it will be available in the javascript array js_products_array for the autocomplete feature.
as per your recommendation i ran json_last_error. on the 71st record it returned: 5 = JSON_ERROR_UTF8..trying to figure out what this means..ok so it means Malformed UTF-8 characters, possibly incorrectly encoded. Any suggestions on what i need to do to fix this? thanks!
check my new update . and also look at php.net/manual/en/function.utf8-encode.php
0

The following piece of code fixed the problem: mysqli_set_charset($db, "utf8");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.