Solved

php multidimensional array does't display all items

Posted on 2015-01-27
5
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
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…

626 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