2016-11-27 109 views
0

首先我有以下MySQL表名作为store_itemsPHP数组的Javascript在一个循环

id ref store item qty sell 
1 2 m1 001 1 12.00 
2 2 m1 002 3 12.00 
3 3 m3 004 4 5.00 
4 3 m3 003 8 10.00 

并配有简单的PHP代码上述行转换为数组开始

<?php 
$query = "SELECT * FROM store_items ORDER by store Asc"; 
$result = mysql_query($query); 
while ($row = mysql_fetch_assoc($result)){ 
    $data[] = $row; 
} 

//This is for moving PHP array to JS array ? Not sure if it is correct 
$js_arr = json_encode($data); 

?> 

现在我一直在寻找一种方式而不是将它显示为表格,所以我想将这些结果放在一个Excel外观里,所以我发现这个插件叫做:Javascript Handsontable

移动的,将样式表和脚本后:

<script src="http://localhost/handsontable-master/dist/handsontable.full.js"></script> 
<link rel="stylesheet" media="screen" href="http://localhost/handsontable-master/dist/handsontable.full.css"> 
<link rel="stylesheet" media="screen" href="http://localhost/handsontable-master/demo/css/samples.css"> 
<link rel="stylesheet" media="screen" href="http://localhost/handsontable-master/demo/css/samples.css"> 
<link rel="stylesheet" media="screen" href="http://localhost/handsontable-master/demo/css/samples.css"> 

<style type="text/css"> 
body {background: white; margin: auto;} 
h2 {margin: 20px 0;} 
</style> 


<div id="example" class="handsontable htColumnHeaders"></div> 

于是问题就出现在这里:

<script> 
var array_code = '<?php echo $js_arr; ?>'; 
//Alerting result to make sure its correct 
alert(array_code); 

$(document).ready(function() { 

//Not sure how to display the results here ??? 
    var 
    data = 
    [ 
    array_code 
    ], 

    container = document.getElementById('example'), 
    hot; 

    hot = new Handsontable(container, { 
    data: data, 
    minSpareRows: 1, 
    colHeaders: true, 
    contextMenu: true 
    }); 


    function bindDumpButton() { 

     Handsontable.Dom.addEvent(document.body, 'click', function (e) { 

     var element = e.target || e.srcElement; 

     if (element.nodeName == "BUTTON" && element.name == 'dump') { 
      var name = element.getAttribute('data-dump'); 
      var instance = element.getAttribute('data-instance'); 
      var hot = window[instance]; 
      console.log('data of ' + name, hot.getData()); 
     } 
     }); 
    } 
    bindDumpButton(); 

}); 

但我不能达到正确显示;什么我的目标是这样的:

Correct result

但当var data数组的值是这样

var 
    data = [ 
     ['1', '2', 'm1', '001', '1', '12.00'], 
     ['2', '2', 'm1', '002', '3', '12.00'], 
     ['3', '3', 'm3', '004', '4', '5.00'], 

    ], 

我怎样才能把里面的PHP数组值上图所示正确JS数组是否正确?

任何建议,将不胜感激。

+0

$ js_arr = json_encode($ data)会生成JSON格式的数据。 – Perumal

+0

@ Perumal93对不起,我在这里错过了一些东西,是否正确? –

+1

你想要将数组数据直接放入表中并显示它们? – Perumal

回答

0

也许我失去了一些东西(在这种情况下评论,我会编辑我的答案),但我认为你只需要更换:

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

有:

while ($row = mysql_fetch_row($result)){ 
    $data[] = $row; 
} 

和你的“数据”应该是完全array_code

+0

让我试试看,只需一秒钟。 –

+0

在将assoc关联到行后,警告框会正确显示结果,但在表格中显示结果时,会将每个单个字符/数字放在单元格中。如果你想我会张贴结果的图片 –

0

你不需要使用$js_arr = json_encode($data);显示HTML表格中的数据,因为它会产生JSON格式的数据,如果你不打算去m使用JavaScript来调整表格,你可以避免这样做。只需添加以下HTML代码并根据需要添加样式:

<table> 
    <thead> 
     <th>A</th> 
     <th>B</th> 
     <th>C</th> 
     <th>D</th> 
     <th>E</th> 
     <th>F</th> 
    </thead> 
    <tbody> 
     <?php if(!empty($data)): ?> 
      <?php foreach($data as $row): ?> 
       <tr> 
        <td><?= $row['id'] ?></td> 
        <td><?= $row['ref'] ?></td> 
        <td><?= $row['store'] ?></td> 
        <td><?= $row['item'] ?></td> 
        <td><?= $row['qty'] ?></td> 
        <td><?= $row['sell'] ?></td> 
       </tr> 
      <?php endforeach; ?> 
     <?php endif; ?> 
    </tbody> 
</table>