Pagination link returning to index

0

I have two pages with pagination, using the same method.

The first page is home and in it the pagination is working perfectly, already in the page that displays the search results the pagination until it works, however when I click on any pagination link the page returns to the home page.

I was kind of lost and I can not find the problem anymore.

I also had difficulty fetching values from more than one table with columns with the same name. Using UNION worked, but if there is anything wrong please alert me.

Page displaying the search result:

<?php
include("mysql_connect.php");
// definir o numero de itens por pagina
$itens_por_pagina = 2;

// pegar a pagina atual
$pagina = intval($_GET['p']);

// Pegando a palavra a ser pesquisada
$busca = trim($_POST['busca']);

// comparar com valores do banco
$sql = "SELECT titulo, descricao, conteudo, url FROM paginas WHERE MATCH (titulo,conteudo,descricao) AGAINST ('%".$busca."%' IN BOOLEAN MODE)
UNION ALL
SELECT titulo, descricao, conteudo, url FROM ultimas_publicacoes WHERE MATCH (titulo,conteudo,descricao) AGAINST ('%".$busca."%' IN BOOLEAN MODE) LIMIT $pagina, $itens_por_pagina";
$execute = $conn->query($sql) or die($conn->error);
$resultado = $execute->fetch_assoc();
$num = $execute->num_rows;

// pega a quantidade total de objetos no banco de dados
$num_total = $conn->query("SELECT titulo, descricao, conteudo, url FROM paginas WHERE MATCH (titulo,conteudo,descricao) AGAINST ('%".$busca."%' IN BOOLEAN MODE)
UNION ALL
SELECT titulo, descricao, conteudo, url FROM ultimas_publicacoes WHERE MATCH (titulo,conteudo,descricao) AGAINST ('%".$busca."%' IN BOOLEAN MODE)")->num_rows;

// definir numero de páginas
$num_paginas = ceil($num_total/$itens_por_pagina);
?>
<section id="main" class="mb-4">
    <div class="container">
        <div class="row">
            <!-- Menu Lateral -->
            <?php require_once("menu_lateral.php"); ?>
            <!-- Conteúdo Principal -->
            <div class="col-12 col-md-9">
                <?php if($num > 0){ ?>
                <section id="main-content">
                    <div class="main-title py-2 pl-3">Resultado da Busca</div>
                    <div class="list-group list-group-flush">
                        <?php do{ ?>
                        <?php echo '<a href="?p='.$resultado['url'].'" class="list-group-item list-group-item-action flex-column align-items-start">'; ?> 
                        <div class="d-flex w-100 justify-content-between">
                            <h5 class="mb-1"><?php echo $resultado['titulo']; ?></h5>
                        </div>
                        <p class="mb-1"><?php echo htmlspecialchars_decode($resultado['descricao']); ?></p>
                        </a>
                        <?php } while($resultado = $execute->fetch_assoc()); ?>
                    </div>
                </section>
                <?php 
                    $nav_hide = "";
                    if ($num_total < $itens_por_pagina) {
                        $nav_hide = "class=\"d-none\"";
                    }
                ?>
                <nav <?php echo $nav_hide; ?> >
                  <ul class="pagination mt-2">
                    <li class="page-item">
                      <a class="page-link" href="?p=busca-0" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                        <span class="sr-only">Previous</span>
                      </a>
                    </li>
                    <?php 
                    for($i=0;$i<$num_paginas;$i++){
                    $estilo = "";
                    if($pagina == $i)
                        $estilo = "class=\"page-item active\"";
                    ?>
                    <li <?php echo $estilo; ?> ><a class="page-link" href="?p=busca-<?php echo $i; ?>"><?php echo $i+1; ?></a></li>
                    <?php } ?>
                    <li class="page-item">
                      <a class="page-link" href="?p=busca-<?php echo $num_paginas-1; ?>" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                        <span class="sr-only">Next</span>
                      </a>
                    </li>
                  </ul>
                </nav>
                <?php } ?>
            </div>
        </div>
    </div>
</section>

Script responsible for navigation:

function carregar_pagina (){
    (isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
        if(file_exists($pagina.'.php')):
            require_once($pagina.'.php');
        else:
            require_once('home.php');
        endif;
}
?>

Index:

// Functions
require_once("functions.php");
// Include Header
require_once("header.php");
// Include Main Content
carregar_pagina();
// Include Footer
require_once("footer.php");
    
asked by anonymous 09.11.2018 / 23:13

0 answers