Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

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.
0
elm9999
Asked:
elm9999
  • 3
  • 2
1 Solution
 
Daniel WilsonCommented:
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
 
elm9999Author Commented:
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
 
Daniel WilsonCommented:
It's in your WHILE loop.  You're overwriting the earlier entries in the array.
0
 
elm9999Author Commented:
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
 
elm9999Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now