简体   繁体   English

<script> not working in php environment

[英]<script> not working in php environment

I have a javascript that calls for pagination from datatables.net I have tried running in on a full html file and it works like a charm. 我有一个JavaScript,它要求从datatables.net进行分页,而我尝试在完整的html文件中运行它,它的工作原理就像一个魅力。 The trial code is as follows 试用代码如下

<!DOCTYPE html>
<html>
    <head>
        <link href="//datatables.net/download/build/nightly/jquery.dataTables.css" rel="stylesheet" type="text/css" />

        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="//datatables.net/download/build/nightly/jquery.dataTables.js"></script>
        <script type="text/javascript">
    $(document).ready( function () {
    var table = $('#example').DataTable();
} );
</script>
<style type="text/css">
table{
    border-collapse: collapse;
}
th {
        font-size:13px;
    border-width: 2px;
    padding: 8px;
    border-style: solid;
    border-color: #000000;
    background-color: #E6E6FF;
}
td {
    border-width: 2px;
    padding: 8px;
    border-style: solid;
    border-color: #000000;
    background-color: #E0F0FF;
}
</style>
    </head>
    <body>

            <table id="example" class="display" width="100%">

                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Position</th>
                        <th>Office</th>
                        <th>Age</th>
                        <th>Start date</th>
                        <th>Salary</th>
                    </tr>
                </thead>


            </table>

    </body>
</html>

But however when i tried running it in a php environment, the pagination from the script did not appear. 但是,当我尝试在php环境中运行它时,脚本中的分页没有出现。 What is causing it not to work? 是什么导致它不起作用? Because I have sql queries in the php code. 因为我在php代码中有sql查询。 I print the queries into a html table. 我将查询打印到html表中。 So it should work like the trial code right? 所以它应该像试用代码一样工作吗? Some extract from the php code is as follows : 摘录自php代码,如下所示:

<!DOCTYPE html>
<html>
    <head>
        <link href="//datatables.net/download/build/nightly/jquery.dataTables.css" rel="stylesheet" type="text/css" />

        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="//datatables.net/download/build/nightly/jquery.dataTables.js"></script>
        <script type="text/javascript">
    $(document).ready( function () {
    var table = $('#example').DataTable();
} );
</script>
<style type="text/css">
table{
    border-collapse: collapse;
}
th {
        font-size:13px;
    border-width: 2px;
    padding: 8px;
    border-style: solid;
    border-color: #000000;
    background-color: #E6E6FF;
}
td {
    border-width: 2px;
    padding: 8px;
    border-style: solid;
    border-color: #000000;
    background-color: #E0F0FF;
}
</style>
    </head>

<body>

<?php

echo "<table id='example' class='display' width='100%'>

<tr>
<th>Nama Badan</th>
<th>Negeri</th>
<th>Jenis Sukan</th>
<th>Kategori</th>
<th>Status</th>
</tr>";

while($row = mysqli_fetch_array($query)){
    echo "<tr>";
echo "<td>". $row['NamaBadan'] ."</td>";
echo "<td>". $row['STATE_NAME'] ."</td>";
echo "<td>". $row['JenisSukan'] ."</td>";
echo "<td>". $row['KategoriSukantext'] ."</td>";
echo "<td>". $row['Status'] ."</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

</body>
</html>

您应该在第一个<tr>周围使用<thead>标记,以使Datatables加载其对象。

change it to: 更改为:

echo "<table id='example' class='display' width='100%'>

<thead>
<tr>
<th>Nama Badan</th>
<th>Negeri</th>
<th>Jenis Sukan</th>
<th>Kategori</th>
<th>Status</th>
</tr>
</thead>";

That code might not work beacuse this kind of echo won't work: 该代码可能无法工作,因为这种回声将无法工作:

echo "<table id='example' class='display' width='100%'>";

You're writing html attribute with ' instead of ". Try this: 您正在使用'而不是“编写html属性。请尝试以下操作:

echo '<table id="example" class="display" width="100%">';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM