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);
?>

所以基本上在请求完成后,您将在 JavaScript 中传输您在 PHP 中的确切数组。请记住,此技术使用 AJAX。如果你想避免这种情况,你将不得不使用第二种技术。

动态创建 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

第一种方法(w/o 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" />;

然后,在 javascript

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

将字符串转换为数组

第二种方法(使用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 回答