Interprétation des données avec php

Résolu
Utilisateur anonyme -  
miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   -
Salut à tous :D
Alors voila le problème, je n'arrive pas a interprété les données de ma db avec php pour la librairie qui doit traité mes données ensuite, le problème vient surement du fait que j'utilise la fonction php echo (je débute donc je connais pas trop les autres), la librairie en question est phplot.

Voila pour le moment ce que j'ai testé :

  <?php
   require_once 'phplot.php';
   $data = array(
     array('', 1800,   5), array('', 1810,   7), array('', 1820,  10),
     array('', 1830,  13), array('', 1840,  17), array('', 1850,  23),
     array('', 1860,  31), array('', 1870,  39), array('', 1880,  50),
     array('', 1890,  63), array('', 1900,  76), array('', 1910,  92),
     array('', 1920, 106), array('', 1930, 123), array('', 1940, 132),
     array('', 1950, 151), array('', 1960, 179), array('', 1970, 203),
     array('', 1980, 227), array('', 1990, 249), array('', 2000, 281),
   );
   $plot = new PHPlot(800, 600);
   $plot->SetFailureImage(False);
   $plot->SetPrintImage(False);
   $plot->SetImageBorderType('plain');
   $plot->SetPlotType('lines');
   $plot->SetDataType('data-data');
   $plot->SetDataValues($data);
   $plot->SetTitle('US Population, in millions');
   $plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
   $plot->DrawGraph();
  ?>
  <img src="<?php echo $plot->EncodeImage();?>" alt="Plot Image"> 


Merci d'avance de votre aide ;)

Théo B

