Php txt message board (paging function)
Php tutorial txt message board (paging function)
$ File = "data. dat"; // data file
If (file_exits ($ file) {// fault tolerance Processing to prevent the existence of non-data files
$ Fp = fopen ($ file, "w + ");
Fclose ($ fp );
Unset ($ fp );
}
$ Listnumber = 20; // number of entries displayed per page
$ Fp = file ($ file); // read data to the content
$ Number = count ($ fp); // calculates the total data volume
$ Pagenumber = floor ($ number/$ listnumber) + 1;
If ($ number <1 ){
Print "no record at the moment, please leave a message ";
/* + -------------------------------- +
| Print the form or link to the table here |
| Url, |
+ -------------------------------- + */
} Else {
If (empty ($ _ post ["page"]) {// This section if... else... it is written to ensure compatibility with the default php4.20 settings and prevent invalid global variables.
$ Page = 0; // because it is read from the data text, it is saved in the array and has an o subscript.
} Else {
$ Page = $ _ post ["page"]
}
If ($ page <0 | $ page> pagenumber) {// when the page parameter jumps out of the total page or is smaller than the homepage (0), it is returned to the first page.
$ Page = 0;
}
$ Startnote = $ page * $ listnumber; // start recording location
$ Endnote = $ startnote + $ listnumber; // end record location
// For ($ int_a = $ startnote; $ int_a <$ endnote; $ int_a ++) {// sorting display is similar to asc Mode
Pirnt "<table>"
For ($ int_a = $ endnote; $ int_a >=$ startnote; $ int_a --) {// sort display is similar to desc Mode
$ Info = explode ("t", $ fp [$ int-a]); // cut data to obtain detailed data for each record. Here we use a tab key to separate
Print "<tr> <td> User: $ info [1] title $ info [2] content: $ info [3] Release Date: $ info [4] ip: $ info [5] ";
}
$ Prevpage = $ page-1; // Number of pages on the previous page
$ Nextpage = $ page + 1; // Number of pages on the next page
Print "<tr> <td>
<A href = "$ _ server [" php_self "]? Page = 0 "> homepage </a>
<A href = "$ _ server [" php_self "]? Page = $ prevpage "> previous page </a>
<A href = "$ _ server [" php_self "]? Page = $ nextpage "> next page </a>
<A href = "$ _ server [" php_self "]? Page = $ pagenumber "> last page </a>
</Table> "; // here, You can directly add the page detection here. It is comfortable for others, but the effect is the same.
// For example, <a href = "index. php? Page = 21 "> next page </a> or <a href =" index. php? Page =-1 ">
}