Disable pagination link if already on corresponding page

0

I have the code below and wanted to add the class disabled when the user is on the page where he clicked. For example, if you click on page 3 the link on page 3 would have the class disabled so you can not click on it.

Se for a página inicial da consulta, a variável $pag será nula

if(!$pag) {
                        $pc = "1";
                        }
                        Caso contrário, declaramos o valor atual da variável $pag
                        else {
                        $pc = $pag;
                        }
#Armazenamos a quantidade total de registros
                        $todos = $totalreg->num_rows;

                        Armazenamos o resultado da quantidade total de registros pela quantidade de registros por página
                        $tp = $todos / $registros_pagina;

E por fim montamos os links da paginação
                $tp = ceil($tp);
                if($pc>1) {
                    $anterior = $pc - 1;
                    echo '<li><a href="?pag='.$anterior.'" title="Anterior"><i class="fas fa-angle-left"></i></a></li>';
                }
                for($i=$pc-5;$i<$pc;$i++) {
                    if($i<=0) {
                    }
                    else {
                        echo '<li><a href="?pag='.$i.'">';
                        if($i==$pc) {
                            echo $i;
                        }
                        else {
                            echo $i;
                        }
                        echo '</a></li> ';
                    }
                }
                for($i=$pc;$i<=$pc+5;$i++) {
                    if($i==$tp) {
                        echo '<li><a href="?pag='.$i.'">';
                        if($i==$pc) {
                            echo $i;
                        }
                        else {
                            echo $i;
                        }
                        echo '</a></li> ';
                        break;
                    }
                    else {
                        echo '<li><a href="?pag='.$i.'">';
                        if($i==$pc) {
                            echo $i;
                        }
                        else {
                            echo $i;
                        }
                        echo '</a></li> ';
                        if($i==$pc+5 && $tp>$pc+5) {
                            echo ' ... <li><a href="?pag='.$tp.'">'.$tp.'</a></li>';
                        }
                    }
                }
                if($pc<$tp) {
                    $proxima = $pc + 1;
                    echo '<li><a href="?pag='.$proxima.'" title="Pr&oacute;xima"><i class="fas fa-angle-right"></i></a></li>';
                }
    
asked by anonymous 20.12.2018 / 19:04

1 answer

1

Try this:

for($i=$pc;$i<=$pc+5;$i++) {
    if($i==$tp) {
        echo '<li><a href="?pag='.$i;
        if($i==$pc) {
             echo '" class="disabled">'.$i; // <-- insere a classe disabled
        }
        else {
            echo '">'.$i; 
        }
        echo '</a></li> ';
        break;
    }
    else {
        echo '<li><a href="?pag='.$i;
        if($i==$pc) {
             echo '" class="disabled">'.$i; // <-- insere a classe disabled
        }
        else {
            echo '">'.$i; 
        }
        echo '</a></li> ';
        if($i==$pc+5 && $tp>$pc+5) {
            echo ' ... <li><a href="?pag='.$tp.'">'.$tp.'</a></li>';
        }
    }
}

This for can get better. Notice in the image that you have 2 blocks repeated using the same rating:

It'sbesttoputitoutofthisreviewbecauseit'scommonforbothcases,see:

for($i=$pc;$i<=$pc+5;$i++){//---------------------inicioBlocoA--------------echo'<li><ahref="?pag='.$i;
    if($i==$pc) {
        echo '" class="disable">'.$i; // <-- insere a classe
    }
    else {
        echo '">'.$i; 
    }
    echo '</a></li> ';
    // ---------------------final Bloco A --------------
    if($i==$tp) {
       break; 
    }
    if($i==$pc+5 && $tp>$pc+5) {
        echo ' ... <li><a href="?pag='.$tp.'">'.$tp.'</a></li>';
    }
}

This way it gets leaner and easier to maintain.

    
20.12.2018 / 19:16