I want to write a paging code without jumping out of the while loop. each page shows 6 types of food, but it seems that the code is the same no matter which page I enter: PHPcode $ each_page = 6; $ diff = $ rows % 6; $ page_org = $ rows/6; $ co cannot skip the while loop
I want to write a paging code. each page shows 6 types of food, but it seems that the page is the same no matter which page I enter.
The code is as follows:
PHP code
$ Each_page = 6; $ diff = $ rows % 6; $ page_org = $ rows/6; $ count = 1; if ($ diff> 0) {$ page = floor ($ page_org + 1);} else {$ page = floor ($ page_org);} $ page_count_start = 0; $ page_count_end = 0; $ stopwatch = 0; if (! Isset ($ _ GET ['Page']) | (isset ($ _ GET ['Page']) & $ _ GET ['Page']) = 1) {// echo "hello"; $ page_num = 1; $ page_count_end = $ page_count_start + 6;} else {// echo "hello "; $ page_num = $ _ GET ['Page']; $ page_count_start = $ page_num + ($ page_num-1) * 5; $ page_count_end = $ page_count_start + 6 ;} $ query = "SELECT * from dish where dish_id IN (SELECT dish_id FROM CATEGORY_MENU WHERE cat_id = $ cat_id)"; $ result = queryMysql ($ query ); while ($ get_details = mysql_fetch_array ($ result) {$ stopwatch ++; if ($ stopwatch <$ page_count_start) {// $ stopwatch ++; continue; // It seems that the page cannot be jumped out. try again, no matter which page is the same} $ dish_id = $ get_details ['dish _ id']; $ dish_price = $ get_details ['dish _ price']; $ dish_name = $ get_details ['dish _ name']; $ dish_img = $ get_details ['IMG _ url']; $ dish_descr = $ get_details ['dish _ description']; // echo "$ dish_id $ stopwatch"; $ page_count_start ++; if ($ page_count_start = $ page_count_end + 1) {$ stopwatch = 0; break;} // echo "..... ";}
------ Solution --------------------
Since you have calculated the starting offset, why not use the limit clause?
$ Query. = "limit $ page_count_start, 6 ";
$ Result = queryMysql ($ query );
Delete code related to $ stopwatch in the loop at the same time
------ Solution --------------------
The landlord may not know that mysql has a limit clause
------ Solution --------------------
If you can't see the echo before "continue;", your judgment condition is incorrect.
In addition, paging MySQL is implemented using limit instead of loops.
------ Solution --------------------
I am not doing php, so I said, you choose to see it.
In paging mode, I first use count (*) to query the total number of items to be queried, and then divide it by the number of items to be displayed on each page ), get the total number of pages, and then receive the incoming number of pages (inpage). if inpage is less than or equal to 0 or is empty, change inpage to 1. if it is greater than page, inpage = page.
Set variable StartPage = (inpage-1) * item,
Then write SQL
Select * from tab_name where condition limit StartPage, item;
In this way, you can query data based on the input parameters.
On the page, the previous page uses the current page-1 directly, and the next page uses the current page + 1 directly. after the above processing, there will be no extra scope.