HELP - I can’t identify error - PHP and SQL

Asked

Viewed 67 times

1

Hello, I am trying to bring in a table the grouping of all stages and item evaluation per year along with the average scores and weights.

I can only bring the last step and item p table, IE, is not doing the grouping, although the error is pointing p another line (357 - marked in the code below).

Exemplification:

Table

step item score weight year area (area n is required)

A,item 1,2,4,2010

A,item 1,5,2,2010

A,item 1,3,2,2010

A,item 2,3,2,2010

A,item 2,3,2,2010

B,item 1,2,4,2010

B,item 1,5,2,2010

B,item 1,3,2,2011

And on the screen, I need you to show:

step, item, AVG score, Avg weight, total, year

A,item 1,3.3,5.3,17.49,2010

A,item 2,3,2,6,2010

B,item 1,5,4,2010

B,item 1,3,2,2011

Any doubt in the tables, you can ask. I tried to do in a summary form

The error is as follows:

( ! ) Fatal error: Cannot use Object of type stdClass as array in C: wamp64 www system line 357

Follows code below:

    <?php

        $pdo = Conexao::getInstance();


          $dados = $pdo->prepare("SELECT etapa, item_avaliacao,ano,AVG(pontuacao),AVG(peso) FROM item_avaliacao_pg GROUP BY etapa, item_avaliacao, ano");

          $dados->execute();

                  if($dados->rowCount()>=1){ 


              ?>


                
   <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          
          <div class="box">
            <div class="box-header">
            <h3 class="box-title">Item Avaliação PG</h3>
              
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>Etapa</th>
                  <th>Item avaliação</th>
                  <th>Nível satisfação</th>
                  <th>Pontuação(0-5)</th>
                  <th>Peso</th>
                  <th>Total</th>
                  <th>Ano</th>
                  
                </tr>
                </thead>
                <tbody>

                <?php while($table = $dados->fetch(PDO::FETCH_OBJ)){

                 ?>
                <tr>
               
                <?php 
                  
                  echo'<td>'.$table->etapa.'</td>';
                  echo'<td>'.$table->item_avaliacao.'</td>';
                //  echo'<td><img src="'.$table->nivel_satisfacao.'" height="42" width="42"></td>';
                  //echo'<td>'.$table->nivel_satisfacao.'</td>';
                  echo'<td>'.$table['AVG(pontuacao)'].'</td>'; //LINHA 357
                  echo'<td>'.$table['AVG(peso)'].'</td>';
                  echo'<td>'.$table['AVG(pontuacao)']*$table['AVG(peso)'].'</td>';
                  echo'<td>'.$table->ano.'</td>';
                  //Desativado
                 // echo '<td><a class="btn btn-app" href="editando-item-avaliacao.php?id='.$table->id_item_avaliacao_pg.'"><i class="fa fa-edit"></i>Editar</a></td>';
                  
                  
                  } 
                    }

                    
                      ?>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                  <th>Etapa</th>
                  <th>Item Avaliação</th>
                  <th>Nível Satisfação</th>
                  <th>Pontuação</th>
                  <th>Peso</th>
                  <th>Total</th>
                  <th>Ano</th>
                  

Thank you!

1 answer

0


The error says that an object cannot be manipulated as an array. Square brackets are used to access values from an ex array $usuario['nome'] to access properties of an object use the arrow (->) $usuario->nome.

The format the PDO returns the data is specified in fetch() be want to work with arrays change:

$dados->fetch(PDO::FETCH_OBJ)

To:

$dados->fetch(PDO::FETCH_ASSOC)

Also give alias for fields with avg() so it becomes a simple name to recover later.

  • Hi, buddy. Thanks for the availability. So, I tried this, but then it shows only the numerical results, not showing item, step and year. Error: ( ! ) Notice: Trying to get Property of non-object in C: wamp64 www System 353 This line is precisely from the step variable. I will try to create two fetch to see.

  • I managed to resolve, I had to pick up the strings inside the while, so I was just picking up the last one. Thanks again!

  • 1

    @Isadoraalmeida there you need to change the elements you have -> to use colcehtes, $table->etapa flipped $table['etapa']

  • Perfect, @rray is just that. But I accidentally put it out of the while. Now it is. Thank you

Browser other questions tagged

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