Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Powielanie danych po konwersji do JSON
rafik73
post 9.04.2018, 19:09:04
Post #1





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

Ostrzeżenie: (0%)
-----


Witam
Nie wiem jak to ugryźć. Podaję kod:

  1. $result=mysqli_query($mysqli , "SELECT * FROM tssk , tssk_points_2018 , planfri
  2. WHERE
  3. pf_data = data_punktu
  4. AND nr_punktu = point_2018
  5. AND pf_data BETWEEN '$first' AND '$last'
  6. ORDER BY pf_data ASC , nr_punktu ASC
  7. ")
  8. or die(mysqli_error($mysqli));
  9.  
  10.  
  11. while($row=mysqli_fetch_array($result)) {
  12.  
  13. $no=$row['no'];
  14. $date=$row['data_punktu'];
  15. $ts[]=$row['point_time_text_2018'];
  16.  
  17. $posts[$date] = array(
  18. 'no'=> $no,
  19. 'date'=> $date,
  20. 'ts'=> $ts
  21. );
  22.  
  23. }
  24.  
  25. $fp = fopen('results.json', 'w');
  26. fwrite($fp, json_encode($posts, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_ERROR_UTF8));
  27. fclose($fp);


Wynik:


  1. {
  2. "2018-01-01": {
  3. "no": "0",
  4. "date": "2018-01-01",
  5. "ts": [
  6. "4 min lub mniej",
  7. "4 min",
  8. "3 min lub mniej",
  9. "6 min lub mniej"
  10. ]
  11. },
  12. "2018-01-08": {
  13. "no": "0",
  14. "date": "2018-01-08",
  15. "ts": [
  16. "4 min lub mniej",
  17. "4 min",
  18. "3 min lub mniej",
  19. "6 min lub mniej",
  20. "4 min lub mniej",
  21. "2 min lub mniej",
  22. "5 min",
  23. "6 min lub mniej"
  24. ]
  25. },
  26. "2018-01-15": {
  27. "no": "0",
  28. "date": "2018-01-15",
  29. "ts": [
  30. "4 min lub mniej",
  31. "4 min",
  32. "3 min lub mniej",
  33. "6 min lub mniej",
  34. "4 min lub mniej",
  35. "2 min lub mniej",
  36. "5 min",
  37. "6 min lub mniej",
  38. "4 min lub mniej",
  39. "2 min lub mniej",
  40. "3 min lub mniej",
  41. "5 min"
  42. ]
  43. },
  44. "2018-01-22": {
  45. "no": "0",
  46. "date": "2018-01-22",
  47. "ts": [
  48. "4 min lub mniej",
  49. "4 min",
  50. "3 min lub mniej",
  51. "6 min lub mniej",
  52. "4 min lub mniej",
  53. "2 min lub mniej",
  54. "5 min",
  55. "6 min lub mniej",
  56. "4 min lub mniej",
  57. "2 min lub mniej",
  58. "3 min lub mniej",
  59. "5 min",
  60. "4 min lub mniej",
  61. "3 min lub mniej",
  62. "3 min lub mniej",
  63. "6 min lub mniej"
  64. ]
  65. },
  66. "2018-01-29": {
  67. "no": "0",
  68. "date": "2018-01-29",
  69. "ts": [
  70. "4 min lub mniej",
  71. "4 min",
  72. "3 min lub mniej",
  73. "6 min lub mniej",
  74. "4 min lub mniej",
  75. "2 min lub mniej",
  76. "5 min",
  77. "6 min lub mniej",
  78. "4 min lub mniej",
  79. "2 min lub mniej",
  80. "3 min lub mniej",
  81. "5 min",
  82. "4 min lub mniej",
  83. "3 min lub mniej",
  84. "3 min lub mniej",
  85. "6 min lub mniej",
  86. "4 min lub mniej",
  87. "3 min lub mniej",
  88. "3 min lub mniej",
  89. "6 min lub mniej"
  90. ć
  91. }
  92. }


Problem tkwi w tym, że wartości pola ts są 'zapamiętywane' i powielane w kolejnych rekordach. Jego wartości
są w bazie danych przypisane względem daty (pf_data) i powinno ich być po 4 na każdą datę.
Nie mogę z tym dojsc do ładu.
Go to the top of the page
+Quote Post
Pyton_000
post 9.04.2018, 19:39:54
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Spójż na linię 15 z ww. kodu
Go to the top of the page
+Quote Post
rafik73
post 9.04.2018, 20:14:19
Post #3





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

Ostrzeżenie: (0%)
-----


Tworzę tam tabelę, bez tego wyświetla pojedyncze rekordy.
Bo jak dam:

  1. $ts=$row['point_time_text_2018'];


wyświetla:

  1. {
  2. "2018-01-01": {
  3. "no": "0",
  4. "date": "2018-01-01",
  5. "ts": "6 min lub mniej"
  6. },
  7. "2018-01-08": {
  8. "no": "0",
  9. "date": "2018-01-08",
  10. "ts": "6 min lub mniej"
  11. },
  12. "2018-01-15": {
  13. "no": "0",
  14. "date": "2018-01-15",
  15. "ts": "5 min"
  16. },
  17. "2018-01-22": {
  18. "no": "0",
  19. "date": "2018-01-22",
  20. "ts": "6 min lub mniej"
  21. },
  22. "2018-01-29": {
  23. "no": "0",
  24. "date": "2018-01-29",
  25. "ts": "6 min lub mniej"
  26. }
  27. }



Chciałbym uzyskać taki efekt:

  1. {
  2. "2018-01-01": {
  3. "no": "0",
  4. "date": "2018-01-01",
  5. "ts": [
  6. "4 min lub mniej",
  7. "4 min",
  8. "3 min lub mniej",
  9. "6 min lub mniej"
  10. ]
  11. },
  12. "2018-01-08": {
  13. "no": "0",
  14. "date": "2018-01-08",
  15. "ts": [
  16.  
  17. "4 min lub mniej",
  18. "2 min lub mniej",
  19. "5 min",
  20. "6 min lub mniej"
  21. ]
  22. },
  23. "2018-01-15": {
  24. "no": "0",
  25. "date": "2018-01-15",
  26. "ts": [
  27.  
  28. "4 min lub mniej",
  29. "2 min lub mniej",
  30. "3 min lub mniej",
  31. "5 min"
  32. ]
  33. },
  34. "2018-01-22": {
  35. "no": "0",
  36. "date": "2018-01-22",
  37. "ts": [
  38.  
  39. "4 min lub mniej",
  40. "3 min lub mniej",
  41. "3 min lub mniej",
  42. "6 min lub mniej"
  43. ]
  44. },
  45. "2018-01-29": {
  46. "no": "0",
  47. "date": "2018-01-29",
  48. "ts": [
  49.  
  50. "4 min lub mniej",
  51. "3 min lub mniej",
  52. "3 min lub mniej",
  53. "6 min lub mniej"
  54. ]
  55. }
  56. }


Ten post edytował rafik73 9.04.2018, 20:25:14
Go to the top of the page
+Quote Post
mmmmmmm
post 9.04.2018, 21:05:49
Post #4





Grupa: Zarejestrowani
Postów: 1 421
Pomógł: 310
Dołączył: 18.04.2012

Ostrzeżenie: (0%)
-----


Nie tworzysz tabeli, tylko dodajesz kolejny element.
Go to the top of the page
+Quote Post
rafik73
post 9.04.2018, 21:14:40
Post #5





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

Ostrzeżenie: (0%)
-----


Wykombinowałem coś takiego i działa:

  1. $i = 0;
  2. mysqli_query($mysqli , "SET NAMES 'utf8'");
  3.  
  4. $result=mysqli_query($mysqli , "SELECT * FROM tssk , tssk_points_2018 , planfri
  5. WHERE
  6. pf_data = data_punktu
  7. AND nr_punktu = point_2018
  8. AND pf_data BETWEEN '$first' AND '$last'
  9. ORDER BY pf_data ASC , nr_punktu ASC
  10. ")
  11. or die(mysqli_error($mysqli));
  12.  
  13.  
  14. while($row=mysqli_fetch_array($result)) {
  15.  
  16. $no=$row['no'];
  17. $date=$row['data_punktu'];
  18.  
  19.  
  20.  
  21. if($i == 4) {$i = 0;}
  22. $ts[$i]=$row['point_time_text_2018'];
  23.  
  24. $posts[$date] = array(
  25. 'no'=> $no,
  26. 'date'=> $date,
  27. 'ts'=> $ts
  28. );
  29. $i++;
  30. }
Go to the top of the page
+Quote Post
Pyton_000
post 10.04.2018, 07:10:19
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


eeee... Widać że słabo u ciebie z tablocami..


  1. while ($row = mysqli_fetch_array($result)) {
  2.  
  3. $no = $row['no'];
  4. $date = $row['data_punktu'];
  5. $ts[$date][] = $row['point_time_text_2018'];
  6.  
  7. $posts[$date] = [
  8. 'no' => $no,
  9. 'date' => $date,
  10. 'ts' => $ts[$date],
  11. ];
  12. }


Ten post edytował Pyton_000 10.04.2018, 07:10:35
Go to the top of the page
+Quote Post
rafik73
post 14.04.2018, 07:47:29
Post #7





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

Ostrzeżenie: (0%)
-----


Próbowałem wcześniej w ten sposób:


  1. $ts[$date]= $row['point_time_text_2018'];
  2. $posts[$date] = array(
  3. 'no'=> $no,
  4. 'ts'=> $ts
  5. );


ale efekt odbiegał od oczekiwanego. Masz rację, mam sporo do podciągnięcia w tym temacie.
Dziękuję za pomoc.

Wiem, że to może nie ten język, ale po wygenerowaniu pliku results.json mam problem z deserializacją w C#. Wyrzuca błąd "Unexpected character encountered while parsing value: D". Używam Newtonsoft.Json. Czy json_encode generuje niezgodny format?

Co zrobić, żeby nie wyświetlał się index $date

  1. $posts[$date] = [


może inaczej, chciałbym to wyświetlić tak:

  1. {
  2. {
  3. "no": "0",
  4. "date": "2018-01-01",
  5. "ts": [
  6. "4 min lub mniej",
  7. "4 min",
  8. "3 min lub mniej",
  9. "6 min lub mniej"
  10. ]
  11. },
  12. {
  13. "no": "0",
  14. "date": "2018-01-08",
  15. "ts": [
  16.  
  17. "4 min lub mniej",
  18. "2 min lub mniej",
  19. "5 min",
  20. "6 min lub mniej"
  21. ]
  22. },
  23. {
  24. "no": "0",
  25. "date": "2018-01-15",
  26. "ts": [
  27.  
  28. "4 min lub mniej",
  29. "2 min lub mniej",
  30. "3 min lub mniej",
  31. "5 min"
  32. ]
  33. },
  34. {
  35. "no": "0",
  36. "date": "2018-01-22",
  37. "ts": [
  38.  
  39. "4 min lub mniej",
  40. "3 min lub mniej",
  41. "3 min lub mniej",
  42. "6 min lub mniej"
  43. ]
  44. },
  45. {
  46. "no": "0",
  47. "date": "2018-01-29",
  48. "ts": [
  49.  
  50. "4 min lub mniej",
  51. "3 min lub mniej",
  52. "3 min lub mniej",
  53. "6 min lub mniej"
  54. ]
  55. }
  56. }


Dla potomnych - użyłem funkcji array_values i to rozwiązało sprawę

  1. $posts = array_values($posts);
  2. $fp = fopen('results.json', 'w');
  3. fwrite($fp, json_encode($posts, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_ERROR_UTF8));
  4. fclose($fp);
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 20:52