0

変数 Var s1、s2、s3 から php および mysql に配列値を挿入する方法:

$(function () {

    var s1 = [100, 200, 300]; //How to Get Value from mysql database
    var s2 = [30, 80, 90]; //How to Get Value from mysql database
    var s3 = [120, 90, 80]; //How to Get Value from mysql database

    // Can specify a custom tick Array.
    // Ticks should match up one for each y value (category) in the series.

    var ticks = ['2010', '2011', '2012'];
    var plot1 = $.jqplot('chart3', [s1, s2, s3], {

        // The "seriesDefaults" option is an options object that will
        // be applied to all series in the chart.

        seriesDefaults: {
            shadow: true, // show shadow or not.
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                fillToZero: true
            }
        },

        // Custom labels for the series are specified with the "label"
        // option on the series option.  Here a series option object
        // is specified for each series.

        series: [
            {label: 'Hotel'},
            {label: 'Event Regristration'},
            {label: 'Airfare'}
        ],

        // Show the legend and put it outside the grid, but inside the
        // plot container, shrinking the grid to accomodate the legend.
        // A value of "outside" would not shrink the grid and allow
        // the legend to overflow the container.

        legend: {
            show: true,
            placement: 'outsideGrid'
        },
        axes: {
            // Use a category axis on the x axis and use our custom ticks.
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: ticks
            },
            // Pad the y axis just a little so bars can get close to, but
            // not touch, the grid boundaries.  1.2 is the default padding.
            yaxis: {
                pad: 1.05,
                tickOptions: {
                    formatString: '$%d'
                }
            }
        },
        grid: {
            borderColor: '#000', // CSS color spec for border around grid.
            borderWidth: 2.0, // pixel width of border around grid.
            shadow: true // draw a shadow for grid.
        }
    });
    // Bind a listener to the "jqplotDataClick" event.  Here, simply change
    // the text of the info3 element to show what series and ponit were
    // clicked along with the data for that point.
    $('#chart3').bind('jqplotDataClick',

    function (ev, seriesIndex, pointIndex, data) {
        $('#info3').html('series: ' + seriesIndex + ', point: ' + pointIndex + ', data: ' + data);
    });
});
4

3 に答える 3

0

2 つの方法:

アヤックス

使用: $.getJSON ( http://api.jquery.com/jQuery.getJSON/ )

var ses = {};

$.getJSON('page_adress.php', {variable_you_want_to_pass1: 'its value', variable_you_want_to_pass2: 'var 2 value'}, function(data) {
    ses = data;
});

あなたのPHPで:

<?php
$passed_var_1 = $_REQUEST['variable_you_want_to_pass1'];
//.... etc


//Here you get your data from mysql, cast it into array

header('Content-type: application/json');
echo json_encode($dbdata);
?>

したがって、基本的にリクエストが完了すると、PHP で持っていた正確な配列が JavaScript に転送されます。この手法は AJAX を使用することに注意してください。それを避けたい場合は、2 番目の手法を使用する必要があります。

JS を動的に作成する

PHP に JavaScript を生成させます。この場合、メインページにあるでしょう

<script src="js_data.js.php" type="text/javascript"></script>

あなたのjs_data.js.phpファイルで:

<?php
header("content-type: application/x-javascript");

$s1 = array(100,200,300);
//....

var s1 = [<?=implode(', ', $s1)?>],
    s2 = [<?=implode(', ', $s2)?>],
    s3 = [<?=implode(', ', $s3)?>];

?>
于 2012-12-21T10:44:04.413 に答える
0

最初の方法 (ajax と json なし)(untidy-way)

最初にデータベースから値を取得し、それを PHP 変数に入れます。
次に、ページに html 要素を配置し、それに値を割り当てます。
次に、document.getElement メソッドを使用して JavaScript で使用します。

// $valueFrmDB のデータベースから値を取得したと仮定します。

$valueFrmDB;

ここで、html 要素を取得します (複数取得する必要がある場合があります)。

<input type="hidden" id="something" name="something" value="echo value of $valueFrmDB here" />;

次に、ジャバスクリプトで

var vfd = document.getElementById('something').value;

文字列を配列に変換する

2番目の方法(ajaxとjsonを使用)(シンプルで正しいが、ajaxとjsonを知っている必要があります)

ajaxを使用してデータベースから値を取得し、
jsonを使用してその値をjavascriptに渡します

于 2012-12-21T10:53:41.260 に答える
0

これは次の方法で簡単に実行できます。

<?php
   $query = mysql_query("SELECT * FROM attendence");
   $results = array(array());

   while($line = mysql_fetch_array($query)){
    $results[] = $line;
   }
?>

Javascript

  <script type="text/javascript">
     $(document).ready(function(){
     var data = <?php echo json_encode($results); ?>; //array uses here
     var plot1 = jQuery.jqplot ('chart1', [data], 
       { 
       seriesDefaults: {
       renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
        showDataLabels: true}
       }, 
      legend: { show:true, location: 'e' }
      });
     });
  </script>
于 2013-02-06T09:55:55.990 に答える