doucong3048 2013-08-05 13:39
浏览 41
已采纳

PHP数组转换为Javascript数组

Afternoon all. The code below works perfectly, however, I need to pull each row of the php sql array out and into the script var. Any ideas on how to write a while loop that could do this? Thanks for any help

 var enableDays = ["<?php echo mysql_result($result, 0, 'date'); ?>"];

 enableDays.push("<?php echo mysql_result($result, 1, 'date'); ?>");

Additional Code::

$rows = array();

while ($row = mysql_fetch_assoc($result))
{
    $rows[] = $row;
}



    var enableDays = [<?php echo json_encode($rows); ?>];

    console.log(enableDays[1]);
  • 写回答

4条回答 默认 最新

  • dongxinm279890 2013-08-05 13:46
    关注

    You can get the rows using mysqli_fetch_assoc and then encode it to JSON:

    <?php
    $rows = array();
    
    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
    ?>
    
    
    var enableDays = <?php echo json_encode($rows); ?>;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区