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"
}]