2013-07-12 93 views
1

我正在使用Datatables.js,并使用来自PHP文件的JSON填充表。我目前的设置是这样的:链接到PHP变量

PHP文件生成JSON> JS文件被链接到 以前的PHP文件初始化表> PHP文件,显示图形(与HTML这是PHP 因此我可以添加页眉/页脚)。 。

这个问题是我有30个图。 30个用于初始化的JSON + 30个JS文件的PHP文件,以及30个用于显示图形的PHP文件。现在,这已经是一个荒谬的文件数量(在我看来),但我需要添加更多的表格。

在我现在的每个表中,我将有一个新的列,其中有一个链接将同一行中另一列的值传递给URL。例如,在Datatable的其中一列中,值为1983.另一列中的链接为/query.php?value=1983

我想要做的是将此变量传递给生成JSON的PHP文件,以便我可以使用该变量更改查询。这将是PHP代码

<?php 
$myServer = "server"; 
$myDB = "database"; 

$conn = sqlsrv_connect ($myServer, array('Database'=>$myDB)); 

$value = $_GET['value']; 

$sql ="SELECT year, value 
     FROM database.dbo.table 
     WHERE year = $value"; 

$data = sqlsrv_query ($conn, $sql); 

$result = array(); 

do { 
    while ($row = sqlsrv_fetch_array ($data, SQLSRV_FETCH_ASSOC)) { 
     $result[] = $row; 
    } 
} while (sqlsrv_next_result($data)); 

$json = json_encode ($result); 

sqlsrv_free_stmt ($data); 
sqlsrv_close ($conn); 
?> 

这适当地生成JSON。但是,当用户点击表格中的链接时,我想将它们带到一个新页面,并在其中放置一个新表格。然而,我拥有它的方式,链接将我带到生成的JSON。所以我的解决办法是合并产生的JSON和显示表中的PHP文件中的PHP,像这样

<?php //Insert the code I posted above ?> 

<!DOCTYPE html> 

<html> 
    <head> 
     <!-- Included files, title, etc... --> 
    </head> 

    <body> 
     <?php include '../common/header.inc' ?> 

     <div class="container"> 
      <table id="chart" style="clear: both"> 
       <thead> 
      </thead> 

       <tbody> 
        <tr> 
         <td colspan="3" class="dataTables_empty">There doesn't seem to be anything here!</td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 

     <?php include '../common/footer.inc'?> 

     <script src="/js/main.js"></script> 
     <script src="table.js"></script> <!-- This is the Datatable initialization --> 
    </body> 
</html> 

初始化应该是这样那么

$(document).ready(function() { 
    var header = [ // This puts the data in the right column 
     { "sTitle": "Year", "mData": "label", "sClass": "center" }, 
     { "sTitle": "Length", "mData": "value", "sClass": "center" } 
    ] 

    var oTable = $('#chart').dataTable({ 
     "bProcessing": true, 
     "sPaginationType": "full_numbers", 
     "sAjaxSource": "query.php", // Loads the JSON script 
     "sAjaxDataProp": "", 
     "aoColumns": header, 

     "sDom": 'T<"clear">Rlfrtip', 
     "oTableTools": { 
      "sSwfPath": "/media/swf/copy_csv_xls_pdf.swf", 
      "sRowSelect": "multi", 
      "aButtons": ["select_all", "select_none", 
       { 
        "sExtends": "collection", 
        "sButtonText": "Export Selected Rows", 
        "aButtons": [ 
         {"sExtends": "copy", "bSelectedOnly": true, "mColumns": [0, 1] }, 
         { "sExtends": "csv", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
         { "sExtends": "xls", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
         { "sExtends": "pdf", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
        ] 
       }, 
       { "sExtends": "print", "sButtonText": "Print View" } 
      ] 
     } 
    }); 
}); 

的问题,这是初始化是读取JSON 下面的HTML代码。

所以基本上我在问什么,是否有任何方法可以将JSON保存在一个变量中,这样我就可以链接到JS代码中的JSON JSON?这甚至可能吗?有没有更好的方法来做到这一点? (这不需要疯狂的PHP脚本,因为我不太了解它)。


SOLUTION:这是约翰的建议后,我的代码。我也必须将sAjaxSource更改为aaData。但现在它起作用了!

<?php 
$myServer = "server"; 
$myDB = "database"; 

$conn = sqlsrv_connect ($myServer, array('Database'=>$myDB)); 

$value = $_GET['value']; 

$sql ="SELECT year, value 
     FROM database.dbo.table 
     WHERE year = $value"; 

$data = sqlsrv_query ($conn, $sql); 

$result = array(); 

do { 
    while ($row = sqlsrv_fetch_array ($data, SQLSRV_FETCH_ASSOC)) { 
     $result[] = $row; 
    } 
} while (sqlsrv_next_result($data)); 

$json = json_encode ($result); 

sqlsrv_free_stmt ($data); 
sqlsrv_close ($conn); 
?> 

<!DOCTYPE html> 

<html> 
    <head> 
     <!-- Included files, title, etc... --> 
    </head> 

    <body> 
     <?php include '../common/header.inc' ?> 

     <div class="container"> 
      <table id="chart" style="clear: both"> 
       <thead> 
      </thead> 

       <tbody> 
        <tr> 
         <td colspan="3" class="dataTables_empty">There doesn't seem to be anything here!</td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 

     <?php include '../common/footer.inc'?> 

     <script src="/js/main.js"></script> 
     <script type="text/javacript"> 
      $(document).ready(function() { 
       var json = <?php echo $json ?>; 
       var header = [ // This puts the data in the right column 
        { "sTitle": "Year", "mData": "label", "sClass": "center" }, 
        { "sTitle": "Length", "mData": "value", "sClass": "center" } 
       ] 

       var oTable = $('#chart').dataTable({ 
        "bProcessing": true, 
        "sPaginationType": "full_numbers", 
        "aaData": json, // Loads the JSON script 
        "sAjaxDataProp": "", 
        "aoColumns": header, 

        "sDom": 'T<"clear">Rlfrtip', 
        "oTableTools": { 
         "sSwfPath": "/media/swf/copy_csv_xls_pdf.swf", 
         "sRowSelect": "multi", 
         "aButtons": ["select_all", "select_none", 
          { 
           "sExtends": "collection", 
           "sButtonText": "Export Selected Rows", 
           "aButtons": [ 
            {"sExtends": "copy", "bSelectedOnly": true, "mColumns": [0, 1] }, 
            { "sExtends": "csv", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
            { "sExtends": "xls", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
            { "sExtends": "pdf", "bSelectedOnly": true, "mColumns": [0, 1], "bFooter": false }, 
           ] 
          }, 
         { "sExtends": "print", "sButtonText": "Print View" } 
         ] 
        } 
       }); 
      }); 
     </script> 
    </body> 
</html> 
+0

该脚本只是回应json。你需要将它存储到一个JavaScript变量然后使用它。 – ikromm

+0

是的,但我如何将变量链接到初始化,以便我可以在Datatable中使用它?我更新了代码,将其显示在一个变量 – TFischer

+0

'' 甚至更​​好从PHP脚本回声JavaScript我 – ikromm

回答

1

该脚本只是回应json。你需要将它存储到一个JavaScript变量然后使用它。

<script type="text/javascript"> 
    var json = <?php contents go here?>; 
</script>` 

甚至更​​好地从PHP脚本本身回显JavaScript。