for/if PHP omitting element

Asked

Viewed 54 times

-1

I have a big problem. I have a PHP/LARAVEL system that issues PDF reports. The system it is omitting an element when placing it in an array.

I believe it’s time to assemble the loop and make the comparisons of if I’m missing something.

I have a list of people and I have to list 9 people per page, the 10 person who should be on the next page does not appear he jumps this person and the fact repeats itself every 10 elements. The solution may be simple but I’m not thinking.

NOTE: The return of the elements in the database brings all people.

$Serv[$p][$j] = $server; where [$p] is the page and the [$j] element on the page

I hope you can help me.

$i = 0;
$j = 0;
$p = 0;
$dep = '';
$pdep = '';

$aux = count($servidores);

foreach ($servidores as $value => $servidor) {

    $servidor->ferias = $ferias[$value]->ferias;

    if($value == 0){
        $pdep = $servidor->cd_departamento;
        $dep = $servidor->cd_departamento;
        $serv[$p][$j] = $servidor;
        $j++;
    }else{
        if($dep == $servidor->cd_departamento){
            if ($j < 9) {
                $serv[$p][$j] = $servidor;
                $j++;
            } else {
                $p++;
                $j = 0;
                $serv[$p][$j] = $servidor;
            }
        }else{
            $dep = $servidor->cd_departamento;
            $p++;
            $j = 0;
            $serv[$p][$j] = $servidor;
        }
    }

}
  • $servidores_separados_por_pagina = array_chunk($servidores, 9);

  • I ended up not explaining, but the system has a peculiarity, if the person is from a different department than the previous person he should start a new page.

  • has to send the contents of the variable servers?

  • @douglaspjuizfora, and how do you know the person belongs to a different department?

  • It comes with all the records of the server object. When putting this object inside the array is where the problem is. He is commenting on the elements 10,20,30,40 and so on that in the code would be the elements (9,19,29,39 ...)

  • @Vitor Carnaval, I save the elements in the variables "$pdep = $server->cd_departamento; $Dep = $server->cd_departamento; and I compare whether or not I moved to the last server.

  • In the variable $servers, it brings ALL the elements right, Now when it comes to throwing the object inside these arrays it does not put the (9,19,29,39...)

Show 2 more comments

2 answers

1


$pagina = 0;
$departament_do_servidor_anterior = null;
$servidores_paginados = [];
foreach ($servidores as $index => $servidor) {
    if ($index > 0 && $departament_do_servidor_anterior !== $servidor->cd_departamento) {
        $pagina++;
    }

    $servidores_paginados[$pagina][] = $servidor;
    $departament_do_servidor_anterior = $servidor->cd_departamento;

    if (count($servidores_paginados[$pagina]) === 9) {
        $pagina++;
    }
}
  • Thank you so much for helping @Adir Kuhn.

0

Douglas, I updated the code you posted with more readability to make it easy to identify the error.

$depByPage = '';

foreach ($servidores as $value => $servidor) {

    $servidor->ferias = $ferias[$value]->ferias;
    $dep = $servidor->cd_departamento;

    if($value === 0){
        $pdep = $dep;
        $depByPage = $dep;
    }

    /* Criar mais um array de elementos caso atinja o limite de servidores
     * por página ou se o departamento é diferente
     */
    if ($j >= 9 || $dep != $depByPage) {
        $j = 0;
        $p++;
        $depByPage = $dep;

        $serv[$p][$j] = $servidor;
        continue; //Vai diretamente para o inicio do loop
    }      

    $serv[$p][$j] = $servidor;

    $j++;
}

Update your code and see if the error persists.

  • unfortunately continues. He is "eating" the elements 10,20,30,40 and so on.

  • The variable $pdep would be the specific department for each page?

  • This variable is used to store the first server department. It is used in another part of the code that does not interfere with this part.

  • I updated the reply @douglaspjuizfora, try now!

Browser other questions tagged

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