Pagination error while removing number displayed in link

Asked

Viewed 32 times

-1

**The pagination works perfectly, but when I do the following case: I change the pagination everything in good, it gives me a url parameter equal to: localhost/index.php? page=1 if I remove the pagination number and leave only = it gives me an error (Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, Boolean Given or while($Row = mysqli_fetch_assoc($resultado_players)){

$pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1;
$result_player = "SELECT * FROM player";
$resultado_player = mysqli_query($conn, $result_player);
$total_players = mysqli_num_rows($resultado_player);
$quantidade_pg = 3;
$num_pagina = ceil($total_players/$quantidade_pg);
$incio = ($quantidade_pg*$pagina)-$quantidade_pg;
$result_players = "SELECT * FROM player limit $incio, $quantidade_pg";
$resultado_players = mysqli_query($conn, $result_players);
$total_players = mysqli_num_rows($resultado_players);

$sqs = mysqli_query($conn, "SELECT * FROM user WHERE id='1'");

$sql = mysqli_query($conn, "SELECT * FROM config WHERE id='1'");

$sda = mysqli_fetch_assoc($sqs);

$sdl = mysqli_fetch_assoc($sql);

?>

                                            $lim = 1;
                                            $inicio = ((($pagina - $lim) > 1) ? $pagina - $lim : 1);
                                            $fim = ((($pagina+$lim) < $num_pagina) ? $pagina+$lim : $num_pagina);
                                            if($num_pagina > 1 && $pagina <= $num_pagina){
                                            }
                                        ?>
                                        <nav class="text-center">
                                            <ul class="pagination">
                                                <li>
                                                <?php
                                                    if($pagina_anterior != 0){ ?>
                                                    <a href="index.php?pagina=<?php echo $pagina_anterior; ?>" aria-label="Previous">
                                                    <span aria-hidden="true"><i class="fas fa-chevron-left"></i></span>
                                                    </a>
                                                <?php }else{ ?>
                                                    <span aria-hidden="true"><i class="fas fa-chevron-left"></i></span>
                                                <?php }  ?>
                                                </li>
                                                <?php 
                                                    for($i = $inicio; $i <= $fim; $i++){ ?>
                                                    <li><a href="index.php?pagina=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                                                <?php } ?>
                                                <li>
                                                <?php
                                                    if($pagina_posterior <= $num_pagina){ ?>
                                                    <a href="index.php?pagina=<?php echo $pagina_posterior; ?>" aria-label="Previous">
                                                    <span aria-hidden="true"><i class="fas fa-chevron-right"></i></span>
                                                </a>
                                                <?php }else{ ?>
                                                <span aria-hidden="true"><i class="fas fa-chevron-right"></i></span>
                                                <?php }  ?>
                                                </li>
                                            </ul>
                                        </nav>**

  • You do not need to edit the question by saying it has been solved. Just the fact that you have accepted an answer already indicates that the problem has been solved.

1 answer

0


From what it seems if you remove the value of the page parameter your variable will be coming empty, because in $pagina = (isset($_GET['pagina']))? $_GET['page'] : 1; is validated if there is the parameter but is not validated what has in it, ie page=1 and page= will fall in isset because in both cases will exist the parameter. In $incio = ($quantidade_pg*$pagina)-$quantidade_pg; it is multiplying $quantidade_pg by a theoretically null value that is $pagina and so when trying to page it is bursting the error in mysql.

what can be done is as follows:

$pagina = (isset($_GET['pagina']) && !empty($_GET['pagina']))? $_GET['pagina'] : 1;

In the above code it is validated if the page parameter is coming in the URL and if it is not empty. If the two conditions are true it will take the page that came in the parameter, otherwise it will set the page as 1 by default.

  • sorry, I have little knowledge on the subject, could help me in relation to this

  • @Rafaelbarros updated the answer.

  • thank you very much, it worked right, I do not have much knowledge about such area, so I end up confused...

Browser other questions tagged

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