Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [AJAX][PHP][MySQL]Wykres w Google Charts, dane z MYSQL
Forum PHP.pl > Forum > Przedszkole
latino
Witam, chciałbym stworzyć za pomocą Google Charts wykres zmian ceny Oleju Napędowego. Niestety coś mi nie wychodzi, JSON output nie widzi wierszy pobieranych z bazy MySql. Moja strona: http://www.dawny-inowroclaw.info/orlen/wykres3.php

Zawartość pliku wykres3.php
  1. <?php
  2. $con=mysql_connect("localhost","admin","password") or die("Failed to connect with database!!!!");
  3. mysql_select_db("latino_orlen", $con);
  4. $sth = mysql_query("SELECT data, cenaon FROM ceny");
  5.  
  6.  
  7. $results = array(
  8. 'cols' => array (
  9. array('label' => 'data', 'type' => 'date'),
  10. array('label' => 'cenaon', 'type' => 'number')
  11. ),
  12. 'rows' => array()
  13. );
  14.  
  15.  
  16. while($row = mysqli_fetch_assoc($sth))
  17. {
  18. // date assumes "yyyy-MM-dd" format
  19. $dataArr = explode('-', $row['data']);
  20. $year = (int) $dataArr[0];
  21. $month = (int) $dataArr[1] - 1; // subtract 1 to make month compatible with javascript months
  22. $day = (int) $dataArr[2];
  23.  
  24.  
  25. $results['rows'][] = array('c' => array(
  26. array('v' => "data($year, $month, $day)"),
  27. array('v' => $row['cenaon'])
  28. ));
  29. }
  30.  
  31. $json = json_encode($results, JSON_PRETTY_PRINT);
  32. echo $json;

  1. <head>
  2. <!--Load the Ajax API-->
  3. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  4. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  5. <script type="text/javascript">
  6.  
  7. // Load the Visualization API and the piechart package.
  8. google.load('visualization', '1', {'packages':['corechart']});
  9.  
  10. // Set a callback to run when the Google Visualization API is loaded.
  11. google.setOnLoadCallback(drawChart);
  12.  
  13. function drawChart() {
  14.  
  15. // Create our data table out of JSON data loaded from server.
  16. var data = new google.visualization.DataTable(<?php echo $json ?>);
  17. var options = {
  18. title: 'Cena paliw',
  19. is3D: 'true',
  20. width: 800,
  21. height: 600
  22. };
  23. // Instantiate and draw our chart, passing in some options.
  24. // Do not forget to check your div ID
  25. var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  26. chart.draw(data, options);
  27. }
  28. </script>
  29. </head>
  30.  
  31. <body>
  32.  
  33. <!--this is the div that will hold the pie chart-->
  34. <div id="chart_div"></div>
  35. </body>
  36. </html>


Polecenie echo $json; daje:
Kod
{ "cols": [ { "label": "data", "type": "date" }, { "label": "cenaon", "type": "number" } ], "rows": [] }


Tabela Bazy danych wygląda tak:
Kod
Host: localhost
Baza danych: latino_orlen
Czas wygenerowania: 27 Lut 2016, 09:58
Wygenerowany przez: phpMyAdmin 4.0.10.7 / MySQL 5.6.23
Zapytanie SQL: SELECT * FROM `ceny` LIMIT 0, 30;
Rekordy: 4

id    data    cenaon    
19    2016-02-24    2.939
20    2016-02-25    2,99
21    2016-02-26    2.95
22    2016-02-27    2.964


Struktura tabeli:
Kod
Kolumna    Typ              Null    
id         int(11)          Nie          
data       date             Nie          
cenaon     varchar(100)     Nie


Co robię źle? Pomoże ktoś? Z góry dziękuję smile.gif
nospor
Raz uzywasz mysql_ a raz mysqli_ .... To sa zupelnie dwie rozne rzeczy.
A na przyszlosc wlacz wyswietlanie bledow to ci php sam pokaze bledy
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.