Php+smarty Template Paging Program code

Source: Internet
Author: User
Tags data structures prev smarty template

Functional comparison of the whole small page, pass a few parameters up on the line, the output of the calculation after the array, nothing new, but added a shortened page number, the results are pressed to an array, easy to output and call. A smarty output sample is attached ....

The code is as follows Copy Code

/**
* Pagination, public method
* @param int $page Current page
* @param int $size Data Bar number
* Total records of @param int $count
* @return Mixed page number Array (' current page ', prev ', ' Next ', ' home ', ' last ', ' page number ') (' current ', ' prev ', ' Next ', ' first ', ' previous ', ' Pages ' =>array (0= >array (' name ' =>1, ' page ' => ' 1 '))
*/
Public Function Pager ($page, $size, $count) {
Omit page number marks
$name _prev_more = ' ... ';
$name _next_more = ' ... ';
Show number of page numbers
$pages _length = 9;

Calculate the total number of pages
if ($count) {
$num = Ceil ($count/$size);
}

if ($num && $page) {
Current page
$result [' current ']= $page;

Previous page
if ($page-1) {
$result [' prev '] = $page-1;
}else{
$result [' prev '] = 1;
}

Next page
if ($num = = $page) {
$result [' next '] = $num;
}else{
$result [' next '] = $page + 1;
}

$pages = Array ();

Calculate the length of an omitted page number
if ($num >= $pages _length) {
$length = Intval ($pages _length/2);
}else{
$length = Intval ($num/2);
}

       //page number
        for ($i =1 $i < = $num; $i + +) {
            if ($page = = $i) {
                 $pages [$i] [' active '] = ' Active ';
           }
            if ($i >= $page-$length && $i <= $page + $length) {
                $ pages[$i] [' page '] = $i;
                $pages [$i] [ Name '] = $i;
           }
       }

       //Start omitting long page numbers
        if ($page- 1 > $length) {
            $prev _more[' page '] = $page-$ Length-1;
            $prev _more[' name ']  = $name _prev_more ;
            array_unshift ($pages, $prev _more);
       }

       //End omitted long page number
        if ($num-$ Page > $length) {
            $next _more[' page '] = $ Page + $length + 1;
            $next _more[' name ']  = $name _next_more ;
         array_push ($pages, $next _more);
       }

$result [' pages '] = $pages;
Home, last
$result [' a '] = 1;
$result [' last '] = $num;
Print_r ($result); TODO: View Data structures
return $result;
}
Return ";
}

Return data structure

Pager (1,10,11)

Array
(
    [current] => 1
    [prev] => 1
    [NEX T] => 2
    [pages] => Array
        (
             [1] => Array
                 (
                     [Active] => active
                     [Page] => 1
                     [name] => 1
                )

[2] => Array
(
[Page] => 2
[Name] => 2
)

)

[A] => 1
[Last] => 2
)

Pager (1,1,11);

Array
(
    [current] => 1
    [prev] => 1
    [NEX T] => 2
    [pages] => Array
        (
             [1] => Array
                 (
                     [Active] => active
                     [Page] => 1
                     [name] => 1
                )

            [2] => Array
                 (
                     [Page] => 2
                     [Name] = > 2
               )

            [3] => Array
                 (
                     [Page] => 3
                     [Name] = > 3
               )

            [4] => Array
                 (
                     [Page] => 4
                     [Name] = > 4
               )

            [5] => Array
                 (
                     [Page] => 5
                     [Name] = > 5
               )

[6] => Array
(
[Page] => 6
[Name] => ...
)

)

[A] => 1
[Last] => 11
)

PHP Call and Smarty output

PHP Call (new smarty or something will not be written)

The code is as follows Copy Code
$pager = Pager ($page, $size, $count);
$smarty->assign (' pager ', $pager);

Smarty Templates

The code is as follows Copy Code

<div id= "Pager" >
<a href= "? page={$item. Page}" ></a>
{foreach from= $pager. Pages Item=item}
<a href= "? page={$item. Page}" class= "{$item. Active}" >{$item .name}</a>
{/foreach}
<a href= "? page={$item. Page}" ></a>
</div>

Return data structure

The code is as follows Copy Code

Pager (6,1,11)

Array
(
[Current] => 6
[Prev] => 5
[Next] => 7
[Pages] => Array
(
[0] => Array
(
[Page] => 1
[Name] => ...
)

            [1] => Array
                 (
                     [Page] => 2
                     [Name] = > 2
               )

            [2] => Array
                 (
                     [Page] => 3
                     [Name] = > 3
               )

            [3] => Array
                 (
                     [Page] => 4
                     [Name] = > 4
               )

            [4] => Array
                 (
                     [Page] => 5
                     [Name] = > 5
               )

            [5] => Array
                 (
                     [Active] => active
                     [ Page] => 6
                     [name] => 6
                )

            [6] => Array
                 (
                     [Page] => 7
                     [Name] = > 7
               )

            [7] => Array
                 (
                     [Page] => 8
                     [Name] = > 8
               )

            [8] => Array
                 (
                     [Page] => 9
                     [Name] = > 9
               )

            [9] => Array
                 (
                     [Page] =>
                     [Name] = >
               )

[Ten] => Array
(
[Page] => 11
[Name] => ...
)

)

[A] => 1
[Last] => 11
)

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.