1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<?php
/**
* Generate a Pagination (with Bootstrap).
*
* @version 1.0.0
* @link https://github.com/Zheness/Pagination/ Github Repo
* @author Zheness
*/
class Pagination {
private $nbMaxElements = 200;
private $nbElementsInPage = 20;
private $currentPage = 1;
private $url = '?page={i}';
private $innerLinks = 2;
private $linksSeparator = '...';
/**
* Set the maximum elements in your database. (Or other way)
*
* Example :
*
* I have 200 articles in my database, so I type :
*
* $Pagination->setNbMaxElements(200);
*
* @param int $int The maximum elements
* @author Zheness
*/
public function setNbMaxElements($int) {
$this->nbMaxElements = (int) $int;
}
/**
* Set the number of elements to display in the page.
*
* Example :
*
* I would display 20 articles per pages, so I type :
*
* $Pagination->setNbElementsInPage(20);
*
* @param int $int The number of elements
* @author Zheness
*/
public function setNbElementsInPage($int) {
$this->nbElementsInPage = (int) $int;
}
/**
* Set the current page
*
* Example :
*
* The current page is the 5, so I type :
*
* $Pagination->setCurrentPage(5);
*
* @param int $int The current page
* @author Zheness
*/
public function setCurrentPage($int) {
$this->currentPage = (int) $int;
}
/**
* Set the url in the links. You MUST include "{i}" where you want display the number of the page.
*
* Example :
*
* I would display my articles on "articles.php?page=X" where X is the number of page. So I type :
*
* $Pagination->setUrl("articles.php?page={i}");
*
* Why {i} ? Because the number of page can be placed everywhere. If you have your url like this "articles/month/08/page/X/sort/date-desc", you can place {i} instead of X.
*
* @param string $string The url in the link
* @author Zheness
*/
public function setUrl($string) {
$this->url = $string;
}
/**
* Set the number of links before and after the current page
*
* Example :
*
* The current page is the 5, and I want 3 links after and before, so I type :
*
* $Pagination->setInnerLinks(3);
*
* @param int $int The number of links before and after the current links
* @author Zheness
*/
public function setInnerLinks($int) {
$this->innerLinks = (int) $int;
}
/**
* Set the separtor between links.
*
* Example :
*
* I would display " - " between my links, so I type :
*
* $Pagination->setLinksSeparator(" - ");
*
* By default "..." is display.
*
* @param string $string The url in the link
* @author Zheness
*/
public function setLinksSeparator($string) {
$this->linksSeparator = $string;
}
/**
* This is the function to call for render the Pagination.
*
* You have just to configure the options and call this function.
*
* @return string The HTML Pagination (it use Bootstrap)
* @author Zheness
*/
public function renderBootstrapPagination() {
$array_pagination = $this->generateArrayPagination();
$html = $this->generateHtmlPagination($array_pagination);
return $html;
}
/**
* Generate the Pagination in array.
*
* @return array Each value is the link to display.
* @author Zheness
*/
private function generateArrayPagination() {
$array_pagination = array();
$keyArray = 0;
$subLinks = $this->currentPage - $this->innerLinks;
$nbLastLink = ceil($this->nbMaxElements / $this->nbElementsInPage);
if ($this->currentPage > 1) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', 1, $this->url) . '">1</a>';
}
if ($subLinks > 2) {
$array_pagination[$keyArray++] = $this->linksSeparator;
}
for ($i = $subLinks; $i < $this->currentPage; $i++) {
if ($i >= 2) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $i, $this->url) . '">' . $i . '</a>';
}
}
$array_pagination[$keyArray++] = '<b>' . $this->currentPage . '</b>';
for ($i = ($this->currentPage + 1); $i <= ($this->currentPage + $this->innerLinks); $i++) {
if ($i < $nbLastLink) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $i, $this->url) . '">' . $i . '</a>';
}
}
if (($this->currentPage + $this->innerLinks) < ($nbLastLink - 1)) {
$array_pagination[$keyArray++] = $this->linksSeparator;
}
if ($this->currentPage != $nbLastLink) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $nbLastLink, $this->url) . '">' . $nbLastLink . '</a>';
}
return $array_pagination;
}
/**
* Generate the HTML pagination with the array in parameter
*
* @param array $array_pagination The array generate with previous function.
* @return string Pagination in HTML. Use Bootstrap
* @author Zheness
*/
private function generateHtmlPagination($array_pagination) {
$html = "";
$html .= '<div class="pagination">';
$html .= '<ul>';
if ($this->nbMaxElements) {
foreach ($array_pagination as $v) {
if ($v == $this->linksSeparator) {
$html .= '<li class="disabled"><span>' . $this->linksSeparator . '</span></li>';
} else if (preg_match("/<b>(.*)<\/b>/i", $v)) {
$html .= '<li class="active"><span>' . strip_tags($v) . '</span></li>';
} else {
$html .= '<li>' . $v . '</li>';
}
}
} else {
$html .= '<li class="active"><span>1</span></li>';
}
$html .= '</ul>';
$html .= '</div>';
return $html;
}
} |
Partager