我现在有一个jquery,我想分享
$('#load').click(function()
{
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();
$.post("ajax.php",
{ 'version':v,'category':ctg,
'country':cnt,'network_id':nt,
'api':api,'func':'show_datatable',
'start_row':'0','limit':'2'},
function(data)
{
alert(data[1].limit);
var aColumns = [];
var columns = [];
for(var i = 0; i < data.length; i++)
{
if(i>0)
break;
keycolumns = Object.keys(data[i]);
for(j = 0; j < keycolumns.length; j++)
{
if($.inArray(keycolumns[j],aColumns.sTitle)<=0)
{
aColumns.push({sTitle: keycolumns[j]}) //Checks if
columns.push(keycolumns[j]) //Checks if
}
}
}
var oTable = $('#jsontable').dataTable({
"columns":aColumns,
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
{
"sExtends": "csv",
"sButtonText": "CSV",
}
]
}
});
oTable.fnClearTable();
var row = []
for(var i = 0; i < data.length; i++)
{
for(var c = 0; c < columns.length; c++)
{
row.push( data[i][columns[c]] ) ;
}
oTable.fnAddData(row);
row = [];
}
},'json');
});
现在ajax.php已经结束了,它将数据返回给jquery ajax回调函数
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 *
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;
}
现在的问题是我得到的数据是这样的
[{"id":"10580","name":"Galaxy Legend (Android, Free, ROW, w\/ capping)"},
{"id":"10718","name":"Slots Vacation (Android, Free, Tier 3, 35MB, w\/ca"}]{"start_row":2,"limit":"2"}
[]内的数据分配是我需要在数据表中加载的数据库结果,
{&#34; start_row&#34;:2,&#34;限制&#34;:&#34; 2&#34;}应单独用于检查目的。
但问题是,我不能分开他们,即我不能分开
1. [{"id":"10580","name":"Galaxy Legend (Android, Free, ROW, w\/ capping)"},
{"id":"10718","name":"Slots Vacation (Android, Free, Tier 3, 35MB, w\/ca"}]
和第二个
2. {"start_row":2,"limit":"2"}
我该怎么做?
提前致谢
答案 0 :(得分:2)
重新格式化您的返回JSON:
return json_encode(array(
'database' => $database,
'parameters' => $parameters
));
以便JSON读取
{
"database": [{"id":"10580","name":"Galaxy Legend (Android, Free, ROW, w\/ capping)"},
{"id":"10718","name":"Slots Vacation (Android, Free, Tier 3, 35MB, w\/ca"}
],
"parameters": {"start_row":2,"limit":"2"}
}
然后在收到data
的jQuery中,您将使用现在使用数据的data.database
和data.parameters
来阅读其他信息。即data.parameters.limit
会给你2,依此类推:
function(data) {
// OLD alert(data[1].limit);
alert(data.parameters.limit);
...
for(var i = 0; i < data.database.length; i++) {
if (i>0) break;
keycolumns = Object.keys(data.database[i]);
...
答案 1 :(得分:1)
将它们添加到1个对象中。为此,请将PHP代码的最后一部分更改为以下内容:
$result_json = array(
'data' => $arr_result,
'limits' => $arr_limit,
);
echo json_encode($result_json);
之后,在您的AJAX请求中,resultdata包含data
- 和limits
属性。根据需要单独使用这些;
$.post("ajax.php",
{
'version':v,
'category':ctg,
'country':cnt,
'network_id':nt,
'api':api,
'func':'show_datatable',
'start_row':'0',
'limit':'2'
},
function(data) {
var tableData = data.data;
/** tableData contains [{"id":"10580","name":"Galaxy Legend (Android, Free, ROW, w\/ capping)"}, {"id":"10718","name":"Slots Vacation (Android, Free, Tier 3, 35MB, w\/ca"}] **/
var limits = data.limits;
/** limits contains {"start_row":2,"limit":"2"} **/
/** do something with the variables **/
},
'json'
);