链接到PHP变量

时间:2013-07-12 15:14:50

标签: php jquery datatables

我正在使用Datatables.js,并且我使用来自PHP文件的JSON填充表。我目前的设置是:

  

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代码中的JUST THE JSON?这有可能吗?有没有更好的方法来做到这一点? (这并不需要疯狂的PHP脚本,因为我不太了解它。)


解决方案:这是我在John的建议之后的代码。我还必须将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>

1 个答案:

答案 0 :(得分:1)

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

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

甚至更好地回应PHP脚本本身的javascript。