php與MySQL與ajax與echart綜合
阿新 • • 發佈:2017-09-27
error: new ror cat val utf too pri div
http://www.yinghualuowu.com/php/echart.html
創建table sex 有name num
<?php ini_set(‘display_errors‘,1); //錯誤信息 ini_set(‘display_startup_errors‘,1); //php啟動錯誤信息 error_reporting(-1); ini_set(‘error_log‘, dirname(__FILE__) . ‘/error3_log.txt‘);//打印出所有的 錯誤信息require("config.php"); $conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting"); mysqli_query($conn,‘set names utf8‘); mysqli_select_db($conn,$mysql_database); $result = mysqli_query($conn,"select * from sex"); $data=""; $array= array(); class User{ public $name; public $num; } while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){ // printf ("%s : %s",$row["name"],$row["num"]); $user=new User(); $user->name=$row[‘name‘]; $user->num=$row[‘num‘]; $array[]=$user; } $data=json_encode($array); echo $data; ?>
這樣,我們就創建了一個json文件
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文檔</title> <script src="../js/echarts.min.js"></script> <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> </head> <body> <div id="main" style="height:400px"></div> <script type="text/javascript"> var myChart = echarts.init(document.getElementById(‘main‘)); var arr1=[],arr2=[]; function arrTest(){ $.ajax({ type:"post", url:"echart.php", dataType:"json", async:false, data:{}, success:function(result){ if (result){ for (var i = 0; i < result.length; i++) { arr1.push(result[i].name); arr2.push(result[i].num); } } }, error:function(errorMsg) { alert("數據讀取失敗"); } }) return arr1,arr2; } arrTest(); console.log(arr1[0]); var option = { tooltip: { show: true }, legend: { data:[‘num‘] }, xAxis : [ { type : ‘category‘, data : arr1 } ], yAxis : [ { type : ‘value‘ } ], series : [ { "name":"num", "type":"bar", "data":arr2 } ] }; myChart.setOption(option); </script> </body> </html>
php與MySQL與ajax與echart綜合