Logic formulation for problem solving

-2

I have two tables, table A with id and tasks (description), and another table B with the task fk, quantity, fk, and official. In addition to other columns that is not the case.

In my table in the html (table), I show all the tasks already in thead, and in the results I take the respective quantities of each task.

The problem is that a store can register 5 tasks, and register activities for its employees, however, after a while, it can register another task, only that qd this occurs, my table view in the html breaks. For in my table B there is no data for this new task.

Would anyone have any suggestions on how to solve this problem?

Part of the job quest query:

//tabela A, checo todas as tarefas cadastradas
  $consulta2 = $pdo->prepare("SELECT id_tarefa FROM tarefa WHERE id_loja=:id");
   $consulta2->bindParam(':id', $id_loja, PDO::PARAM_INT);
                    
           if($consulta2->execute()){

               if($consulta2->rowCount() > 0){

                 while($dados_cons2 = $consulta2->fetch(PDO::FETCH_OBJ)){
                            

                            $id_tarefa2=$dados_cons2->id_tarefa;
                       

  //essa seria minha tabela B, checo todas as atividades, que possuem as quantidades da tarefas
  $dados3 = $pdo->prepare("SELECT * FROM atividades_diarias as ativ where ativ.id_ativ=:id_ativ and ativ.id_loja=:id_loja and ativ.tipo_atividade=:tipo and ativ.id_tarefa=:id_taf");

                    $dados3->bindParam(':id_ativ',$id_ativ, PDO::PARAM_INT);
                    $dados3->bindParam(':id_loja',$id_loja, PDO::PARAM_INT);
                $dados3->bindParam(':tipo',$tipo_atividade, PDO::PARAM_STR);
                 $dados3->bindParam(':id_taf',$id_tarefa2, PDO::PARAM_INT);

                    $dados3->execute();

                         while($table3 = $dados3->fetch(PDO::FETCH_OBJ)){

                                       
                 $qtd_tarefa = $table3->qtd_tarefa;

                    //só que as tarefas novas n tem dados antigos na minha tabela B, ele é inexistente, aí quebra a tabela
                     if($qtd_tarefa!=null){ //usei null mas n funcionou
                                    echo'<td>'.$qtd_tarefa.'</td>';
                              }else{
                                     $vazio="";
                                             echo'<td>'.$vazio.'</td>';
                                                }

                                                   }    

My table in HTMl (example)

Date .... Official Name ... task1 .... task2 ... NOVATAREFA ..... schedule entry
1/6/18 ......... Isadora ............... 2 ................ 3 .. ............ 08:00

Result I need:
Date .... Official Name ... task1 .... task2 ... NOVATAREFA ..... schedule entry 1/6/18 ......... Isadora .............. 2 ............. 3 ...... ............... EMPTY ................. 08:00

Note that the new job is taken to the value of the next column, which should be empty, as there is no historical data about it.

Does anyone know how I can check to see if my task exists in table B, and if n is null in table view in html? I have tried several alternatives, including inner join.

Thank you!

    
asked by anonymous 01.06.2018 / 17:59

1 answer

0

You said that a store "can" register 5 tasks and then one more. So we have a maximum of 6 tasks. This your table in html should contain 6 columns of tasks, task1, task5, and new task. Problem is not in sql, as it only retrieves from the database what is actually registered, if there is less we must create the empty cells as you wish, then In your loop:

while($table3 = $dados3->fetch(PDO::FETCH_OBJ)){


                 $qtd_tarefa = $table3->qtd_tarefa;

                    //só que as tarefas novas n tem dados antigos na minha tabela B, ele é inexistente, aí quebra a tabela
                     if($qtd_tarefa!=null){ //usei null mas n funcionou
                                    echo'<td>'.$qtd_tarefa.'</td>';
                              }else{
                                     $vazio="";
                                             echo'<td>'.$vazio.'</td>';
                                                }

                                                   }    

Add a found task helper. and at the end of the loop recreate the cells of the empty table, and even if you have registered less than 5 tasks the empty columns will be created and your table in the html will be correct. According to Example:

$contadorTarefas = 0;
while($table3 = $dados3->fetch(PDO::FETCH_OBJ)){
  $contadorTarefas++;
  $qtd_tarefa = $table3->qtd_tarefa;
  echo'<td>'.$qtd_tarefa.'</td>';
}
//Se não existe tarefa para preencher todas as colunas de tarefas, cria as que faltam vazias
while ($contadorTarefas < 6) {
  $contadorTarefas++;
  echo'<td></td>';
}
    
02.06.2018 / 04:41