Switch with several POST [duplicate]

-1

I have a question about Switch.

I want to put several options for a Switch and now I have it and it does not work:

    if (isset($_POST['estado'])&& ($_POST['Distrito']))
{
switch($_POST['estado'] && $_POST['Distrito'])
{
case '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':
$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 tb_trabalhador.Distrito = 'Aveiro' or 'AVEIRO' or 'aveiro' ORDER BY tb_trabalhador.id asc LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die(mysql_error());
break;
    
asked by anonymous 27.03.2014 / 11:55

2 answers

2

The way you want it, I do not know any method, but with a bit of creativity (and gambiarra) there is a way.

switch ( [$_POST["estado"], $_POST["distrito"]] ) {
    case ['Indiferente', 'Indiferente']:
        // ...
    break;
    // ...
}

I hope I have helped.

    
27.03.2014 / 12:23
0

Actually switch is indicated to validate the value of a unique variable. Where you can see it like this

$var = 1;
switch ($var) {
    case 1: echo 'Um'; break;
    case 2: echo 'Dois'; break;
    ...
}

In your case it really has to be the conditions, where you validate several values of the same variable, as in the case of $_POST which is a Array .

However, you can loop all the keys and validate a switch.

$_POST = array('teste' => 'teste', 'teste1' => 'teste1', 'teste2' => 'teste2');
foreach ($_POST as $key => $val) {
    switch ($key) {
        case 'teste': echo 'Testando'; break;
        case 'teste1': echo 'Testando novamente'; break;
        case 'teste2': echo 'Mais um teste'; break;
    }
}

Although I did not indicate this second form, then you would be interacting with the array and validating with the switch, where we usually use more than one array value for processing or something else.

    
27.03.2014 / 12:27