Ask questions about generating JSON pagination in PHP

Source: Internet
Author: User
I have been generating PHP & nbsp; JSON pagination in PHP recently. to implement pagination, the following code can generate arrays. how can I implement pagination? I want to achieve every 10 pages per page & lt ;? Php & nbsp; // connection & nbsp; information may I ask questions about JSON paging generated by PHP?
Recently, PHP is being generated to generate JSON. to implement paging, the following code can generate arrays. how can we implement paging? To achieve every 10 pages

// 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 );

?>
Share php json:
------ Solution --------------------
You can pagination first and then generate json.
------ Solution --------------------
And then use json_encode () to generate json.
------ Solution --------------------
Pages are not divided by JS on the client. pages are usually calculated on the server using PHP to obtain the number of database records and then the selected records are obtained directly from the database.
For example, you used the GET or POST method to guide the page number,
$ Page = 1; // page
$ Pagenum = 10; // number of entries per page
$ Start = ($ page-1) * $ pagenum; // This is the number of records starting from calculation.
// Your SQL statement can be written in this way
$ SQL = "SELECT * FROM akb order by infoid desc LIMIT $ start, $ pagenum ";
------ Solution --------------------
Front-end, for example, using the jquery library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.