1
\$\begingroup\$
$pageUN = $conn->query("SELECT slug_link FROM bn_publicacao WHERE entry_type = 'pagina' and id = 2")->fetchAll();

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


$stmtUN = "SELECT * FROM bn_publicacao WHERE entry_type = 'post' ORDER BY id DESC LIMIT $inicio, $maximo";
$pUN    = $conn->query($stmtUN)->fetchAll();

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

<h3><a href="index.php?post=<?php echo htmlentities($UN['slug_link'], \ENT_QUOTES, 'UTF-8', false); ?>"><?php echo htmlentities($UN['title'], \ENT_QUOTES, 'UTF-8', false); ?></a></h3>

<?php endforeach; ?>


$stmtPG = $conn->prepare("SELECT * FROM bn_publicacao WHERE entry_type = 'post'");
$stmtPG->execute();
$total = $stmtPG->rowCount();
$total_paginas = ceil($total/$maximo);

if($total > $maximo){

echo '<nav aria-label="Page navigation example">';

  echo '<ul class="pagination justify-content-center">';

    echo '<li class="page-item">';

      foreach($pageUN as $PUN){

        echo '<a class="page-link text-secondary" href="?p=';

        echo htmlentities($PUN['slug_link'], \ENT_QUOTES, "UTF-8", false);

        echo '&pagina=1">Primeira</a>';

      }

    echo '</li>';


for ($i = $pagina - $maxlinks; $i <= $pagina -1; $i++) { 

  if ($i >= 1) {

    echo '<li class="page-item">';

      foreach($pageUN as $PUN) {

        echo '<a class="page-link text-secondary" href="?p=';

        echo htmlentities($PUN["slug_link"], \ENT_QUOTES, "UTF-8", false);

        echo '&pagina='.$i.'">'.$i.'</a>';

      }

    echo '</li>';

  }
}

echo '<a class="page-link text-danger">'.$pagina.'</a>';
for ($i= $pagina +1; $i <= $pagina + $maxlinks; $i++) { 
  if ($i <= $total_paginas) {

    echo '<li class="page-item">';

      foreach($pageUN as $PUN) {

        echo '<a class="page-link text-secondary" href="?p=';

        echo htmlentities($PUN["slug_link"], \ENT_QUOTES, "UTF-8", false);

        echo '&pagina='.$i.'">'.$i.'</a>';

      }

    echo '</li>';

  }
}

echo '<li class="page-item">';

  foreach($pageUN as $PUN) {

    echo '<a  class="page-link" href="?p=';

    echo htmlentities($PUN["slug_link"], \ENT_QUOTES, "UTF-8", false);

    echo '&pagina='.$total_paginas.'">Última</a>';

    }

  echo '</li>';

echo '</nav>'; 

}

$conn = null;

I want to know if my pagination is safe and if it's not, where's the problems and what can i do to make it safe.

What you guys think?

Edit:

 $stmtPG = $conn->query("SELECT count(*) FROM bn_publicacao WHERE entry_type = 'post'");
 $stmtPG->execute();
 $total = $stmtPG->fetchColumn();
 $total_paginas = ceil($total/$maximo);
\$\endgroup\$
  • \$\begingroup\$ The way you are getting the number of rows it terrible performance-wise and could be potentially dangerous as it could bring your site down.Instead of selecting all rows only to count them, always ask your database to count for you \$\endgroup\$ – Your Common Sense Sep 24 '18 at 7:43
  • \$\begingroup\$ regarding your edit: you don't have to prepare constant values. it's OK to write 'post' in the query as long as it's a constant value, not a variable \$\endgroup\$ – Your Common Sense Sep 24 '18 at 9:09
  • \$\begingroup\$ I'm not very happy with your choice of variable names. They don't really say what is contained in the variable. At the start you have $maxlinks, $pagina, $maximo and $inicio. It's up to us to find out what they contain, because the names are not all that helpful. I assume $maxlinks to be the number of items on a page. $pagina is the page number you're on. But what would $maximo represent? No idea. You lost me there. And it gets worse after that. What is $pageUN or $i? What I am trying to say is: Choose variable names with care, it can improve readability greatly. \$\endgroup\$ – KIKO Software Sep 24 '18 at 10:21
  • \$\begingroup\$ @YourCommonSense I made some changes on my code accordance with what you said, my code is ok now? \$\endgroup\$ – Susi Sep 24 '18 at 15:05
  • \$\begingroup\$ Unfortunately, you made it either the same or worse. \$\endgroup\$ – Your Common Sense Sep 24 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.