Switch with several POST

Asked

Viewed 273 times

-1

I have a question about the Switch .

I want to put several options for a Switch and currently I have so and 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;

2 answers

2


The way you wish I know no method, but with a little creativity (and gambiarra) there is a way.

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

I hope I’ve helped.

0

In fact the switch is indicated to validate the value of a only 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.

But you can make a loop validating all keys and so could be used 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 it doesn’t indicate this second way because then you would be doing an interaction with the array and validating with the switch, where we usually use more than one value of an array for processing or anything else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.