2017-08-12 58 views
-1

我從數據庫中獲得值的第一件事沒有問題。然後我可以回顯值,但之後值變爲空。不知何故,這些值不會通過這一點。這是我的代碼。JavaScript不會回顯來自數據庫的值

PHP和MySQL部分

$rows = array(); 
$result = mysql_query("SELECT * FROM kayitlar"); 
$i=1; 
while($row = mysql_fetch_array($result)) { 
    $rows []= array(
    'id' => $row['id'], 
    'ad' => $row['ad'], 
    'saat' => $row['saat'], 
);   

     $i++; 


     } 

沒有問題,直到這一點。下面是我有問題,在這裏

<script type="text/javascript"> 
    window.onload = function() { 
    var chart = new CanvasJS.Chart("chartContainer", 
    { 
     title:{ 
     text: "title"  
     }, 
     animationEnabled: true, 
     axisY: { 
     title: "Zaman (saat)" 
     }, 
     legend: { 
     verticalAlign: "bottom", 
     horizontalAlign: "center" 
     }, 
     theme: "theme2", 
     data: [ 

     {   
     type: "column", 
     showInLegend: true, 
     legendMarkerColor: "grey", 
     legendText: "saat", 
     dataPoints: [  

     {y:<?php echo json_encode($row['ad']); ?>, label: "<?php echo json_encode($row['saat']); ?> "}, 


     ] 
     } 
     ] 
    }); 

    chart.render(); 
    } 

    </script> 

的代碼的其餘部分在哪裏卡住<?php echo json_encode($row['ad']); ?>越來越沒有價值

+1

請勿使用*棄用和不安全*'mysql_ *'功能。從PHP 5.5(2013年)開始,它們已被棄用,並且在PHP 7中(2015年)完全刪除。改用MySQLi或PDO。 –

+1

@MagnusEriksson如果他低於PHP 5.5 lol –

+2

@OliverNi然後他應該_really_升級,因爲這些版本多年來都沒有得到支持。另外,您仍然可以在舊版本上使用MySQLI和PDO。無論您使用的是什麼PHP版本,「mysql_ *」功能都很差且不安全。 –

回答

1

你$行數組索引數組包含每個索引你的鑰匙。所以你需要從數組中提取你的鍵值對。

while循環結束後,添加以下代碼

$arr['ad'] = array_column($rows,"ad"); 
$arr['saat'] = array_column($rows,"saat"); 
$arr['id'] = array_column($rows,"id"); 

現在,在您的JS使用

<script type="text/javascript"> 
    window.onload = function() { 
    var chart = new CanvasJS.Chart("chartContainer", 
    { 
     title:{ 
     text: "title"  
     }, 
     animationEnabled: true, 
     axisY: { 
     title: "Zaman (saat)" 
     }, 
     legend: { 
     verticalAlign: "bottom", 
     horizontalAlign: "center" 
     }, 
     theme: "theme2", 
     data: [ 

     {   
     type: "column", 
     showInLegend: true, 
     legendMarkerColor: "grey", 
     legendText: "saat", 
     dataPoints: [  

     {y:<?php echo json_encode($arr['ad']); ?>, label: "<?php echo json_encode($arr['saat']); ?> "}, 


     ] 
     } 
     ] 
    }); 

    chart.render(); 
    } 

    </script> 
1

你的陣列是multidimensional array這麼簡單使用array_column從各指標得到specific column價值和應用json_encode()

<?php echo json_encode(array_column($rows,'ad')); ?> 
<?php echo json_encode(array_column($rows,'saat')); ?>