I have a following jQuery code
The function is, I run it to retrieve 10 row each time from the database via AJAX call.
When the data is received back on AJAX callback, each row of the response is appended to a global array.
Here's my code:
<script type="text/javascript">
var oTable;
var outer_start_row = 0;
var outer_limit = 10;
var final_data = [];
$(document).ready(function() {
window.prettyPrint() && prettyPrint();
$('#load').click(function()
{
load_data_in_datatable();
});
});
function load_data_in_datatable()
{
var v = $('#drp_v').val();
var cnt = $('#drp_cnt').val();
var ctg = $('#drp_ctg').val();
var api = $('#drp_api').val();
var nt = $('#drp_nt').val();
alert('outside start_row :'+outer_start_row);
$.post("ajax.php",
{ 'version':v,'category':ctg,
'country':cnt,'network_id':nt,
'api':api,'func':'show_datatable',
'start_row':outer_start_row,'limit':outer_limit},
function(response)
{
var data = response.data;
var limits = response.limits;
outer_limit = limits.limit;
outer_start_row = limits.start_row;
alert('inside start_row :'+outer_start_row);
final_data.push(data);
for(var f = 0; f < final_data.length; f++)
alert(final_data[f].name);
load_data_in_datatable();
},'json');
}
</script>
but the problem is, when i am using this alert(final_data[f].name); its alerting undefined.
Don't know whats going wrong
here's my php ajax function
function show_datatable($version,$ctg,$cnt,$nt,$api,$start_row,$limit)
{
$cnt_table = "aw_countries_".$version;
$ctg_table = "aw_categories_".$version;
$off_table = "aw_offers_".$version;
$sizeof_ctg = count($ctg);
$cond_ctg = " ( ";
for($c = 0; $c < $sizeof_ctg ; $c++)
{
$cond_ctg = $cond_ctg." $ctg_table.category = '".$ctg[$c]."' ";
if($c < intval($sizeof_ctg-1))
$cond_ctg = $cond_ctg." OR ";
else if($c == intval($sizeof_ctg-1))
$cond_ctg = $cond_ctg." ) ";
}
$sizeof_cnt = count($cnt);
$cond_cnt = " ( ";
for($cn = 0; $cn < $sizeof_cnt ; $cn++)
{
$cond_cnt = $cond_cnt." $cnt_table.country = '".$cnt[$cn]."' ";
if($cn < intval($sizeof_cnt-1))
$cond_cnt = $cond_cnt." OR ";
else if($cn == intval($sizeof_cnt-1))
$cond_cnt = $cond_cnt." ) ";
}
$sizeof_nt = count($nt);
$cond_nt = " ( ";
for($n = 0; $n < $sizeof_nt ; $n++)
{
$cond_nt = $cond_nt." $off_table.network_id = '".$nt[$n]."' ";
if($n < intval($sizeof_nt-1))
$cond_nt = $cond_nt." OR ";
else if($n == intval($sizeof_nt-1))
$cond_nt = $cond_nt." ) ";
}
$sizeof_api = count($api);
$cond_api = " ( ";
for($a = 0; $a < $sizeof_api ; $a++)
{
$cond_api = $cond_api." $off_table.api_key = '".$api[$a]."' ";
if($a < intval($sizeof_api-1))
$cond_api = $cond_api." OR ";
else if($a == intval($sizeof_api-1))
$cond_api = $cond_api." ) ";
}
$output = "";
$sql = "SELECT $off_table.id, $off_table.name
FROM $off_table,$cnt_table,$ctg_table
WHERE $off_table.id = $cnt_table.id
AND $off_table.id = $ctg_table.id
AND ".$cond_api."
AND ".$cond_nt."
AND ".$cond_cnt."
AND ".$cond_ctg." LIMIT $start_row , $limit";
$result = mysql_query($sql);
$arr_result = array();
while($row = mysql_fetch_assoc($result))
{
$arr_result[] = $row;
}
//$arr_result_enc = json_encode($arr_result);
//echo $arr_result_enc;
$arr_limit = array(
'start_row' => intval($start_row + $limit),
'limit' => $limit
);
//$arr_limit_enc = json_encode($arr_limit);
//echo $arr_limit_enc;
$result_json = array(
'data' => $arr_result,
'limits' => $arr_limit,
);
echo json_encode($result_json);
}
EDIT:
Here's a JSON format
{"data":[{"id":"11105","name":"Gummy Drop (iPhone, Free, ROW except CN, 72.3MB, w"}],"limits":{"start_row":1,"limit":"1"}}