我怎样才能从php中的数据库中获取表格到javascript?

时间:2015-07-13 11:54:27

标签: javascript php mysql json echo

我已将php中的值设置为两个表,我将其与数据库连接但是我希望将此表添加到我的javascript中

$tab=array(0,0,0,0,0,0,0,0,0,0,0,0);
$tab1=array(0,0,0,0,0,0,0,0,0,0,0,0);
while($row=mysql_fetch_assoc($fetch)){
    $tab[$row['mois']-1]=$row['Num_ticketORSLA'];
    $tab1[$row['mois']-1]=$row['Num_ticketSLA'];        
}

我试图在javascript中将此表中的值转换为另一个,我使用echo json_encode:

var getvalue1 = <?php echo json_encode($tab1) ?>;

但它无法正常工作。 编辑: 这是我的代码

<?php
    // Connect to database server
        mysql_connect("localhost" , "root" , "")or die (mysql_error());

    // Select database
        mysql_select_db("test") or die(mysql_error());


    // SQL query
        $strSQL = "SELECT MONTH(ost_ticket.created) as mois ,
                (SELECT count(*) FROM ost_ticket WHERE  ost_ticket.sla_id = 0  AND MONTH(created)=mois AND (ost_ticket.status_id=2 OR ost_ticket.status_id=3) ) as Num_ticketORSLA ,
                (SELECT count(*) FROM ost_ticket WHERE  ost_ticket.sla_id = 1  AND MONTH(created)=mois AND (ost_ticket.status_id=2 OR ost_ticket.status_id=3) ) as Num_ticketSLA ,
                FROM ost_ticket
                WHERE ost_ticket.status_id = 3 OR ost_ticket.status_id = 2 
                GROUP BY mois 
                ORDER BY mois ASC ";

    // Execute the query (the recordset $rs contains the result)
        $rs = mysql_query($strSQL)or die ("could not find");

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
            $tab=array(0,0,0,0,0,0,0,0,0,0,0,0);
            $tab1=array(0,0,0,0,0,0,0,0,0,0,0,0);
                while($row=mysql_fetch_assoc($fetch)){
                    $tab[$row['mois']-1]=$row['Num_ticketORSLA'];
                    $tab1[$row['mois']-1]=$row['Num_ticketSLA'];        
                }


    // Close the database connection
    mysql_close();?>

<script>



var getvalue = JSON.parse("<?php echo json_encode($tab) ?>")
var getvalue1 = <?php echo json_encode($tab1) ?>;


var barChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data : [getvalue[0],getvalue[1],getvalue[2],getvalue[3],getvalue[4],getvalue[5],getvalue[6]]


        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,0.8)",
            highlightFill : "rgba(151,187,205,0.75)",
            highlightStroke : "rgba(151,187,205,1)",
            data : [getvalue1[0],getvalue1[1],getvalue1[2],getvalue1[3],getvalue1[4],getvalue1[5],getvalue1[6]]

        }
    ]

}
window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx).Bar(barChartData, {
        responsive : true
    });
}


</script>

0 个答案:

没有答案
相关问题