Make a database query through an array

Asked

Viewed 46 times

0

Hello,

Guys, I got a big question here. I have a query where the user can select if they want to see all(Manager and Managers Jr), Only Managers Jr, or Directly select only one.

  1. All
  2. Only managers Jr
  3. John
  4. Maria
  5. etc....

The problem is when I select "2 - Only Managers Jr".

Because I have to go on the employee register and find out who the junior managers are (colab_func=3), so far so good.

After the previous consultation, where I receive the employee codes, I need to do the consultation itself, where I go in the sales table and look for the sales of these employees. That’s where I’m not getting it.

I’ll put a part of the code here:


$gerente = $_POST["txtgerente"];
if($gerente == 'T'){
    $query = "SELECT * FROM venda_dia WHERE venda_dia_aberto='S'";
    $tipo = "Todos";
} elseif ($gerente == 'GJR'){
    $query_ger = "SELECT colab_id FROM colaborador WHERE colab_funcao in($cod_gerjr)";
    $result_ger = mysqli_query($con, $query_ger) or die(mysqli_error());
    $row_ger = mysqli_fetch_array($result_ger);
    $query = "SELECT * FROM venda_dia WHERE venda_dia_cod_ger in($row_ger) AND venda_dia_aberto='S'";
    $tipo = "Gerente Júnior";
} else {
    $query = "SELECT * FROM venda_dia WHERE venda_dia_cod_ger='$gerente' AND venda_dia_aberto='S'";
    $query_gerente = "SELECT * FROM colaborador WHERE colab_id=$gerente";
    $result_gerente = mysqli_query($con, $query_gerente) or die(mysqli_error());
    $row_gerente = mysqli_fetch_array($result_gerente);
    $tipo = $row_gerente['colab_nome_venda'];
}
?>

<section id="cadastros">
    <fieldset id="cadastros"><legend><?php echo "Expositor com dia aberto - $tipo"; ?></legend>
         <table id="tab" align="center">
             <tr bgcolor="lightblue">
                 <th width="250px">Expositor</th>
                 <th width="150px">Dia Faturamento</th>
                 <th width="250px">Responsável</th>
              </tr>
              <?php 
                   $result = mysqli_query($con, $query) or die(mysqli_error());
                   while ($row = mysqli_fetch_array($result)) {
                        $cod_exp = $row["venda_dia_cod_exp"];
                        $data = $row["venda_dia_data"];
                        $cod_colab = $row["venda_dia_cod_colab"];
                        $cod_colab2 = $row["venda_dia_cod_colab2"];

                        // ESCREVE UMA LINHA NA TABELA
                        echo "<tr>";
                        echo "<td>";
                        $query_expositor = "SELECT * FROM expositor WHERE expo_id=$cod_exp";
                        $result_expositor = mysqli_query($con, $query_expositor) or die(mysqli_error());
                             while ($row_expositor = mysqli_fetch_array($result_expositor)) {
                                 $nome_expo = $row_expositor["expo_nome"];
                                 echo $nome_expo . "</td>";
                         }
                         echo "<td id='centro'>" . date('d/m/Y' , strtotime($data)). "</td>";
                         echo "<td>";
                         if($cod_colab2 == 0){
                             $query_colab = "SELECT * FROM colaborador WHERE colab_id=$cod_colab";
                             $result_colab = mysqli_query($con, $query_colab) or die(mysqli_error());
                             while ($row_colab = mysqli_fetch_array($result_colab)) {
                                   $colab_nome = $row_colab["colab_nome_venda"];
                                   echo $colab_nome . "</td>";
                              }
                          } else {
                              $query_colab = "SELECT * FROM colaborador WHERE colab_id=$cod_colab2";
                              $result_colab = mysqli_query($con, $query_colab) or die(mysqli_error());
                              while ($row_colab = mysqli_fetch_array($result_colab)) {
                                    $colab_nome = $row_colab["colab_nome_venda"];
                                    echo $colab_nome . "</td>";
                              }
                           }
                           echo "</tr>";
                        }
             ?>

When executing, gives me the following error:

Notice: Array to string Conversion in C: xampp htdocs Sistema_1_1 est_exp_opened.php on line 65

The line 65:

$query = "SELECT * FROM venda_dia WHERE venda_dia_cod_ger in($row_ger) AND venda_dia_aberto='S'";

Thanks for your help so far...

  • $row_ger is an array of results from the first query, you can give a implode(', ', $row_ger); or even make a single query replacing WHERE venda_dia_cod_ger in($row_ger) for WHERE venda_dia_cod_ger in($query_ger).

1 answer

0


The error is that you are passing an array (resulting from $row_ger = mysqli_fetch_array($result_ger)) within the IN.

The IN accepts constants (one or more values separated by a comma: valor1[,valor2...]) or a query ("SELECT Id FROM...") that returns a column.

In this case, just put it on IN the query variable ($query_ger):

$query = "SELECT * FROM venda_dia WHERE venda_dia_cod_ger in($query_ger) AND venda_dia_aberto='S'";
  • Buddy, thanks for the help, it worked blz.

Browser other questions tagged

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