I set up a very rudimentary pager, when entering my index.php
page a query of all the records. To do this, I only pass the page number to show the url and reload the same page, which works.
Now I have the need to show and paginate the records by category. I put a select in my index.php and when I choose category I pass category + '1' to another page called categoria.php
where I have the same functions as in my index (I copy how this value happened):
<select id="combo_categorias" name="cat" onchange="location.href='categoria.php?num=' + this.value +'1';">
I control by url the value of the category that is a string, eg: 'shoe shop' + a 1, since the first page to be displayed is always 1.
The issue is that already stopped in categoria.php
, when I have paginada more than 1 page does not work, I can pass the value of the page .. but the value of the category is lost and I can not find the way to solve it.
Copy the code:
<?php
$variable=" ";
$rest=" ";
$restvar=" ";
//Si lo recibido por GET
if (isset($_GET['num'])) {
$variable=$_GET['num'];
}
//Saco del GET el último dígito
$rest=substr($variable, -1);
//Saco todo menos el último dígito
$restvar=substr($variable, 0, -1);
//Convierto lo sustraído en la variable
$variable=$restvar;
// $_SESSION["cat_elegida"]=$variable;
?>
<div class="pagina">
<?php include ("cabecera.php"); ?>
<div class="contenedor">
<div class="productos">
<?php
$consulta="select * from productos where categoria LIKE '$variable' ORDER BY id ASC";
$result = mysqli_query($con, $consulta);
$nro_reg = mysqli_num_rows($result);
if ($nro_reg==0) {
?>
<div class="superado_stock">
<?php
echo "<h4> </h4>";
echo "<h4><strong>Producto temporalmente agotado</strong></h4>";
echo "<h4> </h4>";
?>
</div>
<?php
}
$reg_por_pagina=12;
if (isset($_GET['num'])) {
$nro_pagina=$rest;
var_dump($nro_pagina);
var_dump($variable);
} else {
$nro_pagina=1;
}
if (is_numeric($nro_pagina))
$inicio=(($nro_pagina-1)*$reg_por_pagina);
else
$inicio=0;
$consulta=mysqli_query($con,"select * from productos where categoria LIKE '$variable' order by id ASC limit $inicio, $reg_por_pagina");
$canti_paginas=ceil($nro_reg/$reg_por_pagina);
?>
<?php
while ($filas=mysqli_fetch_array($consulta, MYSQLI_ASSOC)) {
echo mysql_error();
$id=$filas['id'];
$imagen=$filas['imagen'];
$nombre=$filas['nombre'];
$descripcion=$filas['descripcion'];
$precio=$filas['precio'];
$stock=$filas['stock'];
$fecha=$filas['fecha'];
if($imagen=="No disponible"){
$imagen="IMAGENES/ImagenNoDisponible100.gif";
}
?>
<div class="caja">
<div class="caja_imagen"><img src="<?php echo $imagen?>" style=widht="100" height="100"/>
<p class="caja_nombre"><?php echo $nombre?></p>
<p class="caja_precio">$ <?php echo $precio?></p>
</div>
<div class="caja_boton">
<form action="detalle.php" method="post" name="detalle">
<input name="id" type="hidden" value="<?php echo $id ?>" />
<input class="boton_detalle2" type="submit" value="Detalle">
</form>
</div>
</div>
<?php
}
?>
</div>
</div>
<!-------------------------------------------------------------------------------------------->
<!-----------------------------PAGINADOR------------------------------------------------------>
<!-------------------------------------------------------------------------------------------->
<div id="paginador" align="center">
<?php
if ($nro_pagina>1)
echo "<a href='categoria.php?num=".($nro_pagina-1) ."' >Anterior</a> ";
for ($i=1;$i<=$canti_paginas;$i++){
if ($i==$nro_pagina)
echo "<span>$i </span>" ;
else
echo "<a href='categoria.php?num=$i'>$i</a> ";
}
if ($nro_pagina<$canti_paginas)
echo "<a href='categoria.php?num=". ($nro_pagina+1) ."' >Siguiente</a> ";
?>
</div>
<!-------------------------------------------------------------------------------------------->
<!--------------------------FIN DEL PAGINADOR------------------------------------------------->
<!-------------------------------------------------------------------------------------------->
</div>
</div>
Maybe I could pass 2 values x url, but I do not succeed.