Ando
Ando

Reputation: 1827

php to JavaScript variable transfer

I'm not very familiar with JavaScript but for a project that I'm working on, I need to draw a graph using some input from the db. Can you help me out with the transfer of the php values to the javascript. I just want to draw a pie chart using the data from the db.

<div id="pie" style="width:250px;height:250px;">

    <?php 
        $k=0; 
        if (isset($distrib)){
            foreach($distrib as $row){
              echo $distrib[$k]['lang'];
              echo $distrib[$k]['NumArt'];
               $k++;
            }
        }
    ?>​

    <script class="code" type="text/javascript">
        $(document).ready(function() {
            var data = [];
            var plot1 = jQuery.jqplot('pie', [data], {
                seriesDefaults: {
                    // Make this a pie chart.
                    renderer: jQuery.jqplot.PieRenderer,
                    rendererOptions: {
                        // Put data labels on the pie slices.
                        // By default, labels show the percentage of the slice.
                        showDataLabels: true
                    }
                },
                legend: {
                    show: false,
                    location: 'e'
                }
            });
        });​
    </script>

EDIT

I'm still struggling with this issue. These js charts are killing me.

So here is an additional question that will hopefully end my misery

                            if (isset($distrib)){
                                foreach($distrib as $row){
                                  $p[] = $distrib[$k]['langName'];
                                  $s[] = $distrib[$k]['lang'];
                                  $q[] = $distrib[$k]['NumArt'];
                                   $k++;
                                }}  
                            ?><?php $c = array_combine($p, $q); ?>

                        <script class="code" type="text/javascript">$(document).ready(function(){


                                    <?php echo "var s1 = [".implode($c, ",")."];" ?>
                                var plot3 = $.jqplot('pie',  [s1], {

Basically at the moment the chart is printing out numArt, but it does not print the langName which i want. Lang name stores languages in their native alphabet which means anything from arabic to Zulu. I tried using JSON with no success (i totally lack knowledge in that area). My d/encoding was messed up so I left that path.

My goal is to render a chart which has the language's name and the amount of articles for that language.

So here is the print_r from
$p

Array ( [0] => none [1] => Catal [2] => français [3] => Malti [4] => português [5] => Kiswahili [6] => isiZulu [7] => English )

and $q Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 1 [5] => 1 [6] => 1 [7] => 1 )

and the combined $c Array ( [none] => 1 [Catal] => 1 [français] => 1 [Malti] => 2 [português] => 1 [Kiswahili] => 1 [isiZulu] => 1 [English] => 1 )

In the end I need s1 to be something along these lines

[['none',1],['français',1],['Malti',1]]


Upvotes: 0

Views: 286

Answers (4)

RRikesh
RRikesh

Reputation: 14381

<script type="text/javascript">

var data = [ <?php echo implode(",", $array_of_values); ?> ];
<script>

Just make sure the $array_of_values is an array (PHP) containing the values you want to pass in the data variable(JS).

UPDATE:

$string_array = array();
foreach( $array as $key=>$value )
{
  $string_array[] = '[' . $key . ',' . $value . ']';
}

Then implode.

Upvotes: 1

nbrooks
nbrooks

Reputation: 18233

//...
$(document).ready(function(){

    <?php echo "var data = [".implode($distrib, ",")."];" ?>

    // ...
});

Stringify the array and echo a var declaration to the javascript.

Upvotes: 0

martinwnet
martinwnet

Reputation: 581

You haven't mentioned what values you want, or where in the javascript you want them.

You seem to be familiar with echoing PHP values, so you would just want to do the same except do it in your javascript function.

Upvotes: 0

alanmanderson
alanmanderson

Reputation: 8200

I'm not positive if this is what you are looking for but:

Suppose you have $var1=25 in php. Suppose you want that saved in var1 for your js file you would do:

<?php
    echo $var1=25; 
    echo "var var1=$var1";
?>

Please keep in mind javascript is Client side and php is server side, thus the 2 do not interact (excluding ajax, etc.). PHP is ran before sending the response and javascript is ran in the browser. You may already know that, but just for the doubt.

Upvotes: 0

Related Questions