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>
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.
– Bruno Leyne
I added images to improve the question, with the page parameter corrected, the GET page refers to the "page=" in the pagination
– user150548