IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Passer un tableau PHP en Javascript [PHP 5.3]


Sujet :

Langage PHP

  1. #1
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2007
    Messages : 503
    Points : 291
    Points
    291
    Billets dans le blog
    1
    Par défaut Passer un tableau PHP en Javascript
    Bonjour,

    je tente d'utiliser l'api Google Chart interactive.

    il faut donc que je récupère un tableau php : par exemple j'ai ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <?php
    $tableau=array('Mushrooms'=>3, 'Onions'=>1, 'Olives'=>1, 'Olives'=>1, 'Pepperoni'=>2);
    ?>
    il faut que je passe ce tableau en js pour obtenir un code comme celui ci :
    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
    <html>
      <head>
     
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
     
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});
     
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
     
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
     
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'serie1');
          data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1], 
            ['Zucchini', 1],
            ['Pepperoni', 2]
          ]);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':1000,
                         'height':600};
     
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        </script>
      </head>
     
      <body>
    <!--Div that will hold the pie chart-->
        <div id="chart_div" style="width:1000; height:600"></div>
      </body>
    </html>
    ce code vient d'un exemple google, mais pour bien comprendre le truc je voudrais déjà passer mon tableau php au niveau du data.addRows (montableau)!!

    j'ai donc tenté ce code mais en vain :
    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
    <?php
    $tableau=array('Mushrooms'=>3, 'Onions'=>1, 'Olives'=>1, 'Olives'=>1, 'Pepperoni'=>2);
    ?>
     
    <html>
      <head>
      <script type="text/javascript">
    var tableau = <? php echo json_encode($tableau) ?>;
     
    </script>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
     
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});
     
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
     
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
     
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'serie1');
    	  data.addRows(tableau);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':1000,
                         'height':600};
     
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        </script>
      </head>
     
      <body>
    <!--Div that will hold the pie chart-->
        <div id="chart_div" style="width:1000; height:600"></div>
      </body>
    </html>
    merci d'avance de vos bons conseil

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    un coup de json_encode et c'est bon

  3. #3
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2007
    Messages : 503
    Points : 291
    Points
    291
    Billets dans le blog
    1
    Par défaut
    un coup de json_encode et c'est bon
    c'est bien ce que j'ai essayé de faire mais il y a quelque chose qui ne fonctionne pas!!
    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
    <?php
    $tableau=array('Mushrooms'=>3, 'Onions'=>1, 'Olives'=>1, 'Olives'=>1, 'Pepperoni'=>2);
    ?>
     
    <html>
      <head>
      <script type="text/javascript">
    var tableau = <? php echo json_encode($tableau) ?>;
     
    </script>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
     
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});
     
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
     
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
     
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'serie1');
    	  data.addRows(tableau);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':1000,
                         'height':600};
     
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        </script>
      </head>
     
      <body>
    <!--Div that will hold the pie chart-->
        <div id="chart_div" style="width:1000; height:600"></div>
      </body>
    </html>

  4. #4
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    t'as un espace en trop entre le <? et le php

  5. #5
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2007
    Messages : 503
    Points : 291
    Points
    291
    Billets dans le blog
    1
    Par défaut
    effectivement, c'est corrigé mais toujours rien!!! page blanche

  6. #6
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    Citation Envoyé par elnipal Voir le message
    effectivement, c'est corrigé mais toujours rien!!! page blanche
    montre la source HTML

  7. #7
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2007
    Messages : 503
    Points : 291
    Points
    291
    Billets dans le blog
    1
    Par défaut
    voici le code de la page générée :
    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
     
     
     
    <html>
     
      <head>
     
      <script type="text/javascript">
     
    var tableau = {"Mushrooms":3,"Onions":1,"Olives":1,"papaye":1,"Pepperoni":2};
     
     
     
    </script>
     
        <!--Load the AJAX API-->
     
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
     
        <script type="text/javascript">
     
     
     
          // Load the Visualization API and the piechart package.
     
          google.load('visualization', '1.0', {'packages':['corechart']});
     
     
     
          // Set a callback to run when the Google Visualization API is loaded.
     
          google.setOnLoadCallback(drawChart);
     
     
     
          // Callback that creates and populates a data table, 
     
          // instantiates the pie chart, passes in the data and
     
          // draws it.
     
          function drawChart() {
     
     
     
          // Create the data table.
     
          var data = new google.visualization.DataTable();
     
          data.addColumn('string', 'Topping');
     
          data.addColumn('number', 'serie1');
     
    	  data.addRows(tableau);
     
          // Set chart options
     
          var options = {'title':'How Much Pizza I Ate Last Night',
     
                         'width':1000,
     
                         'height':600};
     
     
     
          // Instantiate and draw our chart, passing in some options.
     
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
     
          chart.draw(data, options);
     
        }
     
        </script>
     
      </head>
     
     
     
      <body>
     
    <!--Div that will hold the pie chart-->
     
        <div id="chart_div" style="width:1000; height:600"></div>
     
      </body>
     
    </html>
    a priori c'est le format du tableau qui coince :
    avec le json_encode j'obtiens donc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    {"Mushrooms":3,"Onions":1,"Olives":1,"papaye":1,"Pepperoni":2}
    alors qu'il me faut ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var tableau = [['Mushrooms', 3],['Onions', 1],['Olives', 1],['Zucchini', 1],['Pepperoni', 2]];
    avec ce dernier code en faisant ensuite
    ça fonctionne

  8. #8
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    c'est pas le même type de tableau que tu montres
    en PHP ca donne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    $tableau=array(
        array('Mushrooms', 3), 
        array('Onions', 1), 
        array('Olives', 1), 
        array('Olives', 1), 
        array('Pepperoni', 2)
    );

  9. #9
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2007
    Messages : 503
    Points : 291
    Points
    291
    Billets dans le blog
    1
    Par défaut
    ok c'est bon, c'est un tableau de tableau!

    le code qui fonctionne :
    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
    <?php
    $tableau = array(
    				array('Mushrooms',3),
    				array('Onions',1),
    				array('Olives',1),
    				array('papaye',1),
    				array('Pepperoni',2)
    				);
     
    echo json_encode($tableau);
    ?>
     
    <html>
      <head>
      <script type="text/javascript">
    var tableau = <?php echo json_encode($tableau) ?>;
     
    </script>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
     
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});
     
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
     
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
     
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'serie1');
    	  data.addRows(tableau);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':1000,
                         'height':600};
     
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        </script>
      </head>
     
      <body>
    <!--Div that will hold the pie chart-->
        <div id="chart_div" style="width:1000; height:600"></div>
      </body>
    </html>
    merci bcp

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 22/12/2009, 15h01
  2. Passer un tableau PHP en argument dans une fonction javascript ?
    Par The Molo dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 18/05/2007, 12h31
  3. [PHP-JS] Transferer tableau php a javascript
    Par stephane92400 dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 12/07/2006, 14h05
  4. [PHP-JS] Passer un tableau php en tableau JS
    Par dark_vidor dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 22/12/2005, 11h36
  5. Recuperer un tableau php dans javascript
    Par 17mounir dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 11/08/2005, 09h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo