Best way to use Foreach

Asked

Viewed 33 times

0

I have a bit of code, but I believe that is not one of the best, because I am beginner in programming. The values are coming duplicated, I would like to know if the way I am performing the code is correct and how to make the values not duplicate:

// Percorrendo os Jobs
            foreach ( $asana->getData() as $tasks )
            {   
                // Pegando ID & Incrementado a Conta
                    $ids     = $tasks->id;
                    $exibir  = true;    
                    $jobs++;

                // Pegando detalhes dos Jobs
                    $getTask = $asana->getTask($ids, array('opt_fields' => 'name,completed,assignee_status,created_at,due_on'));
                    $getTask = json_decode($getTask);

                // Percorrendo os dados
                    foreach($getTask as $item)
                    {
                        // Pegando os Jobs Completos & Incompletos
                            if ( $item->completed ) { $jobs_completos ++; }
                            $jobs_incompletos = $jobs-$jobs_completos;

                        // Pegando as datas
                            if ( $item->due_on )
                            { 
                                $datas[] = $item->due_on;
                            }
                            $conta_data     = count($datas, COUNT_RECURSIVE);
                            $conta_sem_data = $jobs-$conta_data;

                    }

                // Jobs do Mês
                    foreach ( $datas as $data ) 
                    {
                        // Recuperando as datas
                            $data     = date('d/m/Y', strtotime($data));
                            $data     = explode("/", $data); 
                            $hoje_dia = date('d'); // Dia
                            $hoje_mes = date('m'); // Mês
                            $hoje_ano = date('Y'); // Ano

                        // Pegando os Jobs do Mês
                            $jobs_mes = 0;
                            if ( $data[1] == $hoje_mes )
                            {
                                echo $data[0]." / ".$data[1]." / ".$data[2]."<br />";
                                $jobs_mes++;
                            }

                    }
            }

I believe it is not necessary this much of "Foreach’s" how can I "improve" this code?

  • The code is nicely "tied up," um foreach depends on the output of the oturo and etc, I suggest you separate them into functions and call each of them in a main function.

  • @Rafaelacioly can give me an example of how to create a function and call it in HTML below all foreach? Not wanting chewed, but wanting!

  • It’s hard to help you without knowing what the object is $asana and what you want to do with it. You have to [Edit] the question and add the result of var_export($asana)?

No answers

Browser other questions tagged

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