Paging a Dynamic Selectbox in different php files

Asked

Viewed 137 times

0

Good afternoon, I am facing a problem in creating a pagination.

Javascript

    <script type="text/javascript">
        $(document).ready(function(){
            $('#estados').on('change', function(e){
                var estado = document.getElementById('estados')
                var estadovl = document.getElementById('estadovl')
                estadovl.value = estado.options[estado.selectedIndex].value
            });
        });
</script>

HTML - Page 1

<div class="container">
    <div class="row">
        <div class="col-xs-4 col-xs-offset-4"> 
            <h1>Cidade Escolha</h1>
            <form class="text-center" method="get" enctype="application/x-www-form-urlencoded" id="form1" action='cidades.php'>
                <div class="form-group">

                    <select class="form-control" id="estados" name="estados">
                        <option>Escolha seu Estado</option>
                        <?php 
                            $query = "select * from estados;";
                            $dt = mysqli_query($conn, $query);
                            while ($row = mysqli_fetch_array($dt)){
                            ?>
                            <option value="<?php echo $row['estado_id'];?>"><?php echo $row['nome'];?></option>
                            <?php }; ?>

                   <input type="text" id="estadovl" name="estadovl" hidden="hidden">
                 </select>

                    <input type="submit" name="submit" id="submit" placeholder="enviar">
                </div>
            </form>
        </div>
    </div>  
</div>






    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>



    <script src="js/bootstrap.min.js"></script>                   

Already connected with the database, on the first page the client will choose a Brazilian state (states are stored in mysql), the value of the option will be played to the <input type="text" id="estadovl" name="estadovl" hidden="hidden"> as mentioned in the javascript code and will be redirected to page 2.

PHP - Page 2

include ('conection.php'); 

/*if(isset($_GET['page'])){
    $page = $_GET['page'];
}else{
    $page = 1;
}*/

$numpag = 10;
$pagina = (isset( $_GET['pagina'])) ? $_GET['pagina'] : 1;
$inicio = $pagina * $numpag;




$query = "SELECT * FROM cidades WHERE estado_id = ".$_GET['estadovl']." AND status = 1 ORDER BY nome ASC LIMIT $inicio,$numpag";
$dt = mysqli_query($conn, $query);
$rowcount = mysqli_num_rows($dt);

On this page, he will find all the cities belonging to the chosen state. Cities (which are also stored in the database) are shown in the above code. It turns out I have to make a pagination limiting 10 cities per page, but this "cities.php" doesn’t open as "cities.php? page=1" as changed in variable $pagina = (isset( $_GET['pagina'])) ? $_GET['pagina'] : 1;, in addition, the "span link" states that the state has not been chosen.

<h1>Cidade Escolha</h1>
  <div class="row">
    <?php while($row = mysqli_fetch_array($dt)){?>
      <div class='col-xs-12 col-md-6'>
        <div class='panel panel-default'>
          <div class='panel-title'>
            <h2 style='text-align:center;'><?php echo $row['nome']; ?></h2>
          </div>
          <div class='panel-body'>
            <p><h3>Lorem Ipsum</h3></p>
            <p>Endereço: <?php echo $row['cep'] ?></p>
            <p>Telefone:</p>      
          </div>
        </div> 
      </div>
    <?php } ?>
  </div>
  <div class="pull-left">
    <span>
      <?php
        $pgquery = "SELECT * FROM cidades WHERE estado_id = ".$_GET['estadovl']." AND status = 1 ORDER BY nome ASC;";
        $pgdt = mysqli_query($conn, $pgquery);
        $pgrowcount = mysqli_num_rows($pgdt); 
        $totalpag = ceil($pgrowcount/$numpag);
        for($i = 1; $i<= $totalpag; $i++){
          echo "<a href='cidades.php?page=".$i."'>$i</a>";
        }
      ?>
    </span>
  </div>

Página 1

Página 2

Página Erro - paginações

  • Have the app hosted to see this running? I read the code and it’s not making sense, your javascript part Oce takes the chosen value and does nothing with it. I can’t see the page parameter anywhere in the HTML being sent. You may be getting lost in the Ajax call settings and PHP Code Interpretation.

  • I added images to improve the question, with the page parameter corrected, the GET page refers to the "page=" in the pagination

2 answers

0


Well before anything else I recommend you to do some research on sql Injection

I took a look at your code and I think it might help:

  • Taking into account that when you navigate to the page cidades.php, you already have the value estado by the variable $_GET['estadovl']

In the observation you say that the results are skipping the first 10 records, this is because its variable $inicio begins of 10 and not of 0, change this code on page 2 should already resolve this issue:

// De 
$inicio = $pagina * $numpag;

// Para
$inicio = ( $pagina - 1 ) * $numpag;

About the pagination I advise you to add the status along with the link to the page

// De
echo "<a href='cidades.php?page=".$i."'>$i</a>"; 

// Para
echo "<a href='cidades.php?page=".$i."&estadovl=".$_GET['estadovl']."'>$i</a>"; 
  • I fixed the variable $inicio And the echo you mentioned, it took care of the mistakes. However, all the links in the pages only follow the same information on page 1, that is, changes the URL but not the information. I only touched those codes you told me to fix.

  • Tidied up!! GET and URL names were different

0

Well, from what I see in the prints, the problem is in the states. You have to pass the state parameter to get the $_GET['estadovl']. Your URL has to stay http://.../cities.php? pagina=1&estadovl=MG It’s missing change in this place here too:

  for($i = 1; $i<= $totalpag; $i++){
          echo "<a href='cidades.php?page=".$i."'>$i</a>";
        }

Must be something like:

 for($i = 1; $i<= $totalpag; $i++){
              echo "<a href='cidades.php?page=".$i."'&estadovl=".$_GET['estadovl'].">$i</a>";
            }

Browser other questions tagged

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