Page 1 of 1

Question about getDoubleKeyCollectionFromDb()

Posted: 01 October 2022, 04:11
by IndianaScones
I'm going through the Reversi tutorial and stuck on the following:

Code: Select all

// Get the complete board with a double associative array
    function getBoard()
    {
        return self::getDoubleKeyCollectionFromDB( "SELECT board_x x, board_y y, board_player player
                                                       FROM board", true );
    }
Since bSingleValue is true, the result should only contain the third column. So in the above, it's just a single column db with player?

The code is then used as follows:

Code: Select all

if( $board[ $x ][ $y ] === null ) // If there is already a disc on this place, this can't be a valid move
How is [ $x ][ $y ] being used to find the player value if the result of getBoard() only contains the third column? It all makes sense to me except for the second arg for getDoubleKeyCollectionFromDb().

Re: Question about getDoubleKeyCollectionFromDb()

Posted: 01 October 2022, 07:52
by tchobello
hi, you should have a look at getCollectionFromDb().

It works quite the same way and there are more explanations on the wiki.

Re: Question about getDoubleKeyCollectionFromDb()

Posted: 01 October 2022, 11:31
by robinzig
So this method returns an array whose keys are the `x` values, and whose values are themselves arrays. Those "second level" arrays have keys as the `y` values, and values which are the arrays of player values. Hence using `$board[$x][$y]` to access those values.

Had the `bSingleValue` argument been `false`, that array of player values would in fact have been an array of further associative arrays, each containing just a single key `player` with the player value in question. It does this so you can use the same method with queries for more than 3 columns, and get the complete rest of the data in this "third level" array. But usually when there's just a single third column you're interested in, you don't want extra "noise" of extracting the single value from this array, so that's what that argument is there for.

Hope this makes sense. If it doesn't from a text explanation, try just `var_dump`-ing the value returned by this call (and perhaps also try doing that when changing the `true` argument to `false` and compare the two) and it should make sense.