Why is my localhost adding an extra bar?

Asked

Viewed 45 times

2

trait Pagination{

    private $totalRegistros;
    private $limite;
    private $offset;
    private $totalPaginas;
    private $segmentoUrl;
    private $links;
    private $explodeBarrasUrl;
    private $posicaoPaginaUrl;

    static $nextLabel = '>>';
    static $prevLabel = '<<';

    public function setTotalRegistros($totalRegistros){

        $this->totalRegistros = $totalRegistros;

    }

    public function setLimite($limite){

        //Limita o número de dados do banco a serem mostrados por página
        $this->limite = $limite;

    }

    private function encontrarPagNaUrl(){

        $this->explodeBarrasUrl = explode('/', $_SERVER['REQUEST_URI']);
        $this->posicaoPaginaUrl = array_search('pag', $this->explodeBarrasUrl);
    }

    private function paginaAtual(){

        $this->encontrarPagNaUrl();
        return (!$this->posicaoPaginaUrl) ? 1 : $this->explodeBarrasUrl[$this->posicaoPaginaUrl+1];

    }

    private function offset(){

        return $this->offset = ($this->paginaAtual() * $this->limite) - $this->limite;

    }

    private function totalPaginas(){

        return $this->totalPaginas = ceil($this->totalRegistros / $this->limite);

    }

    private function nextLink(){

        if ($this->paginaAtual() < (int)$this->totalPaginas()){
        return '<a href="http://' . $_SERVER['HTTP_HOST'] . '/' . $this->segmentoUrl . 'pag/' . ($this->paginaAtual()+1) . '">' . self::$nextLabel . '</a>';

        }

    }

    private function prevLink(){//vou sair ja voltook

        if ($this->paginaAtual() <= (int)$this->totalPaginas() && $this->paginaAtual() > 1){
        return '<a href="http://' . $_SERVER['HTTP_HOST']. '/' . $this->segmentoUrl . 'pag/' . ($this->paginaAtual()-1) . '">' . self::$prevLabel . '</a>';

        }

    }

    private function temPagUrl(){
        for ($i=0; $i < $this->posicaoPaginaUrl; $i++) {
        $this->segmentoUrl .= $this->explodeBarrasUrl[$i].'/';
        }
        $this->segmentoUrl = ltrim($this->segmentoUrl, '/');
    }


    private function naoTemPagUrl(){
        $this->segmentoUrl = $_SERVER['REQUEST_URI'].'/';
    }

    private function segmentoUrl(){

        $numeroBarrasUrl = substr_count($_SERVER['REQUEST_URI'], '/');

        if ($numeroBarrasUrl > 1) {

        $this->encontrarPagNaUrl();

        (!$this->posicaoPaginaUrl) ? $this->naoTemPagUrl() : $this->temPagUrl();

        } else {

        $this->segmentoUrl = $_SERVER['REQUEST_URI'].'/';
        $this->segmentoUrl = ltrim($this->segmentoUrl);
        }

    }

    public function render(){

        $this->segmentoUrl();
        $this->links .= '<div>';
        $this->links .= '<ul class="pagination">';
        $this->links .= '<li>' . $this->prevLink() . '</li>';
        for ($i=1; $i <= $this->totalPaginas(); $i++){
            $this->links .= '<li' . ($i == $this->paginaAtual() ? ' class="active"' : '') . '><a href="http://'.$_SERVER['HTTP_HOST'].$this->segmentoUrl.'pag/'.$i.'">'. $i . '</a></li>';

        }

        $this->links .= '<li>' . $this->nextLink() . '</li>';
        $this->links .= '</ul>';
        $this->links .= '</div>';
        return $this->links;

    }

    public function paginate($sql, $paginate = null) {

        return (is_null($paginate)) ? $sql : $sql . ' LIMIT ' . $this->limite . ' OFFSET ' . $this->offset();
    }   
}

I created this Trait to paginate a project, but the function render() is returning me the following link on the buttons

http:localhost:8080//admin_listar_cursos/pag/1 http:localhost:8080//admin_listar_cursos/pag/2

For some reason he’s adding an extra bar after the host, so when I click the button, he can’t find the page.

  • 1

    Take a look at what REQUEST_URI returns. I think it returns /minha/url. When you do '<a href="http://' . $_SERVER['HTTP_HOST']. '/' . $this->segmentoUrl..., segmentoUrl() returns /minha/url, that you are concatenating with $_SERVER['HTTP_HOST']. '/'.

  • Thank you, Marcel Alves, for your answer. Yesterday I was seeing with a friend and I realized that the problem occurred right there, but we could not determine why... However, I noticed that the error only happens when we do not directly access the http://localhost:8080/admin_listar_cursos/pag/1... When accessing this page directly, there is no error... Summarizing. I answered the problem, asking that when clicking List, in the administrative panel menu, it went straight to pag/1 which is the first page of the database...

No answers

Browser other questions tagged

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