过滤包含一些文本的 PHP 数据表


Filter table of PHP data that contains some text

我有一个按他们的jobID列出的工作数据库,我设置的是一个搜索框,您可以在其中输入jobID号,它会显示该号码的相关详细信息。我想做的是能够输入jobID的前几个数字,并让它过滤所有包含这些数字的jobID。

没有JavaScript或j查询可以吗?

这是我分别用于搜索框和输入结果的表的代码。

<script type="text/javascript">
    function search () {
    var jobSearch = document.getElementById('jobSearch').value;
    window.location.href = ('search.php?jobID='+jobSearch);
}
</script>
<input id="jobSearch" placeholder="Search Job No.."><button onClick="search();">Search</button></input>

和结果表..

<table class="auto details" data-type="table" data-other="" data-filter="jobs.jobID=<?php echo $_GET['jobID']; ?>" data-columns="jobID,customerName,machineName,staffName,operation,notes,operationStatus,pallets"></table>

您可以使用dataTable插件进行排序,搜索。只需查看以下链接中的示例

https://www.datatables.net/

您不需要重新配置太多。只需下载dataTable插件,将css和js添加到您的php文件中,然后在脚本中添加以下两行

$(document).ready(function() {
    $('#example').DataTable(); //replace the #example and put your table ID here
} );

希望它会有所帮助。谢谢

好的,所以你已经有一个脚本正在加载搜索页面,并在URL前面附加一个变量jobID。

现在在搜索.php页面上,您需要添加

if(isset($_GET['jobID'])) { // this checks if job id was set
//and place your code here to grad bata from the database and output
//below is just an example of getting data from the database:
$jobID = mysql_real_escape_string($_GET['jobID']);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT * from jobs WHERE job.id like '%{$jobID}%' ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
    /* извлечение ассоциативного массива */
    while ($row = $result->fetch_assoc()) {
        echo $row['title'];
    }
    $result->free();
}
}

您可以简单地使用 HTML 表单。我可以看到你的javascript你想通过URL传递变量。这可以使用表单中的方法 GET 来完成。

<form method="GET" action="search.php">
    <input type="text" name="jobID" placeholder="Enter Job ID">
    <button type="submit" value="Submit">Submit</button>
</form>

然后,在您的搜索页面上,您可以使用这样的东西,

使用 PDO:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "jobs";
// Connect to DB
$pdo = new PDO('mysql:host=' . $servername . ';dbname=' . $dbname, $username, $password);
// Process search
if(isset($_GET['jobID'])) {
    $jobID = trim(stripslashes(strip_tags(preg_replace('/'D/', '', $_GET['jobID']))));
    $stmt = $pdo->prepare("SELECT * FROM jobs WHERE jobID LIKE '%$jobID%' LIMIT 100");
    $stmt->execute();
    $jobs = "";
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $jobs .= '<p>' . $row["jobID"] . '</p>';
    }
}
?>
<div class="jobs-list">
    <?php print $jobs; ?>
</div>

或者使用 mysqli:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "jobs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if(isset($_GET['jobID'])) {
    $jobID = trim(stripslashes(strip_tags(preg_replace('/'D/', '', $_GET['jobID']))));
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    $jobs = "";
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $jobs .= '<p>' . $row["jobID"] . '</p>';
        }
    } else {
        $jobs = '<p>0 results</p>';
    }
    $conn->close();
}
?>
<div class="jobs-list">
    <?php print $jobs; ?>
</div>

对于 mysqli,$conn 是数据库连接的名称。对于 PDO,$pdo是数据库连接的名称。

我建议在运行 SQL 之前使用以下方法来清理变量。

$jobID = trim(stripslashes(strip_tags(preg_replace('/'D/', '', $_GET['jobID']))));