我已经写了一些CSS和PHP来查询MySQL表。我还具有一个下拉框形式的过滤器,该过滤器允许用户选择一个“系列”,无论是“电容器”,“电阻器”还是“铁氧体磁珠”(我在下面提供了图片,好像)。
我的问题是:一旦元素被家庭过滤,如何为元素创建分类系统?也就是说,例如,如果我想从MySQL查询与“电压”的ASC值相对应的表,我将如何处理?选择排序方法后,我需要保留过滤器。到目前为止,我的代码均已包含在图像下方。感谢您的帮助!
(以下:1,已加载完整表:2,仅加载与“电容器”匹配的系列条目)
代码:(文件名,index.php)
<html>
<form action="index.php" method="post">
<select name="family">
<option value="" selected="selected">Any family</option>
<option value="capacitor">capacitor</option>
<option value="resistor">resistor</option>
<option value="ferrite bead">ferrite bead</option>
</select>
<input name="search" type="submit" value="Search"/>
</form>
<head>
<meta charset = "UTF-8">
<title>test.php</title>
<style>
table {
border-collapse: collapse;
width: 50%;
}
th, td {
input: "text";
text-align: left;
padding: 8px;
}
th {
background-color: SkyBlue;
}
tr:nth-child(odd) {background-color: #f2f2f2;}
tr:hover {background-color: AliceBlue;}
</style>
</head>
<body>
<p>
<?php
$family = "";
if(isset($_POST['family'])) {
$family = $_POST['family'];
}
try {
$con= new PDO('mysql:host=localhost;dbname=mysql', "root", "kelly188");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!empty($family)) {
$query = 'SELECT * FROM testv2 WHERE family = "'.$family.'"';
}
else {
$query = "SELECT * FROM testv2";
}
//first pass just gets the column names
print "<table>";
$result = $con->query($query);
//return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print " <tr>";
foreach ($row as $field => $value){
print " <th>$field</th>";
}
// end foreach
print " </tr>";
//second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print " <tr>";
foreach ($row as $name=>$value){
print " <td>$value</td>";
} //end field loop
print " </tr>";
} //end record loop
print "</table>";
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
?>
</p>
</body>
</html>
答案 0 :(得分:1)
您可以在表单中添加排序下拉列表,并在查询中使用它。这样,您可以让用户选择一种排序方法并在服务器端进行处理。
<form action="index.php" method="post">
<select name="family">
<option value="" selected="selected">Any family</option>
<option value="capacitor">capacitor</option>
<option value="resistor">resistor</option>
<option value="ferrite bead">ferrite bead</option>
</select>
<select name="sort">
<option value="" selected="selected">Any Order</option>
<option value="ASC">Ascending</option>
<option value="DESC">Descending</option>
</select>
<input name="search" type="submit" value="Search"/>
</form>
在PHP中:
<?php
$family = "";
$sort = "";
if(isset($_POST['family'])) {
$family = $_POST['family'];
}
在您的if语句中:
if(!empty($family)) {
$query = 'SELECT * FROM testv2 WHERE family = "'.$family.'" ORDER BY "'.$sort'"';
}
else {
$query = "SELECT * FROM testv2";
}
答案 1 :(得分:1)
如果您不想使用专用的表排序库,则应该可以自己进行。这是一个解决方案,可以从提供的数据数组中提取所有数据,您应该可以使用PHP轻松提供这些数据。
// Initially populate the table
populateTable(data);
// Listen for a click on a sort button
$('.sort').on('click', function() {
// Get the key based on the value of the button
var key = $(this).html();
// Sort the data and update our data
data = sortBy(data, key);
// Fill the table with our data
populateTable(data);
});
// Modified from: https://www.sitepoint.com/sort-array-index/
function sortBy(inputData, key) {
// Sort our data based on the given key
inputData.sort(function(a, b) {
var aVal = a[key],
bVal = b[key];
if (aVal == bVal) return 0;
return aVal > bVal ? 1 : -1;
});
return inputData;
}
// Modified from: https://stackoverflow.com/questions/5361810/fast-way-to-dynamically-fill-table-with-data-from-json-in-javascript
function populateTable(inputData) {
var keys = new Array(),
i = -1;
// Create an array of keys
$.each(inputData[0], function(key, value) {
keys[++i] = key;
});
var r = new Array(),
j = -1;
// Populate the table headers
r[++j] = '<tr>';
$.each(keys, function(key, value) {
r[++j] = '<th>' + keys[key] + '</th>';
});
r[++j] = '</tr>';
for (var index = 0, size = inputData.length; index < size; index++) {
// Populate the table values
r[++j] = '<tr>';
$.each(keys, function(key, value) {
r[++j] = '<td>' + inputData[index][value] + '</td>';
});
r[++j] = '</tr>';
}
// Join everything together
$('#data-table').html(r.join(''));
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
text-align: left;
padding: 8px;
}
th {
background-color: skyblue;
}
tr:nth-child(odd) {
background-color: #f2f2f2;
}
tr:hover {
background-color: aliceblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
// Set our data
var data = [{
ID: 1,
Family: 'resistor',
Capacitance: 7,
Voltage: 6,
Price: 25.6
},
{
ID: 2,
Family: 'capacitor',
Capacitance: 10,
Voltage: 10,
Price: 100.2
},
{
ID: 3,
Family: 'ferrite bead',
Capacitance: 1,
Voltage: 5,
Price: 35.6
},
{
ID: 4,
Family: 'resistor',
Capacitance: 1,
Voltage: 4,
Price: 35.6
},
{
ID: 5,
Family: 'capacitor',
Capacitance: 9,
Voltage: 4,
Price: 25.6
}
];
</script>
<table id="data-table"></table>
<p>Sort by:</p>
<button class="sort">ID</button>
<button class="sort">Family</button>
<button class="sort">Capacitance</button>
<button class="sort">Voltage</button>
<button class="sort">Price</button>
答案 2 :(得分:0)
我强烈建议您使用此选项以减少很多编码。您只需要将所有完整结果传递给表,表抛出JQuery就会过滤并排序所有结果。另外,此插件允许您根据需要调整列的大小,并发现许多真正有用的功能。
希望您会喜欢它。
答案 3 :(得分:0)
您可以将$ _POST ['family']保留在隐藏字段中(也许$ _POST ['hidden_family'])。当您进行下一个级别的搜索时,可以检查该级别并将其添加到您的搜索中,如果每次都不为空。
答案 4 :(得分:0)
这是您对表格进行数字排序的方式:
1)给目标表一个ID(在我的代码中是主表)
2)每次单击表标题(包括列号,第一个为0)时,都调用排序函数,每次添加更多列时,函数名内的数字都加1,在这种情况下为sortTable(0),sortTable(1 ).....
最终结果将是这样的(测试此示例,它可以工作):
<table id="main-table">
<tr>
<th style="cursor:pointer" onclick="sortTable(0)">colomn 0</th>
<th style="cursor:pointer" onclick="sortTable(1)">colomn 1</th>
<th style="cursor:pointer" onclick="sortTable(2)">colomn 2</th>
<th style="cursor:pointer" onclick="sortTable(3)">colomn 3</th>
<th style="cursor:pointer" onclick="sortTable(4)">colomn 4</th>
</tr>
<tr>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
</tr>
<tr>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
</tr>
<tr>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
</tr>
<tr>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
</tr>
<tr>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
<td><?php echo rand(0,999);?></td>
</tr>
</table>
<script>
function sortTable(column) {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("main-table");
switching = true;
while (switching) {
switching = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[column];
y = rows[i + 1].getElementsByTagName("TD")[column];
if (Number(x.innerHTML) > Number(y.innerHTML)) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
</script>
这是生成列号所需要做的:
$counter = 0;
foreach ($row as $field => $value){
print " <th onclick='sortTable($counter)'>$field</th>";
$counter = $counter+1;
}
答案 5 :(得分:0)
数据表https://datatables.net/确实很棒。正常功能是使用JavaScript,但是您可以将其配置为使用服务器资源并在服务器上处理日期,并仅显示结果。一旦掌握了它,就很容易。
每次对数据进行排序或过滤时,数据表都会向数组发送包含所有必要信息的信息,因此您只需扫描数组并相应地生成查询即可。
答案 6 :(得分:0)
// Initially populate the table
populateTable(data);
// Listen for a click on a sort button
$('.sort').on('click', function() {
// Get the key based on the value of the button
var key = $(this).html();
// Sort the data and update our data
data = sortBy(data, key);
// Fill the table with our data
populateTable(data);
});
// Modified from: https://www.sitepoint.com/sort-array-index/
function sortBy(inputData, key) {
// Sort our data based on the given key
inputData.sort(function(a, b) {
var aVal = a[key],
bVal = b[key];
if (aVal == bVal) return 0;
return aVal > bVal ? 1 : -1;
});
return inputData;
}
// Modified from: https://stackoverflow.com/questions/5361810/fast-way-to-dynamically-fill-table-with-data-from-json-in-javascript
function populateTable(inputData) {
var keys = new Array(),
i = -1;
// Create an array of keys
$.each(inputData[0], function(key, value) {
keys[++i] = key;
});
var r = new Array(),
j = -1;
// Populate the table headers
r[++j] = '<tr>';
$.each(keys, function(key, value) {
r[++j] = '<th>' + keys[key] + '</th>';
});
r[++j] = '</tr>';
for (var index = 0, size = inputData.length; index < size; index++) {
// Populate the table values
r[++j] = '<tr>';
$.each(keys, function(key, value) {
r[++j] = '<td>' + inputData[index][value] + '</td>';
});
r[++j] = '</tr>';
}
// Join everything together
$('#data-table').html(r.join(''));
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
text-align: left;
padding: 8px;
}
th {
background-color: skyblue;
}
tr:nth-child(odd) {
background-color: #f2f2f2;
}
tr:hover {
background-color: aliceblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
// Set our data
var data = [{
ID: 1,
Family: 'resistor',
Capacitance: 7,
Voltage: 6,
Price: 25.6
},
{
ID: 2,
Family: 'capacitor',
Capacitance: 10,
Voltage: 10,
Price: 100.2
},
{
ID: 3,
Family: 'ferrite bead',
Capacitance: 1,
Voltage: 5,
Price: 35.6
},
{
ID: 4,
Family: 'resistor',
Capacitance: 1,
Voltage: 4,
Price: 35.6
},
{
ID: 5,
Family: 'capacitor',
Capacitance: 9,
Voltage: 4,
Price: 25.6
}
];
</script>
<table id="data-table"></table>
<p>Sort by:</p>
<button class="sort">ID</button>
<button class="sort">Family</button>
<button class="sort">Capacitance</button>
<button class="sort">Voltage</button>
<button class="sort">Price</button>