Solved

php multidimensional array does't display all items

Posted on 2015-01-27
5
128 Views
Last Modified: 2015-01-27
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.
0
Comment
Question by:elm9999
  • 3
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 40573603
You don't have a $NOHABILES[0].  You have $NOHABILES["01"] and $NOHABILES["17"]. And in the second dimension you have defined entries for 1 and 2, but not 0.

The associative array syntax sometimes gets in the way of ordinary array access.

See this example from http://php.net/manual/en/control-structures.foreach.php
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

Open in new window


I think your usage would look more like
foreach ($NOHABILES as $d => $x){
  echo "Dia==> " . $d . "   Hora Tramite===> " . $x[1] . "   Cantidad==> " . $x[2];
                echo "<br>";
}

Open in new window

0
 

Author Comment

by:elm9999
ID: 40573810
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 :(
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40573819
It's in your WHILE loop.  You're overwriting the earlier entries in the array.
0
 

Author Comment

by:elm9999
ID: 40574125
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);
      }
}
0
 

Author Comment

by:elm9999
ID: 40574130
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
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now