Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php multidimensional array does't display all items

Posted on 2015-01-27
5
Medium Priority
?
151 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

730 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