Scripts

Pagination

Pagination is een script wat gebruikt kan worden voor een het maken van pagina keuze d.m.v. een balk met getallen van pagina 1...3 etc, en next en previous. De meeste van de classes op het internet die pagination toepassen maken gebruik van een mysql tabel. Deze static class is gemaakt voor een fotoboek oplossing waar geen database gebruikt wordt. De listings array's komen van scandir() of glob(). Het script is opensource dus gratis. Schrijffouten gecorrigeerd.

pagination.php
<?php
include_once 'includes/language.php';//my own translation static class
/**
 * _pn::pagination('index.php?page', 
 *                  $maxpages, //comes from count(filelist)
 *                  $pageno,   //comes from web application 
 *                  PAGEROWS,  //constant
 *                  PAGECOLS,  //constant
 *                  PAGINATION)//constant
 * 
 * define('PAGEROWS', 4);
 * define('PAGECOLS', 3);
 * define('PAGINATION', 6);
 * define('PAGEMAX', 8); //max numbers in pagination
 * 
 * _lang::lang('previous'), uses your own translation class or function
 */
class _pn {
    protected static $_instance = null;
    private static $page, $pages;//never used ...

    public static function instance() {
        if (is_null(static::$_instance)) {
            static::$_instance = new self();
        }
        return static::$_instance;
    }

    public static function static_instance() {
        if (is_null(static::$_instance)) {
            static::$_instance = new static();
        }
        return static::$_instance;
    }

    public static function pagination($path, 
                                      $maxpages, 
                                      $start=1, 
                                      $row=4, 
                                      $column=4, 
                                      $max=6) {
        
        if ($maxpages<$max) {
            $end = $maxpages;
        } elseif ($start<$max) {
            $end = $max;
        } elseif ($maxpages>$max) {
            $end = ($start+$max<=$maxpages)?$start+$max-1:$maxpages;
            $end = ($end==$maxpages)?$end-1:$end;
        } elseif ($maxpages==$max) {
            $end = $max;
        } elseif ($max>$maxpages) {
            $end = $maxpages-1;
        } elseif ($start+$max>$maxpages) {
            $end = $maxpages;
        }
        $page = $start;
        if ($start== $max&&($maxpages<=$max)) {
            $start = 1;
        } elseif ($start+$end <= $max) {
            $start = 1;
        } elseif ($start<$max) {
            $start = 1;
        } elseif ($start+$max>$maxpages) {
            $start = (($maxpages-$start)>=0)?($maxpages-$max):1;
          //$start = $maxpages - $start;  
        } elseif ($maxpages == $max) {
            $start = 1;
        }
        $html =  '<div style="width:100%" class="center">';
            $html .=  '<div class="center">';
                $html .= '<div class="float_left btn">';
                    $html .= '<a class="pagination" href="' . $path . '=previous">' . _lang::lang('previous') . '</a>';
                $html .= '</div>';
                for ($i = $start; $i <= ($end); $i++) {
                    if (($i<=$end)) {
                        $html .= '<div class="float_left btn">';
                            $html .= '<a class="pagination" href="' . $path . '=' . $i . '"> ';
                                $html .= ($page==$i)?'<b>'. $i .'</b>':$i;
                            $html .= '</a>';
                        $html .= '</div>';
                    }
                }
                if (($maxpages!=$max)&&($maxpages>$max)&&($i<$maxpages)&&($maxpages>($end-1))) {
                    $html .= '<div class="float_left empty_btn">';
                        $html .=  ' ... ';//'<a class="pagination" href="' . $path . '=' . $i . '"> ' . $i . '</a>';
                    $html .= '</div>';
                }
                if ($i<=$maxpages) {
                    $html .= '<div class="float_left btn">';
                        $html .= '<a class="pagination" href="' . $path . '=' . $maxpages . '"> ';
                            $html .= ($page==$maxpages)?'<b>' . $maxpages . '</b>':$maxpages; 
                        $html .= '</a>';
                    $html .= '</div>';
                }
                $html .= '<div class="float_left btn">';
                    $html .= '<a class="pagination" href="' . $path . '=next">' . _lang::lang('next') . '</a>';
                $html .= '</div>';
            $html .= '</div>';
        $html .= '</div>';
        return $html;
    }
}
/**
 * Language
    $lang['nl']['previous']     = 'Vorig';
    $lang['nl']['next']         = 'Volgend';
 */
/**
 * Style items
 * .empty_btn {
    background-color: rgba(192,192,192,0.3);
    word-break: normal;
    border: #73AD21 solid thin;
    padding: 6px;
    margin: 1px;
}   
.btn {
    background-color: rgba(192,192,192,0.3);
    word-break: normal;
    border: #73AD21 solid thin;
    padding: 6px;
    margin: 1px;
}   
.btn:hover {
    background-color: #73AD21;
    border: #DDD inset medium;
    color: white;
}
a.pagination {
    color : #73AD21; 
}
a.pagination:hover {
    color : white; 
}
.float_left {
    float:left;
}

 * 
 */
/**
 * Sample piece web application part
 * 
 * if (isset($_GET['page'])) {//do navigation choice  
    //take session values or default
    $page_set=$_GET['page'];
    //$pageno = a_sessions::_SessionGet('page_set');
    (isset($pageno))||$pageno = 1;
    switch ($page_set) {
        case 'previous':
            $pageno = a_sessions::_SessionGet('pageno');
            if ($pageno>1) {
                $pageno=$pageno-1;
            } else {
                $pageno = 1;
            }
            break;
        case 'next':
            $pageno = a_sessions::_SessionGet('pageno');
            (!isset($pageno))&&$pageno = 1;
            $fCount = a_sessions::_SessionGet('maxpages');
            (!isset($fCount))&&$fCount = PAGEMAX;
            if ($pageno+1<=$fCount) {
                $pageno=$pageno+1;
            }
            break;

        default:
            $pageno = $page_set;
            break;
    }
    a_sessions::_SessionSet('pageno', $pageno);

 */
?>

Reacties

0
Nog geen reacties.