簡體   English   中英

JavaScript不回顯來自數據庫的值

[英]JavaScript does not echo values coming from database

我首先從數據庫中獲取值沒有問題。 然后,我可以回顯這些值,但是在這些值變為空之后。 值以某種方式沒有超過這一點。 這是我的代碼。

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']); ?> <?php echo json_encode($row['ad']); ?>沒有任何價值

您的$ rows數組索引數組包含每個索引中的鍵。 因此,您需要從數組中提取鍵值對。

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> 

您的數組是multidimensional array因此array_column使用array_column從每個索引中獲取specific column值並應用json_encode()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM