Pagination error removing number displayed on link

-1

** The pagination works perfectly, but when I do the following: I change everything in good pagination, it gives me a url parameter equal to: localhost / index.php? page = 1 if I remove the page number and I just leave it = it gives me an error (Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean given or in the while parameter ($ row = mysqli_fetch_assoc ($ result_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>**
    
asked by anonymous 23.08.2018 / 19:08

1 answer

0

It seems that if you remove the value of the page parameter your variable will be coming empty, because in $ page = (isset ($ _GET ['page']))? $ _GET ['page']: 1; is validated if the parameter exists but is not validated what has in it, ie page = 1 and page = will fall in the isset because in both cases will exist the parameter. In $ initial = ($ amount_pg * $ page) - $ amount_pg; it is multiplying the $ amount_pg by a theoretically null value that is the $ page and so when trying to make the page it is popping 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 both conditions are true it will pick up the page that came in the parameter, otherwise it will set the page to 1 by default.

    
23.08.2018 / 19:21