Array returns only item 0 PHP

2

I have the code below in the PHP class. However, the Array is only returning a value (position [0]) in the table. By executing the SQL statement in MySql, two lines return. Could someone tell you where the error is? Thankful.

   if($consUsu->execute()){
   $usuDados = $consUsu->fetchAll(PDO::FETCH_OBJ);
   foreach($usuDados as $usuario){
   $usuarios = $usuario->funcNome."/".$usuario->funcRg."/".$usuario->deptDescricao."/".$usuario->divDescricao;
   $usuarios = explode("/",$usuarios);

   echo '<table width="100%" border="2px solid" bordercolor="#000000">';

   echo '<th width="">NOME</th>';
   echo '<th>R.G.</th>';
   echo '<th>DEPARTAMENTO</th>';
   echo '<th>DIVISÃO</th>';
   echo '<th>CARGO</th>';
   echo '<tr width="100%">';

   for($x = 0; $x < 4; $x++){
      echo '<td width="auto">'.$usuarios[$x].'</td>';
   }

    
asked by anonymous 08.09.2015 / 19:18

1 answer

4

It is possible to simplify the code, to print the header and its values, using FETCH_NUM and another forech that will print each value in the right column.

if($consUsu->execute()){
   $usuDados = $consUsu->fetchAll(PDO::FETCH_NUM);
   $tabela = '<table width="100%" border="2px solid" bordercolor="#000000">
                <th width="">NOME</th>
                <th>R.G.</th>
                <th>DEPARTAMENTO</th>
                <th>DIVISÃO</th>
                <th>CARGO</th>
                <tr width="100%">';

    foreach($usuDados as $usuario){
        echo $tabela;
        foreach($usuario as $info){
            printf('<td width="auto">%s</td>', $info);
        }
        echo '</tr>';
    }
}

Simulated example - phpfiddle

    
08.09.2015 / 20:08