Switch Result

-5
<?php
include("conectar.php");

$quantidade = 1;
$pagina = (isset($_GET ['pagina'])) ? (int) $_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina) - $quantidade;
$where = "";
$estado = '' . @$_POST['estado'];
$distrito = '' . @$_POST['distrito'];
$concelho = '' . @$_POST['concelho'];

switch ([$estado, $distrito, $concelho]) {

    Case ['Indiferente', 'Indiferente', 'Indiferente']:
        break;
    case ['Indiferente', 'Aveiro', 'Indiferete']:
        $where = "WHERE tb_trabalhador.Distrito = 'Aveiro' ";
        break;
    case ['Indiferente', 'Aveiro', 'Agueda']:
        $where = "WHERE tb_trabalhador.distrito = 'Aveiro' AND tb_trabalhador.concelho = 'Agueda'";
        break;
}


//Aqui tenho a dúvida pois nem sempre quero utilizar $SQL. Se for ['autorizado','Indiferente','Indiferente'] Já tem uma query diferente. Como posso fazer essa distinção? 


$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>";

    echo "</table>";
}

$sqltotal = "SELECT id FROM tb_trabalhador $where";
$qrtotal = mysql_query($sqltotal) or die(mysql_error());
$numtotal = mysql_num_rows($qrtotal);
$totalpagina = ceil($numtotal / $quantidade);

echo '<a href="?pagina=1">Primeira página</a>';

for ($i = 1; $i <= $totalpagina; $i++) {
    if ($i == $pagina)
        echo $i;
    else
        echo"<a href=\"?pagina=$i\">$i</a>";
}

echo '<a href="?pagina=$totalpagina">Ultima Pagina</a>';
?>
    
asked by anonymous 02.04.2014 / 15:07

3 answers

1

You can not put code that is not relative to switch within switch because this is bad programming practice. Another thing is to set $sql before even entering switch , because nothing guarantees that $sql will be set inside switch .

The correct way is to use this:

$quantidade = 1;
$pagina = (isset($_GET ['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina) - $quantidade;
$sql = "";

if (isset($_POST['estado']) AND ($_POST['Distrito']) AND ($_POST['Concelho']))
{
    switch([$_POST['estado'] , $_POST['Distrito'], $_POST['Concelho']])
    {
        case ['Indiferente','Indiferente','Indiferente']:
            $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 ORDER BY tb_trabalhador.id asc LIMIT $inicio, $quantidade";
            $qr = mysql_query($sql) or die(mysql_error());
            break;
        case ['Indiferente','Aveiro','Indiferente']:
            $sql = "(Query) asc LIMIT $inicio, $quantidade";
            $qr = mysql_query($sql) or die(mysql_error());
            break;
        //(Varios cases)
    }

    $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>";
        //...
    }
}
    
02.04.2014 / 18:02
3
  

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";
    
08.04.2014 / 16:53
0

I came here because of a duplicate: Switch with several POST

I'll share something I have not seen in any reply or comment. It is a more flexible technique that allows conditional structures:

$foo = 1;
$bar = 2;
switch(true)
{
    case ($foo > $bar):
        echo 'foo maior que bar';
    break;
    case ($foo < $bar):
        echo 'foo menor que bar';
    break;
    case ($foo == $bar):
        echo 'foo igual a bar';
    break;
}
    
17.12.2015 / 00:47