Page 1 of 1

JS re-ordering data

Posted: 05 June 2020, 12:51
by RicardoRix
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:

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" } } }
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:

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",
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

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", …}
which leads me to my final hack:
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", …}
which works, but looks very ugly, and feels like the tail is wagging the dog. :D

Re: JS re-ordering data

Posted: 05 June 2020, 13:31
by Lymon Flowers
I think that at least you should do

Code: Select all

select position+1000 AS position_fixed
or whatever so as to produce valid json. ;-)

However, the sorting is weird since as ASCII value of "-" is less that any number, negative numbers should go first.

Re: JS re-ordering data

Posted: 05 June 2020, 13:31
by Tisaac
Didn't read in détails but I'm always using array_values in the backend to remove associative arrays before sending to frontend. This make sure the data is not converted as an object on the js side and then I can use all js array fonctions which are really the best (filter, forEach, reduce,mam, includes,...)

Re: JS re-ordering data

Posted: 05 June 2020, 14:03
by RicardoRix
Tisaac wrote: 05 June 2020, 13:31 Didn't read in détails but I'm always using array_values in the backend to remove associative arrays before sending to frontend. This make sure the data is not converted as an object on the js side and then I can use all js array fonctions which are really the best (filter, forEach, reduce,mam, includes,...)
Yes, array_values works well, or better should I say.
Thx.

Would be nice to know if this behaviour can be fixed \ changed in the framework.