PHP JSON
Recently in the generation of PHP generated JSON, want to implement paging, the code below has been able to create the array, paging how to implement it? Want to achieve every 10 pages of the page
Connection Information
$host = "XXX";
$user = "XXX";
$password = "XX";
$database = "XX";
Make connection
$server = mysql_connect ($host, $user, $password);
$connection = mysql_select_db ($database, $server);
Query the database
mysql_query (' Set character set ' UTF8 ');
$query = mysql_query ("SELECT * from AKB ORDER by infoid Desc");
Start JSON object
$json = ' {' tags ': [';
Loop through and return results
for ($x = 0; $x < mysql_num_rows ($query); $x + +) {
$row = Mysql_fetch_assoc ($query);
$infoid = $row ["Infoid"];
$Title = $row ["Title"];
$pic = $row ["logoimg"];
$bpic = $row ["LogoImg2"];
$time = $row ["Acctime"];
$info = $row ["Burden"];
$url = $row ["Exturl"];
$json. = ' {' id ': '. $infoid. ' "," title ":" '. $Title. ' "," pic ":" '. $pic. ' "," bpic ":" '. $bpic. ' "," Time ":" '. $time. ' "," info ":". $info. ' "," url ":". $url. '} ';
if ($x < mysql_num_rows ($query)-1)
$json. = ",";
Else
$json. = "]}";
}
$response = $_get["Callback"]. $json;
Echo $response;
Close connection
Mysql_close ($server);
?>
Reply to discussion (solution)
You can split the page first and then generate the JSON.
Mister, and then use Json_encode () to generate JSON.
Paging is not in the client with JS, paging is usually on the server side with PHP to calculate the number of database required to obtain, and then directly from the database to obtain the selected records.
For example, you guide the page by the Get or post method,
$page = 1;//First page
$pagenum = 10;//per page
$start = ($page-1) * $PAGENUM;//This is the number of records that are calculated for the start
Your SQL statements can be written like this.
$sql = "SELECT * from AKB ORDER BY infoid desc LIMIT $start, $pagenum";
The front-end part, for example with the jquery library
var page=1;var total=-1;$.getjson (' list.php?total= ' +total+ ' &page= ' +page,function (data) {var len,i,htm; Total = Data.total; Lists = data.lists; Len=data.length; htm = '; for (i=0; i
The above JS is probably a meaning, when used can not be directly copy, according to their own situation.
Back-end Section
$result = Array (), if ($total = =-1) { $result [' total '] = mSQL The number of records that meet the criteria}else{ $result [' totals '] = $total;} The current number of pages $page = min (1, $page),//Load data per page $pgerow = ten; $sql = "SELECT * from AKB ORDER BY infoid desc LIMIT". ($page-1) * $pagerow. ", $pagerow"; $lists = Array (); $query = mysql_query ($sql); for ($x = 0; $x < mysql_num_rows ($query); $ X + +) { $lists [] = $row;} $result [' lists '] = $lists; echo json_encode ($result); exit;
The idea is the same as upstairs, but here is the total number of records, because this query can only be done at the first time, back to the front end cache.
The subsequent query puts total in Get, which has a slightly more efficient advantage.
I remember before have encapsulated this kind of thing, do not know you this application scenario is not the general web paging, if is, can find many similar encapsulation function. Under Google
Do not understand why the introduction of JSON, with PHP direct page output is good AH.
Used for Android development call data with, not Java, so use PHP to build!
Used for Android development call data with, not Java, so use PHP to build!
Our Android development is all called PHP interface, very convenient.