Link to home
Start Free TrialLog in
Avatar of Member_2_6479049
Member_2_6479049

asked on

php multidimensional array does't display all items

Hi guys,

I'm trying to get three values from a MySQL query and store them in a multidimensional array, the query works fine but when I try to read the array, appears this error "Notice: Undefined offset: 0 in C:\inetpub\wwwroot\Intranet2014\aplicaciones\citas\calendar0115.php on line 118"

Data base sample:
Clave          Diatramite          Mestramite          Anotramite          Horatramite
0001           01                        01                          2015                      9:00
0002           01                        01                          2015                      9:00
0003           01                        01                          2015                      9:15
0004           01                        01                          2015                      9:30
0005           12                        01                          2015                      9:00
0006           12                        01                          2015                      9:00
0007           17                        01                          2015                      10:00

My Code

$ano="2015";
$mes="01";


$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
if ($mysqli->connect_errno) {
printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
      exit();
}else{
      $result = mysqli_query($mysqli,"SELECT anotramite,mestramite,diatramite,horatramite,COUNT(*) FROM transacciones WHERE anotramite = '$ano' && mestramite = '$mes' GROUP BY diatramite,horatramite ORDER BY anotramite, mestramite, diatramite, horatramite");
      if ($result) {
            while($row = mysqli_fetch_array($result)) {
                  $NOHABILES[$row["diatramite"]][1] = $row["horatramite"];
                  $NOHABILES[$row["diatramite"]][2] = $row["COUNT(*)"];
            }
            $arrlength = count($NOHABILES);
            for($x = 0; $x < $arrlength; $x++) {
                echo "Dia==> " . $NOHABILES[$x][0] . "   Hora Tramite===> " . $NOHABILES[$x][1] . "   Cantidad==> " . $NOHABILES[$x][2];
                echo "<br>";
            }
      }
      mysqli_close($mysqli);
}

So the result must be like this:

Dia==>   1      Hora Tramite===>  9:00       Cantidad==> 2
Dia==>   1      Hora Tramite===>  9:15       Cantidad==> 1
Dia==>   1      Hora Tramite===>  9:30       Cantidad==> 1
Dia==>   12    Hora Tramite===>  9:00       Cantidad==> 2
Dia==>   17    Hora Tramite===>  10:00     Cantidad==> 1

So, I need to group the horatramite per day on mes 01.

Hope you guys can help me please.....

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_6479049
Member_2_6479049

ASKER

Thank you Daniel,

Almost done :)

it displays this:
Dia==> 01 Hora Tramite===> 09:15:00 Cantidad==> 1
Dia==> 03 Hora Tramite===> 09:15:00 Cantidad==> 2
Dia==> 12 Hora Tramite===> 09:15:00 Cantidad==> 1

but two lines are missing since the result must be:

Dia==> 01 Hora Tramite===> 09:00:00 Cantidad==> 1
Dia==> 01 Hora Tramite===> 09:15:00 Cantidad==> 1
Dia==> 03 Hora Tramite===> 09:00:00 Cantidad==> 1
Dia==> 03 Hora Tramite===> 09:15:00 Cantidad==> 2
Dia==> 12 Hora Tramite===> 09:15:00 Cantidad==> 1

And I can't find where is the error :(
It's in your WHILE loop.  You're overwriting the earlier entries in the array.
Daniel thank you again, I already change the loop and it works :)

$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
if ($mysqli->connect_errno) {
printf("No fue posible conectarse a la base de datos: %s\n", $conn->connect_error);
      exit();
}else{
      $result = mysqli_query($mysqli,"SELECT clave, anotramite,mestramite,diatramite,horatramite,COUNT(*) FROM transacciones WHERE anotramite = '$ano' && mestramite = '$mes' GROUP BY diatramite,horatramite ORDER BY anotramite, mestramite, diatramite, horatramite");
      if ($result) {
            while($row = mysqli_fetch_array($result)) {
                  $NOHABILES[$row[0]][1] = $row["diatramite"];
                  $NOHABILES[$row[0]][2] = $row["horatramite"];
                  $NOHABILES[$row[0]][3] = $row["COUNT(*)"];
            }
            foreach ($NOHABILES as $d => $x){
                    echo "Dia==> " . $x[1] . "   Hora Tramite===> " . $x[2] . "   Cantidad==> " . $x[3];
            echo "<br>";
            }
            //foreach ($NOHABILES as $d => $x){
              //      echo "Dia==> " . $d . "   Hora Tramite===> " . $x[1] . "   Cantidad==> " . $x[2];
        //   echo "<br>";
            //}
            mysqli_close($mysqli);
      }
}
Dia==> 01 Hora Tramite===> 09:00:00 Cantidad==> 1
Dia==> 01 Hora Tramite===> 09:15:00 Cantidad==> 1
Dia==> 03 Hora Tramite===> 09:00:00 Cantidad==> 1
Dia==> 03 Hora Tramite===> 09:15:00 Cantidad==> 2
Dia==> 12 Hora Tramite===> 09:15:00 Cantidad==> 1