4 réponses

  1. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
     
    Et tu as essayé de dumper les données avant de les passer à ta méthode pour voir si elles correspondaient à ce schéma ?
    1
    1. Utilisateur anonyme
       
      Oui j'ai dumper ma base mais le schéma de ma base est correct car après il deviens un diagramme donc y a pas de schéma en particulier je pense mais c'est le code php qui ne fonctionne pas.
      Pour me connecter je fais comme ça:
      <code php>
      <?php
      try
      {
      $bdd = new PDO('mysql:host=localhost;dbname=;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
      }
      catch (Exception $e)
      {
      die('Erreur : ' . $e->getMessage());
      }
      $reponse = $bdd->query('SELECT * FROM facture_2015');

      while ($donnees = $reponse->fetch())
      {
      ?>
      <p>
      La societe <?php echo $donnees['societe']; ?>.
      </p>
      <?php
      }

      $reponse->closeCursor();

      ?>
      </code php>
      Et ensuite pour le diagramme je fais comme j'ajoute le code qu'il y a au début .

      Merci encore de t'on aide ;)
      0
    2. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      Colle le schéma de ta base et une ou deux lignes pour voir.
      0
    3. Utilisateur anonyme
       
      <?php
      require_once 'cosql_db1_sncf.php';
      ?>
      <?php
      $reponse = $bdd->query('SELECT * FROM facture_2015 WHERE id=\'1\'');

      while ($donnees = $reponse->fetch())
      {
      ?>
      <?php
      # PHPlot Example: Simple line graph
      require_once 'phplot.php';

      $data = array(
      array('', <?php echo $donnees['prix']; ?>, 0),
      );

      $plot = new PHPlot(800, 600);
      $plot->SetFailureImage(False);
      $plot->SetPrintImage(False);
      $plot->SetImageBorderType('plain');

      $plot->SetPlotType('lines');
      $plot->SetDataType('data-data');
      $plot->SetDataValues($data);

      # Main plot title:
      $plot->SetTitle('LD Consultants 2014 - 2015');

      # Make sure Y axis starts at 0:
      $plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);

      $plot->DrawGraph();

      ?>

      <?php
      }

      $reponse->closeCursor();

      ?>

      <div class="rendu"><a href="http://localhost/include/pdfplot.php"><img src="<?php echo $plot->EncodeImage();?>" alt="Plot Image"></a></div>


      C'est un exemple avec un seul point sur le graphique juste pour les test, sa fonctionne pas :/

      Merci de ton aide ;)
      0
    4. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      Le schéma de ta table facture_2015, pas le code. C'est quoi comme base de donnée.
      0
      1. Utilisateur anonyme > miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention  
         
        Structure de la table :
        #| Nom| Type|
        1| id | int(11)| auto_increment
        2| societe| varchar(255)|
        3| mois| varchar(255)|
        4| prix| int(11)|

        Merci de ton aide :)
        0
  2. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
     
    Hello,

    quel est le problème ?
    0
    1. Utilisateur anonyme
       
      C'est que je n'arrive pas a afficher mon diagramme via ma librairie avec les données de ma db, par contre le diagramme s'affiche quand les données sont inscrite dans le code de la page directement comme ci-dessus :)
      0
  3. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
     
    Hello,
    j'ai recrée ta base avec des données bidon.

    Le fichier graph.php qui génère donc le graphe, sans infos je suis parti du fait qu'on représente le prix et l'id sur le graphe :

    <?php
    
    require_once 'phplot/phplot.php';
    
    try
    {
        $bdd = new PDO('mysql:host=localhost;dbname=fac;charset=utf8', 'root', 'root', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    }
    catch (Exception $e)
    {
        die('Erreur : ' . $e->getMessage());
    }
    
    $response = $bdd->query("SELECT * FROM facture_2015");
    
    $datas = array_map(function($data){
        return array('', $data['prix'], $data['id']);
    }, $response->fetchAll());
    
    $response->closeCursor();
    
    $plot = new PHPlot(800, 600);
    
    $plot->SetFailureImage(False);
    $plot->SetPrintImage(False);
    
    $plot->SetPlotType('lines');
    $plot->SetDataType('data-data');
    $plot->SetDataValues($datas);
    
    $plot->SetTitle('LD Consultants 2014 - 2015');
    
    $plot->DrawGraph();
    
    return $plot->EncodeImage();
    
    ?>
    


    Et le fichier test.php pour générer l'image :

    <html>
        <body>
            <?php $graph = include 'graph.php'; ?>
            <div class="rendu"><img src="<?php echo $graph;?>" alt="Plot Image"></div>
        </body>
    </html>
    
    0
    1. Utilisateur anonyme
       
      Salut !
      Sa a l'aire de bien fonctionner mais j'ai des erreur au dessus du graph qui s'affiche moi :/
      Et sur le graph ya pas de courbe qui s'affiche .
      Voici les erreurs ;) :
      ( ! ) Notice: Undefined index: prix in C:\wamp\www\graph.php on line 17
      Call Stack
      # Time Memory Function Location
      1 0.0135 238672 {main}( ) ..\index.php:0
      2 0.0253 252320 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.4030 1955792 array_map ( ) ..\graph.php:18
      4 0.4030 1956048 {closure:C:\wamp\www\graph.php:16-18}( ) ..\graph.php:18

      ( ! ) Notice: Undefined index: prix in C:\wamp\www\graph.php on line 17
      Call Stack
      # Time Memory Function Location
      1 0.0135 238672 {main}( ) ..\index.php:0
      2 0.0253 252320 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.4030 1955792 array_map ( ) ..\graph.php:18
      4 0.4579 1956992 {closure:C:\wamp\www\graph.php:16-18}( ) ..\graph.php:18

      ( ! ) Notice: Undefined index: prix in C:\wamp\www\graph.php on line 17
      Call Stack
      # Time Memory Function Location
      1 0.0135 238672 {main}( ) ..\index.php:0
      2 0.0253 252320 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.4030 1955792 array_map ( ) ..\graph.php:18
      4 0.5116 1957600 {closure:C:\wamp\www\graph.php:16-18}( ) ..\graph.php:18

      ( ! ) Notice: Undefined index: prix in C:\wamp\www\graph.php on line 17
      Call Stack
      # Time Memory Function Location
      1 0.0135 238672 {main}( ) ..\index.php:0
      2 0.0253 252320 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.4030 1955792 array_map ( ) ..\graph.php:18
      4 0.5620 1958336 {closure:C:\wamp\www\graph.php:16-18}( ) ..\graph.php:18

      ( ! ) Notice: Undefined index: prix in C:\wamp\www\graph.php on line 17
      Call Stack
      # Time Memory Function Location
      1 0.0135 238672 {main}( ) ..\index.php:0
      2 0.0253 252320 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.4030 1955792 array_map ( ) ..\graph.php:18
      4 0.6167 1958944 {closure:C:\wamp\www\graph.php:16-18}( ) ..\graph.php:18

      Merci encore de ton aide ;)
      0
    2. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      Qu'est ce que ça donne

      var_dump($response->fetchAll());
      
      0
    3. Utilisateur anonyme
       
      Sa affiche qu'une erreur cette fois mais sa affiche que ça !
      ( ! ) Parse error: syntax error, unexpected ';' in C:\wamp\www\graph.php on line 18
      Call Stack
      # Time Memory Function Location
      1 0.0013 238672 {main}( ) ..\index.php:0


      pour ce code :
      $datas = array_map(function($data){
      return array('', $data['prix'], $data['id']);
      }, var_dump($response->fetchAll());

      $response->closeCursor();
      0
    4. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      Laisse le array_map tel qu'il était, rajoute le var _dump après la ligne avec $bdd->query
      0
    5. Utilisateur anonyme
       
      La sa a l'air de mieux fonctionner mais sa m'affiche qu'une erreur :
      array (size=5)
      0 =>
      array (size=8)
      'id' => string '1' (length=1)
      0 => string '1' (length=1)
      'societe' => string 'ld_consultants' (length=14)
      1 => string 'ld_consultants' (length=14)
      'mois' => string 'Janvier' (length=7)
      2 => string 'Janvier' (length=7)
      'montant' => string '10450' (length=5)
      3 => string '10450' (length=5)
      1 =>
      array (size=8)
      'id' => string '2' (length=1)
      0 => string '2' (length=1)
      'societe' => string 'ld_consultants' (length=14)
      1 => string 'ld_consultants' (length=14)
      'mois' => string 'Fevrier' (length=7)
      2 => string 'Fevrier' (length=7)
      'montant' => string '14300' (length=5)
      3 => string '14300' (length=5)
      2 =>
      array (size=8)
      'id' => string '3' (length=1)
      0 => string '3' (length=1)
      'societe' => string 'ld_consultants' (length=14)
      1 => string 'ld_consultants' (length=14)
      'mois' => string 'Mars' (length=4)
      2 => string 'Mars' (length=4)
      'montant' => string '11000' (length=5)
      3 => string '11000' (length=5)
      3 =>
      array (size=8)
      'id' => string '4' (length=1)
      0 => string '4' (length=1)
      'societe' => string 'ld_consultants' (length=14)
      1 => string 'ld_consultants' (length=14)
      'mois' => string 'Avril' (length=5)
      2 => string 'Avril' (length=5)
      'montant' => string '11825' (length=5)
      3 => string '11825' (length=5)
      4 =>
      array (size=8)
      'id' => string '5' (length=1)
      0 => string '5' (length=1)
      'societe' => string 'ld_consultants' (length=14)
      1 => string 'ld_consultants' (length=14)
      'mois' => string 'Mai' (length=3)
      2 => string 'Mai' (length=3)
      'montant' => string '18425' (length=5)
      3 => string '18425' (length=5)

      ( ! ) Fatal error: DrawGraph(): No data array in C:\wamp\www\phplot\phplot.php on line 1957
      Call Stack
      # Time Memory Function Location
      1 0.0076 238048 {main}( ) ..\index.php:0
      2 0.0084 252432 include( 'C:\wamp\www\graph.php' ) ..\index.php:3
      3 0.8067 3032032 PHPlot->DrawGraph( ) ..\graph.php:33
      4 0.8067 3032664 PHPlot->CheckDataArray( ) ..\phplot.php:7484
      5 0.8067 3032904 PHPlot->PrintError( ) ..\phplot.php:2380
      6 0.8067 3033000 trigger_error ( ) ..\phplot.php:1957


      Pour le code suivant :

      <?php

      require_once 'phplot/phplot.php';

      try
      {
      $bdd = new PDO('mysql:host=localhost;dbname=comptabilite_sncf;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
      }
      catch (Exception $e)
      {
      die('Erreur : ' . $e->getMessage());
      }

      $response = $bdd->query("SELECT * FROM facture_2015");
      var_dump($response->fetchAll());
      $datas = array_map(function($data){
      return array('', $data['prix'], $data['id']);
      }, $response->fetchAll());

      $response->closeCursor();

      $plot = new PHPlot(800, 600);

      $plot->SetFailureImage(False);
      $plot->SetPrintImage(False);

      $plot->SetPlotType('lines');
      $plot->SetDataType('data-data');
      $plot->SetDataValues($datas);

      $plot->SetTitle('LD Consultants 2014 - 2015');

      $plot->DrawGraph();

      return $plot->EncodeImage();

      ?>


      Merci ;)
      0
  4. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
     
    Le premier array dans le array_map ne va rien faire, il n'est assigné à aucune variable, n'est pas retourné, c'est du code sans effet.

    Regarde la doc de array_map : https://www.php.net/manual/fr/function.array-map.php.

    Voilà un exemple avec 4 graphiques sans base de donnée avec des données bidon.

    <?php
    
    require_once 'phplot/phplot.php';
    
    $datas = array(
        array('2001', 18,   5, 20, 1),
        array('2002', 18,   7, 22, 4),
        array('2003', 18,  10, 24, 7),
        array('2004', 18,  13, 26, 10),
        array('2005', 18,  17, 28, 13),
        array('2006', 18,  23, 30, 16),
        array('2007', 18,  31, 32, 19),
        array('2008', 18,  39, 34, 22),
        array('2009', 18,  50, 36, 25),
        array('2010', 18,  63, 38, 28),
        array('2011', 19,  76, 40, 28),
        array('2012', 19,  92, 42, 31),
        array('2013', 19, 106, 44, 34),
        array('2014', 19, 123, 46, 37),
        array('2015', 19, 132, 48, 40),
        array('2016', 19, 151, 50, 43),
        array('2017', 19, 179, 52, 46),
        array('2018', 19, 203, 54, 49),
        array('2019', 19, 227, 56, 51),
        array('2020', 19, 249, 58, 54),
        array('2021', 20, 281, 60, 59),
    );
    
    $chartDatas = array_map(function($data){
        return array($data[0], $data[1], $data[2], $data[3], $data[4]);
    }, $datas);
    
    $plot = new PHPlot(800, 600);
    
    $plot->SetFailureImage(false);
    $plot->SetPrintImage(false);
    
    $plot->SetPlotType('lines');
    $plot->SetDataType('text-data');
    $plot->SetDataValues($chartDatas);
    
    $plot->SetTitle('Charts');
    $plot->SetLegend(array('Graph 1', 'Graph 2', 'Graph 3', 'Graph 4'));
    
    $plot->DrawGraph();
    
    return $plot->EncodeImage();
    
    ?>
    
    0
    1. Utilisateur anonyme
       
      Ah d'accord, j'ai compris le principe.Mais donc pour que les données soient sur une base de données je remplace les valeurs des "array" par une variable ?

      Comme ceci :

      <?php
      
      require_once 'phplot/phplot.php';
      
      $datas = array(
          array('2001', $data['montant'],   $data['id']),
          array('2002', $data['montant'],   $data['id']),
      );
      
      $chartDatas = array_map(function($data){
          return array($data[0], $data[1]);
      }, $datas);
      
      $plot = new PHPlot(800, 600);
      
      $plot->SetFailureImage(false);
      $plot->SetPrintImage(false);
      
      $plot->SetPlotType('lines');
      $plot->SetDataType('text-data');
      $plot->SetDataValues($chartDatas);
      
      $plot->SetTitle('Charts');
      $plot->SetLegend(array('Graph 1', 'Graph 2'));
      
      $plot->DrawGraph();
      
      return $plot->EncodeImage();
      
      ?>
      
      0
    2. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      Je comprends pas.
      0
    3. Utilisateur anonyme
       
      Ah d'accord, en faite moi j'aimerais faire un graphique avec plusieurs lignes mais avec la base de donnée. c'est ce que j'ai essayer de faire juste avant :)
      0
    4. miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention   92
       
      C'est comme l'exemple 9 où l'on extrait les données de la base de donnée. Juste avant il n'y a aucune queries donc je ne vois pas d'où tu extrais tes données.
      0
    5. Utilisateur anonyme > miramaze Messages postés 1429 Date d'inscription   Statut Contributeur Dernière intervention  
       
      Oui d'accord mais sur l'exemple 9 il n'y a qu'une ligne et moi j'aimerais en mette plus mais je n'y arrive pas.
      Merci
      Cdlt Théo B
      0