Voila depuis 2 jours j'ai un problème pour afficher un graphique en fonction de donnée dans une bdd voila mon problème:
je recupére les données d'une table que je met dans un tableau:
La fonction connect()
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 <?php function connect() { $hote = 'localhost'; $name = 'perso'; $user = 'root'; $pwd = '*********'; try { $bdd = new PDO('mysql:host='.$hote.';dbname='.$name, $user, $pwd); } catch(Exception $e) { echo 'Erreur : '.$e->getMessage().'<br />'; echo 'N° : '.$e->getCode(); } return $bdd; }
La fonction get_all_article()(Static dans la classe article)
Maintenant le code qui est censé générer l'image (dans un fichier histogramme.php a part du reste)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <?php static public function get_all() { $bdd=connect(); return $bdd->query("SELECT * FROM articles")->fetchAll(PDO::FETCH_ASSOC); }
J'appel l'image :
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 <?php $i = 0; $max = 0; /* *je récupère les valeur retourné dans ma requête *et je détermine le max (pour déterminer la taille final de l'image) */ $all_values = Article::get_all(); foreach ($all_values as $value){ $array_valeur[$i] = (int) $value['I_ARTICLE_STOCK']; //ici si je fait un echo $array_valeur[$i].'</br>'; //j'ai bien les valeur dans mon tableau if ($max < $array_valeur[$i]) { $max = $array_valeur[$i]; } $i++; } header('Content-Type: image/png'); $im = imagecreatetruecolor(600, 600); //normalement déterminer par $i et $max $bleu = imagecolorallocate($im, 29, 56, 252); $bleu2 = imagecolorallocate($im, 134, 149, 253); $gris = imagecolorallocate($im, 193, 193, 193); // ariére plan gris imagefilledrectangle($im, 0, 0, 599, 599, $gris); $echelle = 400/$max; //nombre d'unité par pixel taille du graph en lui même 400px //initialisation des coordonnées $x2 = 10; $y2 = 405; $x1 = 5; $i = count($array_valeur); for ($j = 0; $j < $i; $j++) { $y1 = ceil($y2 - $echelle*$array_valeur[$j]); //taille de la barre imagefilledrectangle($im, $x1,$y1,$x2,$y2, $bleu); //affichage $x2 += 10; $x1 = $x2 - 5; //si je fait: //echo 'imagefilledrectangle($im, '.$x1.','.$y1.','.$x2.','.$y2.', $bleu);<br>'; // le code est bon pas de float a la place de int ect... } imagepng($im); //affichage de l'image imagedestroy($im); ?>
Et j'obtiens : voir la première image ci-dessous
Code : Sélectionner tout - Visualiser dans une fenêtre à part <img src="display/image_histo.php" />
Maintenant en bidouillant je remplace:
Par:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?php $all_values = Article::get_all(); foreach ($all_values as $value){ $array_valeur[$i] = (int) $value['I_ARTICLE_STOCK']; //ici si je fait un echo $array_valeur[$i].'</br>'; //j'ai bien les valeur dans mon tableau if ($max < $array_valeur[$i]) { $max = $array_valeur[$i]; } $i++; } ?>
Et j'obtiens bien l'image mais les données ne viennent de la bdd:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?php // //array(....) généré en fesant un echo dans le foreach précédent //puis copié collé pour pas d'erreur :) // $all_values = array(639, 164, 1654, 159, 755, 3573, 3830, 203, 272, 103, 0, 46, 2502, 2312, 509, 2829, 1419, 2359, 482, 3461, 106, 3288, 2881, 3455, 974, 2130, 62, 104, 216, 1321, 64, 23, 101, 43, 59, 362, 802, 172, 206, 146, 212, 88, 0, 143, 80, 123, 241, 288, 123, 139, 384); foreach ($all_values as $value){ $array_valeur[$i] = $value; if ($max < $array_valeur[$i]) { $max = $array_valeur[$i]; } $i++; } ?>
(voir i'image en pj)
Avec le code de remplacement j'ajoute juste ce code:
Avec ça sa marche plus , je pense que ça viens de ma fonction get_all ou connect() comme si il y avait un "echo" dans ces fonctions.
Code : Sélectionner tout - Visualiser dans une fenêtre à part $var = Articles::get_all();
Merci d'avance
Alexandre
Partager