Home Links

Functions of a paginal conclusion in PHP

Hello dear readers, today it would be desirable will discuss a problem of a paginal conclusion in PHP and to result possible{probable} ways of the decision. In this clause{article} I shall result rather unique function from the point of view of its{her} opportunities.

Often at creation of dynamic sites (there are no pages as such) on a database or on files there is a question on a paginal conclusion of the information be - that clauses{articles}, archive of news or that nibud` another containing dovl`no a plenty of pages.

Therefore we begin (if we shall not think or somebody will not help) to ransack on the Internet in searches of the necessary code, but a decent paginal conclusion to find hardly. So we shall try to make today that nibud` good, and the main thing working.


Below the code of the elementary function of a paginal conclusion will be resulted, this function can be used on small sites. All comments will be resulted in the function, I think you will understand.

/*Prostejshaja Function of a paginal conclusion, nazavjom her  it is simple PagePrint*/

function PagePrint ($start, $count) {

$HrefPage = "; //----we Create a variable which will contain a paginal conclusion

$number=10; //----Quantity{Amount} of recordings on page

/*Podschityvaem Quantity{Amount} of pages, where $count - total of recordings, $number - quantity{amount} of recordings on stranice*/

$CountPage = (int) (($count + $ number-1) / $number);

/*Perebiraem In a cycle all stranicy*/

for ($link=1; $link <= $CountPage; $link ++):

$PageStart = ($ link - 1) * $number; //----we Consider a point of sample of a database

$HrefPage = $ HrefPage. " <a href = ".getenv (' PHP_SELF '). "? start = ". $ PageStart. " target = _ parent> ". $link. " </a> "; // - we Form links

endfor;

return $HrefPage;

}



You see, that function has two entrance parameters $start - a point of sample of a database and $count - quantity{amount} of the data, in a file or in a database. This function the most simple so on it{her} there is not enough hope at use on large sites where a lot of information.


Let's consider more complex  function, but having there is more than functionality.

function PagePrint ($variable) {

global $beginPage, $endPage; //------It is declared the global variables responsible for the link to first and last page

$href = "; //-----Peremmennaja containing in itself a paginal conclusion

$separator = ' '; //-----the Variable specifying a separator between links, by default a blank

$style ='style = "text-decoration:none" style = "color:orange" style = "font-weight:bold" '; //-------Sets style of the current page

$stylePage ='style = "text-decoration:none" style = "color:blue" style = "font-weight:bold" '; //------Sets style of the link to first and last page

   if (is_array ($variable)): //------we Check type array

   *ESUSn¿UbS¼ the file also is appropriated{given} $number - quantity{amount} of recordings, $start - a point of extraction, $pageCount - quantity{amount} of recordings, $andere - other properties of the link, $class - style for links, $showLink - quantity{amount} of links on page

   list ($number, $start, $pageCount, $andere, $class, $showLink) = $variable;

   *DnUbnbFHHub¼ $start

     if (empty ($start) **! is_int ($start) **! is_numeric ($start)) $start=0;

     else $start = $ start;

     *DnUbnbFHHubS¼ $number

     if (empty ($number) **! is_int ($number) **! is_numeric ($number)) $number=10;

     else $number = $ number;



     $page = (($start/10) +1); //-------Rasschityvavem number{room} of page

     $count=ceil ($pageCount / $ number); //------Quantity{Amount} of pages


       *jHHu«s sslyki on first and last pages

        if ($count! == 1):

        $beginPage = " <a href = "".getenv (' PHP_SELF '). "? start=0 ". $andere. "" ". $stylePage. "> perv </a> "; //--------the First page

        $endPage = " <a href = "".getenv (' PHP_SELF '). "? start = ". (($count-1) *10). $andere. "" ". $stylePage. "> posl </a> "; //-----Last page

        else: FALSE;

        endif;



         if ($pageCount == 10): return False;

         else:

         *E«JAFUb¡¿ju¡HH® a conclusion

         $begin = $ page - intval ($showLink/2); //------Defines{Determines} number{room} of page with which to deduce{remove}

            for ($j=0; $j <= $showLink; $j ++):

            $i = $ begin + $ j; //------Number{Room} of page

            *zb``¿``bSF the script if $ <0, will increase until then while $showPage=1

              if ($i <1):

              $showLink ++;

              continue;

              endif;

                *E«JAFUb¡¿ju¡HH® a conclusion

                if ($i> $count) break; //-----Protection if quantity{amount} $i> possible{probable} recordings

                  if ($i == $page) $href = $ href. " <a ". $style. ">". $i. " </a> "; //------the Conclusion of the current page

                  else $href = $ href. " <a href = "".getenv (' PHP_SELF '). "? start = ". (($i - 1) *10). $andere. "" ". $class. ">". $ i. " </a> "; //------the Conclusion of the following pages

           endfor;

         return $href;

         endif;

   endif;

}



Entrance parameter of function is the file from six elements:

?         $number - quantity{amount} of displayed messages on page

?         $start - a point of extraction from a database, t.e number{room} of the message with which begins zivlechenie the data

?         $pageCount - total of recordings, t.e how much all is contained with base of recordings

?         $andere - other parameters of the link, whether it be new variables or something else

?         $class - the given parameter sets appearance of links, t.e links to pages

?         $showLink - quantity{amount} of links on page


The first parameter can be transferred{handed} through a method $ _GET [' number '], only in the event that you want that the user could change quantity{amount} of messages on page.

The second parameter is necessarily passed in a file by a method $ _GET [' start '] as parameter $start all time varies when the user clicks under the link to page.


If, you suddenly do not want, to pass the first, third, fourth, fifth element of a file instead of them it is necessary to pass emptiness - ", without a blank between inverted commas.


As you can see function strongly expands opportunities at creation of a paginal conclusion. Now you can place the paginal conclusion to a small site of page, adjusting quantity{amount} of displayed links in parameter $showLink. All this is convenient at creation gallerej, search under the catalogue of a site or something else.


Below examples of a call of function will be resulted:


PagePrint (", $ _GET [' start '], 90, ", ", 10) - All recordings in base 90, the paginal conclusion will display on ten links


PagePrint ($ _GET [' number '], $ _GET [' start '], 90, ", ", 10) - All recordings in base 90, the paginal conclusion will display on ten links and there is an opportunity to change quantity{amount} of displayed recordings on page


PagePrint ($ _GET [' number '], $ _GET [' start '], 90, ' *category=1 ', ", 10) - All recordings in base 90, a paginal conclusion will display on ten links, is an opportunity to change quantity{amount} of displayed recordings on page and the link passes an additional variable category with value 1


PagePrint ($ _GET [' number '], $ _GET [' start '], 90, ' *category=1 ',' style = "color:black" ', 10) - All recordings in base 90, a paginal conclusion will display on ten links, is an opportunity to change quantity{amount} of displayed recordings on page, the link passes an additional variable category with value 1 and links to page will be black color.

I hope the explanations resulted below and comments inside function will help to understand to you with a principle of job of function.


For those who wants to see a code, instead of page in interente swing PagePrint. Success in studying!



Home Page
Compression of pages on PHP
Functions of a paginal conclusion in PHP
PHP a script for automatic definition of the coding of the text:
XML Sapiens - magic of revival of sites
XML: opportunities and prospects
The data, their performance and forms of the user interface in XML
It is a little about OPML
Protection of mail
Importation of the information from an another's site on the site in the design
The forum working with database MySQL
The main page of a forum
Paginal conclusion from tables MSSQL with help PHP
Small cunnings
We print rationally or one more way of protection of the information
Site from within
The link - business not artful
Ideology HTML
Effect bukvicy
Ways of storage of the dynamic data