duanbi3151 2012-12-21 10:24
浏览 46
已采纳

使用var名称中的php和mysql获取值[value1,value2,value2]

How I can insert array value in php and mysql from variable Var s1, s2, s3:

$(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);
    });
});
  • 写回答

3条回答 默认 最新

  • dongyakui8675 2012-12-21 10:44
    关注

    2 ways:

    Ajax

    Use: $.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;
    });
    

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

    So basically after request finishes you will have exact array you had in PHP transferred in JavaScript. Have in mind that this technique uses AJAX. If you want to avoid that, you will have to use second technique.

    Dynamically Creating JS

    Make PHP generate your javascript. In this case you would have in your main page

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

    In your js_data.js.php file:

    <?php
    header("content-type: application/x-javascript");
    
    $s1 = array(100,200,300);
    //....
    
    var s1 = [<?=implode(', ', $s1)?>],
        s2 = [<?=implode(', ', $s2)?>],
        s3 = [<?=implode(', ', $s3)?>];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?