Array returns only item 0 PHP

Asked

Viewed 145 times

2

I have the code below in the PHP class.However, the Array is only returning one value (position [0]) in the table. Executing the SQL statement in Mysql, return two lines. Could anyone tell 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>';
   }

Resutado na página HTML. Deveria conter duas linhas.

  • 1

    $teste comes from where? and does what/?

  • Excuse me. I put this name only to make a test that ended up not working. The right name of this variable is: $user.

  • 1

    I would ask just that @rray. What if I remove that line? And in FOR for($x = 0; $x < 4; $x++) I predict problems, because it goes to element 3 of the array, and if you have no more than one object or have more than 3 objects?

  • 1

    Please edit your question with the correct code?

  • This < 4 is related to the number of columns that will be inserted in the <table>; My select returns four columns. So < 4.

  • If you have no reason to use an object, pq does not use a numeric array instead? no way that explode() ai.

  • I’m using explode() to dismember the columns returned by select and play them in the <table> columns. Otherwise, the result is launched into a single cell of the table. ?

Show 2 more comments

1 answer

4

It is possible to simplify the code, to prevent the header and its values, using the FETCH_NUM and one more 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

  • Hi @rray, Thanks for the tip. I followed his example and only one line was printed on the page; However, I printed the "$info" Array with var_dump and returned all the items in the array. array(2) { [0]=> array(4) { [0]=> string(9) "Renatinha" [1]=> string(4) "1111" [2]=> string(18) "TEST DEPARTMENT" [3]=> string(14) "TEST DIVISION" } [1]=> array(4) { [0]=> string(16) "Test User 1" [1]=> string(4) "1112" [2]=> string(18) "TEST DEPARTMENT" [3]=> string(14) "TEST DIVISION" } }.

  • The problem (I believe), should be in the way the result is printed on the page.

  • @Vanderci see if the generated html failed to close tr.

  • i there was percibo no closure of <tr>. I closed it over still printing only the first line.

  • After closing the <tr> tag, he started to print the two lines, only this way: NAME R.G. DIVISION DEPARTMENT CARGO Renatinha 1111 DEPARTMENT TEST DIVISION TEST NAME R.G. DEPARTMENT DIVISION CARGO User Test 1 1112 DEPARTMENT TEST DIVISION TEST That is, it is repeating the table header.

  • 1

    It worked @rray. I took the echo $table from the foreach and printed only one header and the two lines from the Array; Thank you all for your help. Was worth too much.

  • @Vanderci, if the answer solved the problem can mark it as accepted, How and why to accept an answer?, the site works different from a forum, see the other differences in tour

  • Of course @rray. I just wanted to ask a question: I would like to know why the "%s" in the code below: printf('<td width="auto">%s</td>', $info);

  • the printf() format a string based on type, the %s means string, it will be replaced by the value of the variable passed as the second argument. At this link shows all available options. @Vanderci

Show 4 more comments

Browser other questions tagged

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