Bonjour à tous, en espérant que je ne me trompe pas de forum, sinon, merci aux admins de m'indiquer où poser mon problème.
Je suis loin d'etre un expert de la programmation, je dirais même que pour 3 lignes de code il me faut 3 heures avant de comprendre.
Bien :
J'ai une base mysql dans laquel j'ai créé une table nommée gasoil. Dans cette table, je souhaite récupérer les valeurs de deux colonnes afin de pouvoir générer un graphique avec Highcharts.
Pour ce faire, j'ai donc créé deux fichiers php, database.php et graph.php.
Voici le résultat obtenu :
voici le code du fichier database.php
Et enfin, le code du fichier graph.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 <?php $servername = "localhost"; $username = "nobody"; $password = ""; $dbname = "hhhh"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sqlquery = "select date from gasoil order by date"; //la requete prend les champs date ds la base de données et trie par dates $resultset = mysqli_query($conn, $sqlquery) or die("database error:". mysqli_error($conn)); $array_date= array(); $array_ppl= array(); while ( $row = mysqli_fetch_array($resultset) ) // tant qu'il y a des données c 'est incorporé dans le tableau { //debut while $date = $row["date"]; //repcupération de la valeur date array_push($array_date, $date); } // fin while $sqlquery1 = "select ppl from gasoil"; //la requete prend les champs ppl ds la base de données $resultset1 = mysqli_query($conn, $sqlquery1) or die("database error:". mysqli_error($conn)); while ( $row1 = mysqli_fetch_array($resultset1) ) // tant qu'il y a des données c 'est incorporé dans le tableau { //debut while $ppl = $row1["ppl"]; array_push($array_ppl, $ppl); } // fin while $conn->close(); ?>
Vous pouvez remarquer que j'ai bien mes deux arrays (date et ppl) qui sont bien renseignés.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 <?php include ('../database.php'); include ('../fonctions.php'); print_r($array_date); print_r($array_ppl); ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Highcharts pleins</title> <style type="text/css"> .highcharts-figure, .highcharts-data-table table { min-width: 360px; max-width: 800px; margin: 1em auto; } .highcharts-data-table table { font-family: Verdana, sans-serif; border-collapse: collapse; border: 1px solid #EBEBEB; margin: 10px auto; text-align: center; width: 100%; max-width: 500px; } .highcharts-data-table caption { padding: 1em 0; font-size: 1.2em; color: #555; } .highcharts-data-table th { font-weight: 600; padding: 0.5em; } .highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption { padding: 0.5em; } .highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) { background: #f8f8f8; } .highcharts-data-table tr:hover { background: #f1f7ff; } </style> </head> <body> <script src="../code/highcharts.js"></script> <script src="../code/modules/series-label.js"></script> <script src="../code/modules/exporting.js"></script> <script src="../code/modules/export-data.js"></script> <script src="../code/modules/accessibility.js"></script> <figure class="highcharts-figure"> <div id="container"></div> </figure> <script type="text/javascript"> Highcharts.chart('container', { chart: { type: 'spline', scrollablePlotArea: { minWidth: 600, scrollPositionX: 1 } }, title: { text: 'Pleins Peugeot 2008' }, subtitle: { text: '' }, yAxis: { title: { text: 'Prix du litre' } }, xAxis: { accessibility: { rangeDescription: '' } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle' }, tooltip: { valueSuffix: ' /L' }, plotOptions: { spline: { lineWidth: 4, states: { hover: { lineWidth: 5 } }, marker: { enabled: false }, } }, /* plotOptions: { series: { label: { connectorAllowed: false }, pointStart: 2010 } }, */ series: [{ data: [<?php echo join($array_ppl, ',') ?>], }, ], responsive: { rules: [{ condition: { maxWidth: 800 }, chartOptions: { legend: { layout: 'horizontal', align: 'center', verticalAlign: 'bottom' } } }] } }); </script> </body> </html>
En revanche, sur le graphique, je n'arrive pas à obtenir les dates correspondantes à chaque prix de l'essence au litre. (et oui, ppl = prix par litre)
Si quelqu'un pouvait m'aider de façon à ce que je comprenne, ce serait formidable. J'ai tenté des tas de truc, mais rien de chez rien ne fonctionne.
Merci par avance à mon futur sauveur.
Eric
Partager