I am using: self:getDoubleKeyCollectionFromDB() which does a perfect job of ordering array elements by position.
I found 2 strange things when this gets passed to JS via getAlldatas(). php var_dump:
I have data 'position' with both positive and negative values, aswell as 0.
This is what I have to use to get it across in the same order:
Strange thing #1:
Using just SELECT idTrain PHP again is perfect, but JS side gives now uses an associate array with the Id field as key, which is fine BUT changes the order of the data.
Strange thing #2:
So, I add SELECT idTrain, position PHP again is perfect, JS side now uses an associate array with the position field as key - great, BUT the it is ordered by string(? or still the Id?), so I get 0,1,2,-1,-2,-3,-4,-5
which leads me to my final hack:
SELECT idTrain, position+1000
which works, but looks very ugly, and feels like the tail is wagging the dog.
I found 2 strange things when this gets passed to JS via getAlldatas(). php var_dump:
Code: Select all
array(1) { [0]=> array(8) { [150]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "150" ["position"]=> string(2) "-5" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "22" ["x"]=> string(1) "9" ["y"]=> string(1) "6" ["operatingRound"]=> string(1) "1" } [149]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "149" ["position"]=> string(2) "-4" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "28" ["x"]=> string(1) "8" ["y"]=> string(1) "7" ["operatingRound"]=> string(1) "1" } [148]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "148" ["position"]=> string(2) "-3" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "41" ["x"]=> string(1) "8" ["y"]=> string(1) "9" ["operatingRound"]=> string(1) "1" } [147]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "147" ["position"]=> string(2) "-2" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "54" ["x"]=> string(1) "8" ["y"]=> string(2) "11" ["operatingRound"]=> string(1) "1" } [146]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "146" ["position"]=> string(2) "-1" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "48" ["x"]=> string(1) "9" ["y"]=> string(2) "10" ["operatingRound"]=> string(1) "1" } [143]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "143" ["position"]=> string(1) "0" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "55" ["x"]=> string(2) "10" ["y"]=> string(2) "11" ["operatingRound"]=> string(1) "1" } [144]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "144" ["position"]=> string(1) "1" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "49" ["x"]=> string(2) "11" ["y"]=> string(2) "10" ["operatingRound"]=> string(1) "1" } [145]=> array(9) { ["idTrain"]=> string(1) "0" ["id"]=> string(3) "145" ["position"]=> string(1) "2" ["player_id"]=> string(7) "2308257" ["idCompany"]=> string(2) "14" ["idHex"]=> string(2) "62" ["x"]=> string(2) "11" ["y"]=> string(2) "12" ["operatingRound"]=> string(1) "1" } } }This is what I have to use to get it across in the same order:
Code: Select all
$result['routes'] = self::getDoubleKeyCollectionFromDB("SELECT idTrain, position+1000, route.* FROM route WHERE player_id = $current_player_id ORDER BY position ASC");
Strange thing #1:
Using just SELECT idTrain PHP again is perfect, but JS side gives now uses an associate array with the Id field as key, which is fine BUT changes the order of the data.
Code: Select all
143: {idTrain: "0", id: "143", position: "0", player_id: "2308257", idCompany: "14", …}
144: {idTrain: "0", id: "144", position: "1", player_id: "2308257", idCompany: "14", …}
145: {idTrain: "0", id: "145", position: "2", player_id: "2308257", idCompany: "14", …}
146: {idTrain: "0", id: "146", position: "-1", player_id: "2308257", idCompany: "14", …}
147: {idTrain: "0", id: "147", position: "-2", player_id: "2308257", idCompany: "14", …}
148: {idTrain: "0", id: "148", position: "-3", player_id: "2308257", idCompany: "14", …}
149: {idTrain: "0", id: "149", position: "-4", player_id: "2308257", idCompany: "14", …}
150: {idTrain: "0", id: "150", position: "-5", player_id: "2308257", idCompany: "14",So, I add SELECT idTrain, position PHP again is perfect, JS side now uses an associate array with the position field as key - great, BUT the it is ordered by string(? or still the Id?), so I get 0,1,2,-1,-2,-3,-4,-5
Code: Select all
0: {idTrain: "0", position: "0", id: "143", player_id: "2308257", idCompany: "14", …}
1: {idTrain: "0", position: "1", id: "144", player_id: "2308257", idCompany: "14", …}
2: {idTrain: "0", position: "2", id: "145", player_id: "2308257", idCompany: "14", …}
-1: {idTrain: "0", position: "-1", id: "146", player_id: "2308257", idCompany: "14", …}
-2: {idTrain: "0", position: "-2", id: "147", player_id: "2308257", idCompany: "14", …}
-3: {idTrain: "0", position: "-3", id: "148", player_id: "2308257", idCompany: "14", …}
-4: {idTrain: "0", position: "-4", id: "149", player_id: "2308257", idCompany: "14", …}
-5: {idTrain: "0", position: "-5", id: "150", player_id: "2308257", idCompany: "14", …}SELECT idTrain, position+1000
Code: Select all
995: {idTrain: "0", position+1000: "995", id: "150", position: "-5", player_id: "2308257", …}
996: {idTrain: "0", position+1000: "996", id: "149", position: "-4", player_id: "2308257", …}
997: {idTrain: "0", position+1000: "997", id: "148", position: "-3", player_id: "2308257", …}
998: {idTrain: "0", position+1000: "998", id: "147", position: "-2", player_id: "2308257", …}
999: {idTrain: "0", position+1000: "999", id: "146", position: "-1", player_id: "2308257", …}
1000: {idTrain: "0", position+1000: "1000", id: "143", position: "0", player_id: "2308257", …}
1001: {idTrain: "0", position+1000: "1001", id: "144", position: "1", player_id: "2308257", …}
1002: {idTrain: "0", position+1000: "1002", id: "145", position: "2", player_id: "2308257", …}