简体   繁体   English

JavaScript不回显来自数据库的值

[英]JavaScript does not echo values coming from database

The first thing I get values from database with no problem. 我首先从数据库中获取值没有问题。 Then I can echo the values but after The values turn in to null. 然后,我可以回显这些值,但是在这些值变为空之后。 Somehow the values do not pass this point. 值以某种方式没有超过这一点。 Here are my codes. 这是我的代码。

php and mysql part 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++;


        }

No problem till this point. 到此为止没有问题。 Here is the rest of the code that I am having problem 这是我遇到问题的其余代码

<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> 

here where I stuck <?php echo json_encode($row['ad']); ?> 这里我卡住<?php echo json_encode($row['ad']); ?> <?php echo json_encode($row['ad']); ?> is getting no value <?php echo json_encode($row['ad']); ?>没有任何价值

Your $rows array indexed array which contain your keys in every index. 您的$ rows数组索引数组包含每个索引中的键。 So you need to extract your keys value pair from array. 因此,您需要从数组中提取键值对。

After your while loop completed, add following codes while循环完成后,添加以下代码

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

Now use this in your jS 现在在您的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> 

Your array is multidimensional array so simple use array_column to get specific column value from each index and apply json_encode() 您的数组是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