我需要帮助添加筛选选项。

huangapple go评论63阅读模式
英文:

I need a help to add filter option

问题

这是你的代码部分:

<div>
  <label for="status_filter">按状态筛选:</label>
  <select id="status_filter">
    <option value="All">全部</option>
    <option value="Active">活跃</option>
    <option value="Inactive">不活跃</option>
  </select>
</div>

<?php
if(isset($_GET['status'])){
  $status = $_GET['status'];
} else {
  $status = "Active";
}

if($status == "Active"){
  $query = "SELECT * FROM students WHERE status = 'Active'";
} elseif($status == "Inactive"){
  $query = "SELECT * FROM students WHERE status = 'Inactive'";
} else {
  $query = "SELECT * FROM students";
}
?>

希望这有助于你解决过滤选项的问题。如果需要更多帮助,请参考你提供的GitHub链接,查看完整的代码。

英文:

Here you can find my code
https://github.com/fawwash/temp/blob/aff321e94a58fb80e82ae38c2ea5aad47f0b7d37/dashboard.php

&lt;div&gt;
  &lt;label for=&quot;status_filter&quot;&gt;Filter by status:&lt;/label&gt;
  &lt;select id=&quot;status_filter&quot;&gt;
    &lt;option value=&quot;All&quot;&gt;All&lt;/option&gt;
    &lt;option value=&quot;Active&quot;&gt;Active&lt;/option&gt;
    &lt;option value=&quot;Inactive&quot;&gt;Inactive&lt;/option&gt;
  &lt;/select&gt;
&lt;/div&gt;


