Disable paging link if already on the corresponding page

Asked

Viewed 32 times

0

I have the code below and wanted to add the class disabled when the user is on the page he clicked on. For example: if the user clicks on page 3 the link on page 3 would have the class disabled so that it is not possible to click 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>';
                }

1 answer

1


Try it like 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>';
        }
    }
}

That one for can get better. Notice the image that you have 2 repeat blocks using the same evaluation:

inserir a descrição da imagem aqui

It is better to put him out of this evaluation, because it is common for both cases, see:

for($i=$pc;$i<=$pc+5;$i++) {
    // ---------------------inicio Bloco A --------------
    echo '<li><a href="?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.

  • Hello Andrei, it didn’t work, it didn’t insert the class. I think it had to be a query based on the parameter "pag=i" because it would put it in the right place

  • @Fredericomoreira sorry, I used the wrong for the view.

  • I tried at all. and did not give kkkk. I switched all for in part $i==$pc for $i==$pag and it worked

  • @Fredericomoreira try now.

  • @Fredericomoreira on the second loop

  • @Fredericomoreira has some redundancies in this for that I will try to help you correct.

  • what I could improve on this for

  • 1

    I did that and really wiped the code. Thank you

  • 1

    Cool @Fredericomoreira was happy to help! Hug!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.