PHP infinite loop to obtain MySQL Data instance code, mysql instance

Source: Internet
Author: User

PHP infinite loop to obtain MySQL Data instance code, mysql instance

Recently, the company has a need to obtain data from MySQL, and then display the data on the page in a loop. It is mainly to keep clicking a button, and then the data starts from the beginning to the end. If the data at the end is not enough, then add several records from the beginning of the data.

In fact, this function can be implemented through JQ or through PHP + MYSQL, but JQ is more convenient and more efficient.

10 data entries are displayed at a time.

 public function get_data($limit){ $sql="select * from ((select id,name from `mytable` limit {$limit},10) union all (select id,name from `mytable` limit 0,10)) as test limit 0,10";    return $this->query($sql); }

The preceding SQL statement concatenates two sets using the union all method of mysql and obtains the first 10 pieces of data.

Public function getCount () {// number of data records obtained $ SQL = "select count (id) as t from 'mytable '"; return $ this-> query ($ SQL );}

The next step is to obtain data in the Controller and provide the data interface for ajax.

// Test Database infinite loop to obtain data public function getInfiniteData () {// user clicks $ page =$ _ GET ['click']; // The number of entries displayed each time $ pagesize = 10; // obtain the total number of items $ total = $ this-> Mydemo-> get_count (); $ t = $ total [0] [0] ['T']; // calculate the start position of each click. $ limit = ($ page-1) * $ pagesize) % $ t; $ data = $ this-> Mydemo-> get_data ($ limit); if (! Empty ($ data) {// convert to a two-dimensional array $ list = []; foreach ($ data as $ key => $ v) {$ list [$ key] = $ data [$ key] [0];} $ info ['msg '] = $ list; $ info ['code'] = '001';} else {$ info ['code'] = '002 '; $ info ['msg '] = 'no data temporarily';} echo json_encode ($ info, JSON_UNESCAPED_UNICODE); die ;}

Summary

The above is a small series of PHP infinite loop to get the data instance code in MySQL, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.