&lt;?php
if(isset($_GET[&#39;status&#39;])){
  $status = $_GET[&#39;status&#39;];
} else {
  $status = &quot;Active&quot;;
}

if($status == &quot;Active&quot;){
  $query = &quot;SELECT * FROM students WHERE status = &#39;Active&#39;&quot;;
} elseif($status == &quot;Inactive&quot;){
  $query = &quot;SELECT * FROM students WHERE status = &#39;Inactive&#39;&quot;;
} else {
  $query = &quot;SELECT * FROM students&quot;;
}
?&gt;

In my dashboard I tried to set a filter option to filter the students data by their 'status' (All, Active, Inactive)

This is the output of my code:

我需要帮助添加筛选选项。

Actually here the filter option is not working. When I select Active it shows all data, When I select Inactive it shows all data.

Expecting Output: I am expecting the filter should work like if we filter the Active, the active students list should display. If we filter Inactive, inactive students should display. If we filter All, then All (Active and Inactive) students data should visible. The data should be visible in the jquery dataTable. You can refer the github link to refer the full code.

答案1

得分: 1

以下是您要翻译的内容:

You actually don't require a PHP script for filtering. You are already using a jQuery and JavaScript.

First of all, remove the below code from your script. It's not usable.

You have <select id="status_filter"> so while getting table data using ajax use it and send it to post data

"ajax": {
"url": "students_data.php",
"data": {
"teacher_id": "",
"status" : $("#status_filter").val();
}
},

Use the status in your query to filter the data.

Now you need to initialize the datatable script and assign a variable like below.

var table = $('#studentsTable').DataTable({
"ajax": {
"url": "students_data.php",
"data": {
"teacher_id": "",
"status" : $("#status_filter").val();
}
},

Now inside $(document).ready(function() { what you can do is create an event listener for the status_filter when the value is being changed so that you can keep this below code and inside the function you can simply reload the data of the datatable:

$("#status_filter").change(function() {
table.ajax.reload();
});

For reference, I am sending you the entire ready function:

$(document).ready(function() {
var table = $('#studentsTable').DataTable({
"ajax": {
"url": "students_data.php",
"data": {
"teacher_id": "",
"status" : $("#status_filter").val();
}
},
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "phone" },
{ "data": "email" },
{
"data": "status",
"render": function(data, type, row, meta) {
if data == 'Active' {
return '' + data + '';
} else if (data == 'Inactive') {
return '' + data + '';
} else {
return data;
}
}
},
{
"data": "id",
"render": function(data, type, row, meta) {
var status = row.status;
return '';
}
}
],
});

$("#status_filter").change(function() {
table.ajax.reload();
});
});

Your students.php: where you need to add the status in the query for filtering the data.

$data));
} else {
header("Location: login.php");
exit();
}
?>

英文:

You actually don't require a PHP script for filtering. You are already using a jQuery and JavaScript.

First of all, remove the below code from your script. It's not usable.

You have &lt;select id=&quot;status_filter&quot;&gt; so while getting table data using ajax use it and send it to post data

&quot;ajax&quot;: {
                &quot;url&quot;: &quot;students_data.php&quot;,
                &quot;data&quot;: {
                    &quot;teacher_id&quot;: &quot;&lt;?php echo $_SESSION[&#39;teacher_id&#39;]; ?&gt;&quot;,
                    &quot;status&quot; : $(&quot;#status_filter&quot;).val();
                }
            },

Use the status in your query to filter the data.

Now you need to initialize the datatable script and assign a variable like below.

var table = $(&#39;#studentsTable&#39;).DataTable({
            &quot;ajax&quot;: {
                &quot;url&quot;: &quot;students_data.php&quot;,
                &quot;data&quot;: {
                    &quot;teacher_id&quot;: &quot;&lt;?php echo $_SESSION[&#39;teacher_id&#39;]; ?&gt;&quot;,
                    &quot;status&quot; : $(&quot;#status_filter&quot;).val();
                }
            },

Now inside $(document).ready(function() { what you can do it create a eventlistener for the status_filter when the value is being changed so that you can keep this below code and inside function you can simply reload the data of the datatable:

$(&quot;#status_filter&quot;).change(function() {
      table.ajax.reload();
});

For reference, I am sending you the entire ready function:

$(document).ready(function() {
        var table = $(&#39;#studentsTable&#39;).DataTable({
            &quot;ajax&quot;: {
                &quot;url&quot;: &quot;students_data.php&quot;,
                &quot;data&quot;: {
                    &quot;teacher_id&quot;: &quot;&lt;?php echo $_SESSION[&#39;teacher_id&#39;]; ?&gt;&quot;,
                    &quot;status&quot; : $(&quot;#status_filter&quot;).val();
                }
            },
            &quot;columns&quot;: [
                { &quot;data&quot;: &quot;id&quot; },
                { &quot;data&quot;: &quot;name&quot; },
                { &quot;data&quot;: &quot;phone&quot; },
                { &quot;data&quot;: &quot;email&quot; },
                {
                    &quot;data&quot;: &quot;status&quot;,
                    &quot;render&quot;: function(data, type, row, meta) {
                        if (data == &#39;Active&#39;) {
                            return &#39;&lt;span class=&quot;active&quot;&gt;&#39; + data + &#39;&lt;/span&gt;&#39;;
                        } else if (data == &#39;Inactive&#39;) {
                            return &#39;&lt;span class=&quot;inactive&quot;&gt;&#39; + data + &#39;&lt;/span&gt;&#39;;
                        } else {
                            return data;
                        }
                    }
                },
                {
                    &quot;data&quot;: &quot;id&quot;,
                    &quot;render&quot;: function(data, type, row, meta) {
                        var status = row.status;
                        return &#39;&lt;button onclick=&quot;changeStatus(&#39; + data + &#39;, \&#39;&#39; + (status == &#39;Active&#39; ? &#39;Inactive&#39; : &#39;Active&#39;) + &#39;\&#39;)&quot; class=&quot;btn &#39; + (status == &#39;Active&#39; ? &#39;btn-danger&#39; : &#39;btn-success&#39;) + &#39;&quot;&gt;&#39; + (status == &#39;Active&#39; ? &#39;Terminate&#39; : &#39;Activate&#39;) + &#39;&lt;/button&gt;&#39;;
                    }
                }
            ],
        });
        
        $(&quot;#status_filter&quot;).change(function() {
            table.ajax.reload();
        });
    });

Your students.php: where you need to add the status in the query for filtering the data.

&lt;?php 
session_start();
$conn = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;mydatabase&#39;);
if (isset($_SESSION[&#39;username&#39;])) {
    $teacher_id = $_SESSION[&#39;username&#39;];
    $status = $_REQUEST[&#39;status&#39;];
    $query = &quot;SELECT * FROM students WHERE teacher_id IN ( SELECT teacher_id FROM teacher WHERE username = &#39;$teacher_id&#39;) AND status = &#39;&quot;.$status.&quot;&#39;&quot;;
    $result = mysqli_query($conn, $query); $data = array();
    while (
        $row = mysqli_fetch_assoc($result)) { $data[] = $row;
    }
    echo json_encode(array(&quot;data&quot; =&gt; $data));
} else
{
    header(&quot;Location: login.php&quot;); exit();
} ?&gt;

huangapple
  • 本文由 发表于 2023年3月23日 11:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定