Paging system not working - SERVER SIDE

Asked

Viewed 39 times

1

This is my file called index.php:

<?php  

$maxlinks = 4;
$paginaAtual = (isset($_GET['paginaAtual'])) ? (int)$_GET['paginaAtual'] : 1;
$maximo = 5;
$inicio = (($maximo * $paginaAtual) - $maximo);

$publicacoesUN = DBRead('publicacao', "ORDER BY id DESC LIMIT $inicio, $maximo");

$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

if ($post != '' || ($post == '' && $pagina != '')) {

    switch ($pagina):
    case 'home':
        $titulo = '';
        $shareTitulo = '';

        $descricao = '';
        $shareDescricao = '';

        $shareImg = '';
        $keywords = '';

        $ogUrl = '';
        $urlCanonico = '';
        break;

    case 'ultimasnoticias':
        $titulo = '';
        $shareTitulo = '';

        $descricao = '';
        $shareDescricao = '';

        $shareImg = '';
        $keywords = '';

        $ogUrl = '';
        $urlCanonico = '';
        break;

    default:
        $titulo = 'Home';
        $pagina = 'home';
    endswitch;

}

?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>

<nav>

    <ul>
        <li>
          <a href="?p=home">Página Inicial</a>
        </li>

        <li>
          <a href="?p=ultimasnoticias">Últimas Notícias</a>
        </li>
    </ul>

</nav>

    <?php
        if (empty($post)) {
        require_once 'page_' . $pagina . '.php';
        } else {
            require_once 'posts/' . $post . '.php';
        }
    ?>

</body>
</html>

This is my file called ultimasnoticias.php:

<div class="container my-3">

    <div class="row">

        <?php foreach ($publicacoesUN as $UN): ?>

           <div class="col-12 col-md-6 col-lg-3 mb-3 mb-md-3">

                <div class="card">

                    <div class="img-container">

                      <a href="index.php?post=<?php echo $UN['title']?>"><img src="<?php echo $UN['capa']?>" alt="<?php echo $UN['alt']?>" class="card-img-top" id="imgUNcover"></a>

                    </div>

                    <div class="card-body">

                      <a href="index.php?post=<?php echo $UN['title']?>" class="card-title cardTitleLink"><h1 class="cardTitleUN"><?php echo $UN['title']?></h1></a>

                      <p class="card-text text-muted"><?php echo $UN['text']?></p>

                      <a href="index.php?post=<?php echo $UN['title']?>" class="btn btn-outline-danger btn-sm">Continue Lendo</a>

                    </div>

                </div>

           </div>

       <?php endforeach; ?>

    </div>

</div>

<?php

  $pdo = new PDO('mysql:host=localhost;dbname=publicacoes', 'root', '');

  $seleciona_2 = $pdo->prepare("SELECT * FROM `bn_publicacao`");
  $seleciona_2->execute();
  $total = $seleciona_2->rowCount();
  $total_paginas = ceil($total/$maximo);

  if($total > $maximo){

  echo '<a href="?paginaAtual=1">First page</a>';
  for ($i = $paginaAtual - $maxlinks; $i <= $paginaAtual -1; $i++) { 

    if ($i >= 1) {
      echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<span>'.$paginaAtual.'</span>';
  for ($i= $paginaAtual +1; $i <= $paginaAtual + $maxlinks; $i++) { 
    if ($i <= $total_paginas) {
      echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<a href="?paginaAtual='.$total_paginas.'">Last page</a>';

  }

?>

When I click on the pagination links, 1, 2, 3, first page, last page.. etc, the link redirects me to the main page, which in this case is page_home.php.

If I put the script of the file ultimasnoticias.php inside the index.php file the pagination works perfectly. I mean, for a static site this paging would work, but my site is server side and it’s not working, and I couldn’t find a solution.

Does anyone know how to solve?

1 answer

2


Fera, I think we’re missing the querystring "p" in the pagination links.

<?php

  $pdo = new PDO('mysql:host=localhost;dbname=publicacoes', 'root', '');

  $seleciona_2 = $pdo->prepare("SELECT * FROM `bn_publicacao`");
  $seleciona_2->execute();
  $total = $seleciona_2->rowCount();
  $total_paginas = ceil($total/$maximo);

  if($total > $maximo){

  echo '<a href="?p=ultimasnoticias&paginaAtual=1">First page</a>';
  for ($i = $paginaAtual - $maxlinks; $i <= $paginaAtual -1; $i++) { 

    if ($i >= 1) {
      echo '<a href="?p=ultimasnoticias&paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<span>'.$paginaAtual.'</span>';
  for ($i= $paginaAtual +1; $i <= $paginaAtual + $maxlinks; $i++) { 
    if ($i <= $total_paginas) {
      echo '<a href="?p=ultimasnoticias&paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<a href="?p=ultimasnoticias&paginaAtual='.$total_paginas.'">Last page</a>';

  }

?>
  • I was suspecting that the links were wrong anyway, but had failed to get it right. Thank you very much.

Browser other questions tagged

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