Jump to content

array multidimensional error


Go to solution Solved by Jacques1,

Recommended Posts

Hi

 

I'm having a problem regarding getting the values off my array....

 

I'm calling a function to get values

 public function getAllAulasSemDespacho(){
        try {
            $stmt = $this->db->prepare("SELECT a.Atividade, a.Local, a.DataAula, ae.idAula, p.Nome, t.Turma
                                        FROM
                                            aulaexteriorturmas AS ae
                                            INNER JOIN turmas AS t
                                            ON (ae.idTurma = t.idTurma)
                                            INNER JOIN aulaexterior  AS a 
                                            ON (ae.idAula = a.idAula)
                                            INNER JOIN professores AS p
                                            ON (a.idProfessor = p.idProfessor) WHERE a.Autorizado IS NULL ");
            if (!$stmt->execute()) {
                print_r($stmt->errorInfo());
                return false;
            } else {
                $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
                $aulas_array = array();
                foreach ($result as $row) {
                    $idAula = $row['idAula'];
                    if (!isset($aulas_array[$idAula])) {
                        $aulas_array[$idAula]['idAula'] = $row['idAula'];
                        $aulas_array[$idAula]['Atividade'] = $row['Atividade'];
                        $aulas_array[$idAula]['Local'] = $row['Local'];
                        $aulas_array[$idAula]['DataAula'] = $row['DataAula'];
                        $aulas_array[$idAula]['Nome'] = $row['Nome'];
                        $aulas_array[$idAula]['Turma'] = array();
                    }
                    $aulas_array[$idAula]['Turma'][] = $row['Turma'];
                }
                foreach ($aulas_array as $idAula => $aulas) {
                    json_encode($aulas);
                    return $aulas;
                }

            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

From the query...in my sql i receive

Atividade                          Local                  DataAula    idAula  Nome                    Turma   
---------------------------------  ---------------------  ----------  ------  ----------------------  --------
Teste final PDF                    Algarve                2017-08-31       5  xxxxxxxxxxxxxxxxxxxxxx  5ºC    
Teste final PDF                    Algarve                2017-08-31       5   xxxxxxxxxxxxxxxxxxxxxx  6ºB    
Teste final PDF                    Algarve                2017-07-27       6  xxxxxxxxxxxxxxxxxxxxxx  10ºO   
Teste final PDF                    Algarve                2017-07-27       6   xxxxxxxxxxxxxxxxxxxxxx  11ºJ   
Teste final PDF                    Algarve                2017-07-27       6   xxxxxxxxxxxxxxxxxxxxxx  12ºL

So i want to join the same (Turma) values by (idAula)

 

When i want to see in the front end i receive an array...but i get illegal offset errors

 <?php
                        if (!empty($aulasListagem)) {
                            foreach ($aulasListagem as $row) {
                                print_r($row);
                                ?>
                                <tr>
                                    <td align="center"><?php echo $row['Atividade']; ?></td>
                                    <td align="center"><?php echo $row['Local']; ?></td>
                                    <td align="center"><?php echo $row['DataAula']; ?></td>
                                    <td align="center"><?php echo $row['Nome']; ?></td>
                                    <td align="center"><?php echo $row['Turma']; ?></td>
                                    <td><a data-toggle="modal" data-id="<?php echo $row['idAula']; ?>"
                                           class="open-AddDialog btn btn-sm btn-success center-block"
                                           href="#myModalDespachoAula">Opção</a></td>
                                </tr>
                                <?php
                            }
                        }
                        ?>

From var_dump i'm getting this

array(6) { ["idAula"]=> string(1) "5" ["Atividade"]=> string(33) "Algarve" ["Local"]=> string(7) "Algarve" ["DataAula"]=> string(10) "2017-08-31" ["Nome"]=> string(22) "xxxxxxxxx" ["Turma"]=> array(2) { [0]=> string(4) "5ºC" [1]=> string(4) "6ºB" } }

I only receive the first idAula and  i get illegal offset errors in the table

 

 

any help?

Link to comment
https://forums.phpfreaks.com/topic/304369-array-multidimensional-error/
Share on other sites

The whole code doesn't make a lot of sense. You should program in much smaller steps, test each result and only continue of it's actually correct. Then you don't end up with a wall of non-working code which you're no longer able to debug yourself.

  • For some reason, you simultaneously assume that the code can throw exceptions and cannot throw exceptions. You have a try statement to catch PDO exceptions and print them on the screen (which is stupid – leave them alone). But you also have return value checks, which implies that exceptions are turned off completely. It cannot be both. Either exceptions are enabled, or they're disabled.
  • What's the point of using a prepared statement for a query which doesn't have any parameters and is only executed once? Just do a plain old query().
  • When you iterate over your $aulas_array, you call json_encode() but don't store the return value anywhere. And then you immediately return the first item, so all other data is lost.
    foreach ($aulas_array as $idAula => $aulas) {
        json_encode($aulas);    // <---- this doesn't have any effect
    
        return $aulas;          // <---- this stops the loop immediately after the first iteration
    }
    
     
Edited by Jacques1

Hi Jacques

 

How are you?

 

About your questions

You have a try statement to catch PDO exceptions and print them on the screen (which is stupid – leave them alone). Yes, is to check if everything is ok, after the code is ok, i remove it.

What's the point of using a prepared statement for a query which doesn't have any parameters and is only executed once. You are right, i agree with you.

When you iterate over your $aulas_array, you call json_encode() but don't store the return value anywhere. Again dummy from me.

 

I was returning the array after the 1º iteration.

 

Regards

  • Solution

You have a try statement to catch PDO exceptions and print them on the screen (which is stupid – leave them alone). Yes, is to check if everything is ok, after the code is ok, i remove it.

 

This doesn't make sense. Just leave the exception alone and turn display_errors on during development, then PHP will print the error message and all relevant information on the screen.

 

There's no point in adding and then removing a try statement only to print the message. PHP can do that much better than you.

 

 

So -- problem fixed?

This doesn't make sense. Just leave the exception alone and turn display_errors on during development, then PHP will print the error message and all relevant information on the screen.

...and kill the script, too. Having a try/catch at least prevents that from happening.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.