实时更新可搜索数据库MYSQL和PHP

实时更新可搜索数据库MYSQL和PHP,php,mysql,datatable,Php,Mysql,Datatable,Tony,下面显示的更新代码,它不允许我对此进行评论,所以我编辑了我以前的文章,下面的代码没有返回任何结果或错误,它只是重新加载页面。我在代码中看到的唯一问题是,我需要能够按所有表头、评级、名称、区域等进行搜索。我有一个datatable脚本,可以实时更新表以获得结果,但这不适用于PHP表,我不确定如何使其与datatables一起工作,因为我以前从未使用过它: updated code: <html> <head> <body> <style>

Tony,下面显示的更新代码,它不允许我对此进行评论,所以我编辑了我以前的文章,下面的代码没有返回任何结果或错误,它只是重新加载页面。我在代码中看到的唯一问题是,我需要能够按所有表头、评级、名称、区域等进行搜索。我有一个datatable脚本,可以实时更新表以获得结果,但这不适用于PHP表,我不确定如何使其与datatables一起工作,因为我以前从未使用过它:

updated code:

<html>
<head>
<body>
<style>
table { 
color: #333; /* Lighten up font color */
font-family: Helvetica, Arial, sans-serif; /* Nicer font */
width: 100%; 
border-collapse: 
collapse; border-spacing: 0; 
}

td, th { border: 1px solid #00000; height: 30px; } /* Make cells a bit taller */

th {
background: #F3F3F3; /* Light grey background */
font-weight: bold; /* Make sure they're bold */
}

td {
background: #FAFAFA; /* Lighter grey background */
text-align: center; /* Center our text */
}
</style>

<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.dataTables.min.js"></script>

<script type="text/javascript" charset="iso-8859-1">



$(document).ready( function(){
    $('#five_year').dataTable({
    "iDisplayLength": 300,
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]]
    });
$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#five-year tfoot th').each( function () {
        var title = $('#five_year thead th').eq( $(this).index() ).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

    // DataTable
    var table = $('#five_year').DataTable();

    // Apply the search
    table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
            table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );
});

} );


