Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
How to find the first checkbox checked in a serie of checkboxs

I have 5 checkbox variables.

In order to avoid the problem of "undefined index" when checkbox was unchecked I've used this:

<input value="" name="checkbox1">
<input value="C1" name="checkbox1">
<input value="" name="checkbox2">
<input value="C2" name="checkbox2">
<input value="" name="checkbox3">
<input value="C3" name="checkbox3">
<input value="" name="checkbox4">
<input value="C4" name="checkbox4">
<input value="" name="checkbox5">
<input value="C5" name="checkbox5">

$col1 = $_POST['checkbox1'];
$col2 = $_POST['checkbox2'];
$col3 = $_POST['checkbox3'];
$col4 = $_POST['checkbox4'];
$col5 = $_POST['checkbox5']; 

Now, I would like to write a query like this:

$query = "Select * from users where 1=1 ";

if($_POST['checkbox1'] !=="") {
$query.="And/OR col1='$col1'"; 
}
if($_POST['checkbox2'] !=="") {
$query.="And/OR col2='$col2'";
}
if($_POST['checkbox3'] !=="") {
$query.="And/OR col3='$col3'";
}
if($_POST['checkbox4'] !=="") {
$query.="And/OR col4='$col4'";
}
if($_POST['checkbox5'] !=="") {
$query.="And/OR col5='$col5'";
}

The problem is with the "And/OR".

If happend that the first checkbox was selected then it should be "And" if it was the second from those checkboxs chosen then "OR".Any idea's how to perform this?

For example: If $col2 checkbox was the first to be chosen and the user also chose $col4 checkbox then the $query should be:

"Select * users where 1=1 AND col2=$col2 OR col4=$col4"
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do like:

    $query = "Select * from users where 1=1 ";
    $chosen = false;
    if($_POST['checkbox1'] !=="") {
        $query.="And col1='$col1'"; 
        $chosen = true;
    }
    if($_POST['checkbox2'] !=="") {
        $query.= (($chosen) ? "OR" : "And") . " col2='$col2'";
        $chosen = true;
    }
    if($_POST['checkbox3'] !=="") {
        $query.= (($chosen) ? "OR" : "And") . " col3='$col3'";
        $chosen = true;
    }
    ...........................
over 4 years ago · Santiago Trujillo Relatório

0

try this:

$query = "Select * from users where 1=1 ";
$where = "";
$where_arr = array();
if($_POST['checkbox1'] !=="") {
$where_arr[]=" col1='$col1'"; 
}
if($_POST['checkbox2'] !=="") {
$where_arr[]=" col2='$col2'";
}
if($_POST['checkbox3'] !=="") {
$where_arr[]=" col3='$col3'";
}
if($_POST['checkbox4'] !=="") {
$where_arr[]=" col4='$col4'";
}
if($_POST['checkbox5'] !=="") {
$where_arr[]=" col5='$col5'";
}

if(count($where_arr))
$where = " AND ".implode(" OR ", $where_arr);

$query .= $where;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda