functie de generat paginatie – revizuita

Acum ceva timp vorbeam despre o functie de generat paginatie in PHP.

Ei bine timpul a trecut si am avut timp sa dezvolt mai bine functia.

Daca veti face niste comparatii intre cele doua functii veti observa ca delimitatorul dintre pagini este o variabila pe care o puteti modifica cum doriti. Mai multe detalii gasiti si in fisierul functie.zip.

In continuare va invit sa observati varianta revizuita a functie de generat paginatie:

[php]
function make_pages($total_page, $curr_page, $url, $next = 0,$delim=" | ") {

if ($next == 1) {

if ($total_page > 1) {

if ($curr_page > 1) {

$da .= "<a href=\"" . str_replace ( "[page]", ($curr_page – 1), $url ) . "\" class=\"paginatie_prev_page\"> </a> $delim ";

} else {

$da .= "<a href=\"#\" class=\"paginatie_prev_page\"> </a> $delim ";

}

}

}

if ($total_page < 1) {

$da .= "<a href=’" . str_replace ( "[page]", 1, $url ) . "’>1</a> $delim ";

}

if ($total_page < 15) {

for($i = 1; $i < ($total_page + 1); $i ++) {

if ($i == $curr_page) {

$da .= "<span class=\"curr\">" . $i . "</span> $delim ";

} else {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

} else {

if ($curr_page < 8) {

for($i = 1; $i < 11; $i ++) {

if ($i == $curr_page) {

$da .= "<span class=\"curr\">" . $i . "</span> $delim ";

} else {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

$da .= " …. ";

for($i = ($total_page – 5); $i < ($total_page + 1); $i ++) {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

if (($curr_page > 7) && ($curr_page < ($total_page – 4))) {

for($i = 1; $i < 4; $i ++) {

if ($i == $curr_page) {

$da .= "<span class=\"curr\">" . $i . "</span> $delim ";

} else {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

$da .= " … ";

for($i = ($curr_page – 3); $i < ($curr_page + 3); $i ++) {

if ($i == $curr_page) {

$da .= "<span class=\"curr\">" . $i . "</span> $delim ";

} else {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

$da .= " … ";

for($i = ($total_page – 3); $i < ($total_page + 1); $i ++) {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

} elseif (($curr_page > 7)) {

for($i = 1; $i < 4; $i ++) {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

$da .= " … ";

for($i = ($total_page – 7); $i < ($total_page + 1); $i ++) {

if ($i == $curr_page) {

$da .= "<span class=\"curr\">" . $i . "</span> $delim ";

} else {

$da .= "<a href=\"" . str_replace ( "[page]", $i, $url ) . "\">" . $i . "</a> $delim ";

}

}

}

}

if ($next == 1) {

if ($total_page > 1) {

if ($curr_page < $total_page) {

$da .= "<a href=\"" . str_replace ( "[page]", ($curr_page + 1), $url ) . "\" class=\"paginatie_next_page\"> </a> $delim ";

} else {

$da .= "<a href=\"#\" class=\"paginatie_next_page\"> </a> $delim ";

}

}

}

return $da;

}

[/php]

Poti sa descarci aceasta functie completa de aici.

Un comentariu

  1. arata ciudat codul la functia de paginatie; cred ca ti-o da peste cap evidentiatorul de sintaxa, sau ceva…

Spune si tu parerea...