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

时间:2022-01-04 03:03:24

I have a database of jobs listed by their jobID, and what I've set up is a search box where you can enter the jobID number and it brings up the relevant details for that number. What I want to do is be able to enter the first few digits of the jobID and have it filter all of the jobID's that contain those digits.

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

Is this possible without JavaScript or a j query?

没有JavaScript或j查询,这可能吗?

This is my code for the search box and for the table that's inputting the results, respectively..

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

<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>

And the results table..

结果表..

<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>

3 个解决方案

#1


1  

You can use dataTable plugin which will do the sorting ,searching for you.Just see the example in the following link

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

https://www.datatables.net/

You don't need to reconfigure so much.Just download the dataTable plugin add the css and js to your php file and in your script just add following two lines

你不需要重新配置这么多。只需下载dataTable插件将css和js添加到你的php文件中并在你的脚本中添加以下两行

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

hope that it will help.Thanks

希望它会有所帮助。谢谢

#2


0  

Ok, so you already have a script that is loading search page and prepends a variable jobID to the url.

好的,所以你已经有了一个加载搜索页面的脚本,并在文件中添加了一个变量jobID。

Now on a search.php page you need to add

现在在search.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();
}


}

#3


0  

You can simply use an HTML form. I can see with your javascript that you want to pass the variable through the URL. This can be done using method GET in your form.

您只需使用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>

Then, on your search page, you can use something like this,

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

Using 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>

Or using mysqli:

或者使用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>

For mysqli, $conn is the name of your db connection. For PDO, $pdo is the name of your db connection.

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

I recommend using the following to sanitize your variable before running through SQL.

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

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

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

#1


1  

You can use dataTable plugin which will do the sorting ,searching for you.Just see the example in the following link

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

https://www.datatables.net/

You don't need to reconfigure so much.Just download the dataTable plugin add the css and js to your php file and in your script just add following two lines

你不需要重新配置这么多。只需下载dataTable插件将css和js添加到你的php文件中并在你的脚本中添加以下两行

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

hope that it will help.Thanks

希望它会有所帮助。谢谢

#2


0  

Ok, so you already have a script that is loading search page and prepends a variable jobID to the url.

好的,所以你已经有了一个加载搜索页面的脚本,并在文件中添加了一个变量jobID。

Now on a search.php page you need to add

现在在search.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();
}


}

#3


0  

You can simply use an HTML form. I can see with your javascript that you want to pass the variable through the URL. This can be done using method GET in your form.

您只需使用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>

Then, on your search page, you can use something like this,

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

Using 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>

Or using mysqli:

或者使用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>

For mysqli, $conn is the name of your db connection. For PDO, $pdo is the name of your db connection.

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

I recommend using the following to sanitize your variable before running through SQL.

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

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

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