1

I am coding an ajax live search box and the returned row could come from two separate MySQL tables. I need to figure out what table the data has come from. For that i figured, as table1 has three columns and table2 has five, i can count the number of elements in the returned array and then i will know.

But MySQL query needs to return the top three entries, hence i am getting an array which always has three elements, which are again arrays.

I need to count the number of elements in the internal array.

How would i do that?

I tried the following, it doesn't work:

$('#search').keyup(function(e) {
  $('#results').empty();
  if ($(this).val().length > 2) {
    $.get('php/search.php', {
      input: $(this).val()
    }, function(data) {
      res = $.parseJSON(data);
      //check if returned data is customer row or vehicle row by counting elements
      //customer row will have 3, vehicle will have 5
      //var x = res[0].length;
      for (var i = 0; i < res.length; i++) {
        var a = "<h2 class=\"searchcustomer\" id=\"result" + i + "\">" + res[i]['name'] + " " + res[i]['mobile'] + "</h2>";
        $('#results').append(a);
        //alert('Its customer'); 
      };
    });
  };
}); 

The return from the server file is:

[{
  "id": "1",
  "cusid": "4",
  "make": "Hyundai",
  "model": "I10",
  "registration": "DL23IK4837"
}, {
  "id": "2",
  "cusid": "4",
  "make": "Maruti",
  "model": "Swift",
  "registration": "DL87UU8933"
}, {
  "id": "3",
  "cusid": "6",
  "make": "Mitsubishi",
  "model": "Lancer",
  "registration": "DL22NM4435"
}]

4
  • 1
    show the structure of returned data Commented May 13, 2015 at 11:27
  • Add that as text. Not image Commented May 13, 2015 at 11:40
  • Please format the code proper too, Commented May 13, 2015 at 11:49
  • @mplungjan apologies, but thanks for doing it for me. I clicked on edit to do it, and then saw it was all done. Was wondering how. Commented May 13, 2015 at 11:59

1 Answer 1

2

You are using length on object, it will be undefined always. You should use Object.keys instead.

var x = Object.keys(res[0]).length; //this is working

You have many syntax errors in the code above:

$('#search').keyup(function(e) {
$('#results').empty();
if ($(this).val().length > 2) {
    $.get('php/search.php',
        {
            input: $(this).val(), cusid: 0
        },
        function(data) {
            res = $.parseJSON(data);

            // check if returned data is table1 or table2 by counting elements
            // table1 will have 3, table2 will have 5

            var x = Object.keys(res[0]).length; //this is working
            //      ^^^^^^^^^^^^^^^^^^^^^^^^^^
            alert(Object.keys(res[0]).length);
            if (x == 3) {
                // respective code
            }
            else if (x == 5) {
                // respective code
            }
        });
}

});

  1. Using $x to get data and x in if conditions
  2. ; should not be used for if else
  3. Missing ) of get

Demo: https://jsfiddle.net/tusharj/Loevj0tL/

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

7 Comments

Yeah... I actually pasted the code and then edited it in the browser..Let me fix what i can spot again and then edit it here.
@user3324298 Show the data returned from server
Great thanks! I actually saw this somewhere else in another help website, but i guess i didn't use it properly that time. This works!
How would the valid array have a length of 0? This alerts 3: res = $.parseJSON('[{ "id": "1", "cusid": "4", "make": "Hyundai", "model": "I10", "registration": "DL23IK4837"}, { "id": "2", "cusid": "4", "make": "Maruti", "model": "Swift", "registration": "DL87UU8933"}, { "id": "3", "cusid": "6", "make": "Mitsubishi", "model": "Lancer", "registration": "DL22NM4435"}]') alert(res.length)
@mplungjan The valid array does have a length of 3. Where did anyone say otherwise? I needed the length of the first element in the array. So not res.length, but length of res[0].
|

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.