Hi;

Well I'm starting to have a little bit of a conceptual understanding of arrays and keys and accessing array elements...so now I want to complicate my life even further.

I want to fill a two-dimensional array with the results of a query. I suppose this means some kind of nested loop?

Say I've got a bunch of test scores. Some were gotten in Fall, some in Winter and some in Spring.

So I write a query that gets the score from the MySQL table:

$Query="Select SEASON, SCORE from TableName Order By SCORE"

(I'm leaving all the connection and Link stuff out for the sake of brevity here...that I believe I understand well enough now).

I want the SEASON field to correspond to the first key in the array and the second key to point to a specific SCORE for each season.

I imagine this will be part of the code:

while ($Row=mysql_fetch_array($Result)) { 
	  $ScoreArray[] = $Row[SCORE];
}

But I don't quite get how to get the SEASON in there.

Note: I'll be perfectly happy at this point to use the default [0][0]...[$n][$n] key values that php (or MySQL?) supplies when generating an array, just to keep the coding simple.

    This seems to do what I want:

    (same query as previous message)

    while ($Row=mysql_fetch_array($Result)) { 
    	  $ScoreArray[$Row[SEASON]][] = $Row[SCORE];
    	}

    I have to use the value of SEASON (ie. $ScoreArray[FALL][2]) to retrieve a specific array element/value, but this seems to be on the right track for what I need.

    Is there a better/more efficient or more standard way to do this?

    Now I just have to finger out how to get jpgraph to display this stuff right.

      I'm trying to put this stuff into a multidimensional array called $ScoreArray.

      Is this just essentially duplicating $Result? Can I use $Result directly?

      Perhaps...

      See Joe learn. Learn Joe. Learn!

        depending on how your graph function works... you wouldnt need to.... if it just cycles through an array using a while loop or something similiar... then yah you dont need the step.... if it has to loop through several times... you would benefit by storing it in a new array

        you can get a numerically indexed array by doing this...

        $some_array[] = $row;
        and this will let you reference it liket his..

        echo $some_array[0]['fieldname']; //fieldname from 1st index
        echo $some_array[0]['fieldname2']; //fieldname2 from 1st index
        echo $some_array[2]['fieldname']; //fieldname from 3rd index

          Thanks for your reply...but I don't quite completely understand it yet.

          I have been able to retrieve/generate a two dimensional array that appears to contain all the data I need. When I do a "print_r' of my array $BoxArray it gives me back:

          Array ( [0202S] => Array ( [0] => 4 [1] => 22.25 [2] => 58.5 [3] => 93.75 [4] => 126 ) [0203S] => Array ( [0] => 1 [1] => 50.75 [2] => 76.5 [3] => 104.25 [4] => 139 ) [0205S] => Array ( [0] => 11 [1] => 65 [2] => 82 [3] => 108 [4] => 163 ) [0211S] => Array ( [0] => 5 [1] => 35.5 [2] => 68 [3] => 99 [4] => 125 ) [02WS] => Array ( [0] => 9 [1] => 30.5 [2] => 59 [3] => 92 [4] => 112 ) )

          (The codes for SEASON are the key/values that look like [0202S] etc.)

          I need to rework this output into two other arrays: $datay and $datax that must be like this:

          //one element for each tick mark
          $datax = array(0202S,0203S,0205S,0211S,02WS)
          
          //five elements for each corresponding box/whisker chart element
          $datay = array(4,22.25,58.5,93.75,126,1,50.75,76.5,104.25,139,11,65,82,108,163,5,35.5,68,99,125,9,30.5,59,92,112)
          

          However, at the moment, the best I can get is like this:
          array(0202S,0203S,0205S,0211S,02WS,) with a peksy trailing comma, because I am extracting the data with a "foreach" loop that looks like this:

          $datax = "array(";
          foreach ($PhaseArray as $Phase) {
          	$datax .= $BoxArray[$Phase][5].",";
          	}
          $datax .= ")";
          

          (note that I'm using yet another array ($PhaseArray) to get the Phase (SEASON) values....I'm sure it's not necessary, but this is the best I've been able to figure out so far).

          I know I am supposed to be using a for/if/else loop to close the last parenthesis and avoid the trailing comma but I am confused as to how to accomplish this with a two-dimensional array.

          I imagine I'll figure it out eventually, but if anyone has a shortcut that helps me simplify the code, I'd appreciate it.

            Originally posted by tekky
            depending on how your graph function works... you wouldnt need to.... if it just cycles through an array using a while loop or something similiar... then yah you dont need the step.... if it has to loop through several times... you would benefit by storing it in a new array

            you can get a numerically indexed array by doing this...

            $some_array[] = $row;
            and this will let you reference it liket his..

            echo $some_array[0]['fieldname']; //fieldname from 1st index
            echo $some_array[0]['fieldname2']; //fieldname2 from 1st index
            echo $some_array[2]['fieldname']; //fieldname from 3rd index

            in this case.....

            you would have....

            $array[0]['season'] = "02020S" (or whatever its code is)
            $array[0]['score'] = "theirscore"
            $array[0]['name'] = "somename"
            //next row returned would be like this...
            $array[1]['season'] = "03020S" (or whatever its code is)
            $array[1]['score'] = "anotherscore"
            $array[1]['name'] = "someothername"

            get the idea behind that now?

            if you want more help you are going to have to post the query and HOW you want the data grouped in arrays... I cant help w/just seeing data

              I think I'm starting to get the idea...

              I put this at the bottom of a big section of code that successfully does a bunch of quartile calculations:

              foreach ($PhaseArray as $Phase) {
              //Start loop for each PHASE
              
              ---omitted a bunch of code here to keep example clear: trust me, the calculations of the $Qx values work fine---
              
              $BoxArray[$Row][0] = $Q0; //min
              $BoxArray[$Row][1] = $Q1; //open
              $BoxArray[$Row][2] = $Q2; //median
              $BoxArray[$Row][3] = $Q3; //close
              $BoxArray[$Row][4] = $Q4; //max 
              $BoxArray[$Row][5] = $Phase; //season
              //seems like I need to do the following? ($Row starts out naturally at 0, but I want each season's scores to be distinguishable.
              $Row=$Row+1;
              //End loop for each phase
              }
              

              With the above, the following for/if/else seems to work properly

              $X = "array(";
              for ($i = 0; $i < count($BoxArray); $i++)
              {
              if ($i < (count($BoxArray)-1)) {
                  $X .= $BoxArray[$i][5].",";
                  }
              else {
                  $X .= $BoxArray[$i][5].")";
                  }
               } 
              

              I'm still not getting my jpgraphs all to come up, but I think this is progress. Probably has something to do with my jpgraph code rather than the the stuff above.

                I haven't really followed the thread, but I noticed this

                $BoxArray[$Row][0] = $Q0; //min
                $BoxArray[$Row][1] = $Q1; //open
                $BoxArray[$Row][2] = $Q2; //median
                $BoxArray[$Row][3] = $Q3; //close
                $BoxArray[$Row][4] = $Q4; //max
                $BoxArray[$Row][5] = $Phase; //season
                

                and wondered why you don't make it more meaningful like this:

                $BoxArray[$Row]['min']    = $Q0; //min
                $BoxArray[$Row]['open']   = $Q1; //open
                $BoxArray[$Row]['median'] = $Q2; //median
                $BoxArray[$Row]['close']  = $Q3; //close
                $BoxArray[$Row]['max']    = $Q4; //max
                $BoxArray[$Row]['season'] = $Phase; //season
                

                  It's a combination of reasons, really.

                  I often do a lot of cut and paste when I try to write code, and when I refer to a bunch of variables/fields in the same array I can just copy/paste $Array[$Row][0]"," a bunch of times and then just have to change the "0" to a different digit. If I used text strings, I know that I'd end up creating (and having to de-bug) a lot more typos.

                  Your suggestion is certainly reasonable, but I'm a rank amateur here and I have trouble enough with the syntax and logic of the scipt and what functions or operators to use at this point. I know I'd just be shooting myself in the foot if I used more text strings.

                    Originally posted by Joseph Sliker
                    If I used text strings, I know that I'd end up creating (and having to de-bug) a lot more typos.

                    I see your point, however, if you let php display warnings when you code, you may actually debug less. (That doesn't necessarily mean I follow my own advice, though)


                    Your suggestion is certainly reasonable, but I'm a rank amateur here and I have trouble enough with the syntax and logic of the scipt and what functions or operators to use at this point. I know I'd just be shooting myself in the foot if I used more text strings.

                    Once again, you may find the logic easier if you don't obfuscate the variables...

                    But hey, TMTOWTDI 🙂

                      Write a Reply...