$().ready(function() {
    var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;

    $("table td").filter(function() {
        return $(this).html().match(regEx);
    }).each(function() {
        $(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>"));
    });
});
</script>
<form action="index.php" method="post">
  <input type="text" name="search" placeholder="Search...." />
  <input type="submit" value=">>" />
</form>
<?php
$con=mysqli_connect("localhost","root","windows11","main");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$result = mysqli_query($con,"SELECT * FROM `users` ORDER BY ID asc, Name asc") or die(mysqli_error($con));

echo 
"<table border=1>
<tr>
<th>Rating</th>
<th>Name</th>
<th>Discipline</th>
<th>Rate</th>
<th>Area</th>
<th>Number</th>
<th>Email</th>
</tr>";

while($row = mysqli_fetch_array($result)){

echo "<tr>";
echo "<td>" . $row['Rating'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Discipline'] . "</td>";
echo "<td>" . $row['Rate'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Number'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
echo "</form>";
}
// If there is a search variable try to search database
if( isset( $_POST['search'] ) ) {

                $searchq = $_POST['search'];
                $searchq = preg_replace( "#[^0-9a-z]#i", "", $searchq );
                $sql = "SELECT * FROM `users` WHERE `Rating` LIKE '%$searchq%';";

                if ( $result = mysqli_query( $conn, $sql ) ) {
                    if ( mysqli_num_rows( $result ) > 0 ) {

                         echo '
                         <table class="hoverTable">
                            <tr>
                                <th>Rating</th>
                                <th>Name</th>
                                <th>Discipline</th>
                                <th>Rate</th>
                                <th>Area</th>
                                <th>Number</th>
                                <th>Email</th>
                            </tr>';

                             while( $row = $result->fetch_assoc() ) {
                                 echo "
                                 <tr>
                                    <td>".$row["rating"]."</td>
                                    <td>".$row["name"]."</td>
                                    <td>".$row["discipline"]."</td>
                                    <td>".$row["rate"]."</td>
                                    <td>".$row["area"]."</td>
                                    <td>".$row["number"]."</td>
                                    <td>".$row["email"]."</td>
                                </tr>";
                             }

                         echo '
                         </table>';

                    } else {
                        $message = "0 results";
                    }
                }
                mysqli_free_result( $result );
            }
        $conn->close();
?>



</body>
</head>
</html>
更新代码:
表{
颜色:#333;/*使字体颜色变亮*/
字体系列:Helvetica,Arial,无衬线;/*更好的字体*/
宽度:100%;
边界塌陷:
折叠;边框间距:0;
}
td,th{border:1px solid#00000;height:30px;}/*使单元格稍微高一点*/
th{
背景:#F3;/*浅灰色背景*/
字体大小:粗体;/*确保它们是粗体的*/
}
运输署{
背景:#FAFAFA;/*浅灰色背景*/
文本对齐:居中;/*将文本居中*/
}
$(文档).ready(函数(){
$('五年')。数据表({
“iDisplayLength”:300,
“阿伦提努”:[[5,10,25,50,-1],[5,10,25,50,“全部”]]
});
$(文档).ready(函数(){
//设置-向每个页脚单元格添加文本输入
$('#五年tfoot th')。每个(函数(){
var title=$('公元五年').eq($(this.index()).text();
$(this.html(“”);
} );
//数据表
var表=$('五年').DataTable();
//应用搜索
table.columns().eq(0).each(函数(colIdx){
$('input',table.column(colIdx).footer()).on('keyup change',function(){
桌子
.列(colIdx)
.search(this.value)
.draw();
} );
} );
});
} );
$().ready(函数()){
var regEx=/(\w+([-+.]\w+*@\w+([-.]\w+*\.\w+)/;
$(“表td”).filter(函数(){
返回$(this.html().match(regEx);
}).each(函数({
$(this.html($(this.html().replace)(regEx,“”);
});
});
这也是与HTML表一起使用的Datatables脚本(所有使用HTML拆分为表行的数据都不会被PHP脚本回显:

<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.dataTables.min.js"></script>

<script type="text/javascript" charset="iso-8859-1">



$(document).ready( function(){
    $('#five_year').dataTable({
    "iDisplayLength": 300,
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]]
    });
$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#five-year tfoot th').each( function () {
        var title = $('#five_year thead th').eq( $(this).index() ).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

    // DataTable
    var table = $('#five_year').DataTable();

    // Apply the search
    table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
            table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );
});

} );


$().ready(function() {
    var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;

    $("table td").filter(function() {
        return $(this).html().match(regEx);
    }).each(function() {
        $(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>"));
    });
});
</script>

$(文档).ready(函数(){
$('五年')。数据表({
“iDisplayLength”:300,
“阿伦提努”:[[5,10,25,50,-1],[5,10,25,50,“全部”]]
});
$(文档).ready(函数(){
//设置-向每个页脚单元格添加文本输入
$('#五年tfoot th')。每个(函数(){
var title=$('公元五年').eq($(this.index()).text();
$(this.html(“”);
} );
//数据表
var表=$('五年').DataTable();
//应用搜索
table.columns().eq(0).each(函数(colIdx){
$('input',table.column(colIdx).footer()).on('keyup change',function(){
桌子
.列(colIdx)
.search(this.value)
.draw();
} );
} );
});
} );
$().ready(函数()){
var regEx=/(\w+([-+.]\w+*@\w+([-.]\w+*\.\w+)/;
$(“表td”).filter(函数(){
返回$(this.html().match(regEx);
}).each(函数({
$(this.html($(this.html().replace)(regEx,“”);
});
});
我可以看到这个datatables代码需要一个表ID来引用,但是我不能给PHP代码分配一个表ID,因为当我尝试时,它会使脚本和页面崩溃


谢谢您的帮助。

我在上一个类项目上设置了一个简单的搜索,该项目查询了我数据库中的一个表并返回了结果。如果您认为有帮助,您可以修改并尝试一下:

将搜索放入表单中

// If there is a search variable try to search database
如果(isset($_POST['search'])){

$searchq=$\u POST['search'];
$searchq=preg#u replace(“#[^0-9a-z]#i”,”,$searchq);
$sql=“从'Customers'中选择*,其中'Client'类似于“%$searchq%”;”;
if($result=mysqli_查询($conn,$sql)){
如果(mysqli_num_行($result)>0){
回声'
评级
名称
纪律
比率
地区
数
电子邮件
';
而($row=$result->fetch_assoc()){
回声“
“$row[“评级”]”
“$row[“name”]”
“$row[“规程”]”
“$row[“rate”]”
“$行[“区域”]”
“$row[“zipcode”]”
“$row[“电子邮件”]”
";
}
回声'
';
}否则{
$message=“0结果”;
}
}
mysqli_免费_结果($result);
}
$conn->close();

?>

是的,这是可能的,检查它是否具有执行此操作所需的所有接口。尽管,根据数据量的不同,您可能希望设置一个MyISAM表,其中包含存储在其中的数据类型的特定字段,并链接到原始记录以提高速度。假设您希望异步搜索数百万条记录中的任何内容。我已经查看了在datatables上,但看不到如何用我的表实现这一点。在原始HTML表上,我可以包含一个表id,然后jscript使用它,但是通过这种方式,如果我在任何地方添加表id,它都不起作用,脚本崩溃。这需要吗
                $searchq = $_POST['search'];
                $searchq = preg_replace( "#[^0-9a-z]#i", "", $searchq );
                $sql = "SELECT * FROM `Customers` WHERE `Client` LIKE '%$searchq%';";

                if ( $result = mysqli_query( $conn, $sql ) ) {
                    if ( mysqli_num_rows( $result ) > 0 ) {

                         echo '
                         <table class="hoverTable">
                            <tr>
                                <th>Rating</th>
                                <th>Name</th>
                                <th>Discipline</th>
                                <th>Rate</th>
                                <th>Area</th>
                                <th>Number</th>
                                <th>Email</th>
                            </tr>';

                             while( $row = $result->fetch_assoc() ) {
                                 echo "
                                 <tr>
                                    <td>".$row["rating"]."</td>
                                    <td>".$row["name"]."</td>
                                    <td>".$row["discipline"]."</td>
                                    <td>".$row["rate"]."</td>
                                    <td>".$row["area"]."</td>
                                    <td>".$row["zipcode"]."</td>
                                    <td>".$row["email"]."</td>
                                </tr>";
                             }

                         echo '
                         </table>';

                    } else {
                        $message = "0 results";
                    }
                }
                mysqli_free_result( $result );
            }
        $conn->close();