Id of the next page on the previous page obtained by calling the database

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about this function. This function is required when you implement the "Previous Page" and "next page" navigation links, however, the document IDs are generally inconsistent, so you cannot use the id of the current page plus one minus one. Instead, you can query the IDs of the records in the upper and lower rows of the database. The function is now written as follows:
Function article_guide ($ current_id, $ table_name) {// $ current_id is generally obtained through get
$ A = M ($ table_name); // A = article
$ Result = $ A-> order ('Id ASC ')-> select ();
$ Guide ['min _ id'] = $ link [0] ['id']; // the smallest id in the database
$ Guide ['max _ id'] = $ link [count ($ result)-1] ['id']; // The largest id in the database
// Obtain the ID of the previous Article
$ Prev_id = $ current_id-1;
While ($ A-> where ('Id = "'. $ prev_id.'" ')-> getField ('id') = false ){
If ($ prev_id <= $ min_id ){
$ Prev_id = '-1'; // indicates the earliest article
Break;
}
$ Prev_id --;
}
// Obtain the id of the next article
$ Next_id = $ current_id + 1;
While ($ A-> where ('Id = "'. $ next_id.'" ')-> getField ('id') = false ){
If ($ next_id> = $ max_id ){
$ Next_id = '-2'; // indicates the latest article.
Break;
}
$ Next_id ++;
}
$ Guide ['Next _ id'] = $ next_id;
$ Guide ['prev _ id'] = $ prev_id;
Return $ guide;
}
Call this function in the operation you need, assign values to the template, and add the corresponding id to the next page of the previous page.
You can also use the id to determine whether the page can be accessed: I ('get. id') =-1? $ This-> error ('no earlier articles! ', 'Index'): false;
I ('get. id') =-2? $ This-> error ('no updated article! ', 'Index'): false;

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.