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!