I answered here what I had already put in a duplicate question , and deleted it from there .
Well, what I realize is that when the person changes pages, there are no longer any variables of $_POST
, so the query has no way to execute.
Or you change from POST
to GET
and include the query data in the links, or use POST
to change pages as well, or you'll need to rethink this logic, whether using session variables or some other technique to preserve the results.
Here is a possible alternative, using POST
for everything. Note at the end, in the paging part, how the original form fields were passed along with the desired page:
<?php
include("conectar.php");
$quantidade = 1;
$pagina = (isset($_POST['pagina'])) ? (int)$_POST['pagina'] : 1;
$inicio = $quantidade * $pagina - $quantidade;
$where = "";
$estado = ''.@$_POST['estado'];
$distrito = ''.@$_POST['distrito'];
$concelho = ''.@$_POST['concelho'];
switch([$estado, $distrito, $concelho])
{
case ['Indiferente','Aveiro','Indiferente']:
$where= "Where tb_trabalhador.Distrito = 'Aveiro'";
break;
case ['Indiferente','Aveiro','Agueda']:
$where= "WHERE tb_trabalhador.Distrito = 'Aveiro' AND
tb_trabalhador.Concelho = 'Agueda'";
break;
}
$sql = "select * from tb_detalhe_trabalhador inner join tb_trabalhador on tb_detalhe_trabalhador.id = tb_trabalhador.id inner join tb_equipamentos on tb_detalhe_trabalhador.id = tb_equipamentos.id $where ORDER BY tb_trabalhador.id asc LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die(mysql_error());
while($exibe = mysql_fetch_array($qr)){
echo "<table>";
echo "<tr><td>Nome:</td>";
echo "<td>".$exibe["Nome"]."</td></tr>";
}
$sqltotal = "SELECT id FROM tb_trabalhador";
$qrtotal = mysql_query($sqltotal) or die(mysql_error());
$numtotal = mysql_num_rows($qrtotal);
$totalpagina = ceil ($numtotal/$quantidade);
echo '<form>';
echo '<input type="hidden" name="estado" value="'.htmlentities($estado).'">';
echo '<input type="hidden" name="distrito" value="'.htmlentities($distrito).'">';
echo '<input type="hidden" name="concelho" value="'.htmlentities($concelho).'">';
for ($i = 1; $i <= $totalpagina; $i++){
if($i == $pagina)
echo $i;
else
echo '<input type="submit" name="pagina" value="'.$i.'">';
}
echo '</form>';
?>
Remember to adjust the source form to use the same casing of characters in the% of% of inputs. I passed everything to lowercase.
There are other problems, such as lack of optimization, and also the use of unsafe functions name=""
instead of mysql_
, but a search in SOpt or the internet as a whole can give more details about this. >
In addition you could have a sql only, and include the mysqli_
condition of "Aveiro" as a variable. What's more, if you use a collation case insensitive (which is usually the default), MySQL will find "Aveiro", "AvEiRo", "AVEIRO"
I assume you're using 1 as a quantity for testing purposes only.
Another detail: to count the records, just use WHERE
, do not need SELECT COUNT(*) FROM tabela
.
Of curiosity, an optimization for the code, if it were to stay as it is, that would take the whole switch and leave only these two lines in place:
$where= $distrito=='aveiro'?" WHERE tb_trabalhador.Distrito = 'Aveiro' ":"";
$sql = "select * from tb_detalhe_trabalhador inner join tb_trabalhador on tb_detalhe_trabalhador.id = tb_trabalhador.id inner join tb_equipamentos on tb_detalhe_trabalhador.id = tb_equipamentos.id $where ORDER BY tb_trabalhador.id asc LIMIT $inicio, $quantidade";