feat(problemset): add hardness filter and improve query parameter handling

- Added hardness filter dropdown in problemset UI with options 1-8
- Implemented server-side hardness parameter validation and filtering
- Refactored query parameter handling to use $filter_params array
- Updated SQL queries to use parameter binding for both search and hardness filters
- Fixed pagination to include hardness parameter in URL
- Improved code organization by centralizing filter parameter management
This commit is contained in:
2026-03-13 15:03:46 +08:00
parent 3f6db1935a
commit 60f1532eb8
2 changed files with 54 additions and 58 deletions

View File

@@ -41,9 +41,18 @@ $postfix = "";
$filter_sql = ""; $filter_sql = "";
$limit_sql = ""; $limit_sql = "";
$order_by = " order by problem_id "; $order_by = " order by problem_id ";
$filter_params = array();
$hardness = 0;
if (isset($_GET['hardness'])) {
$hardness = intval($_GET['hardness']);
if ($hardness < 1 || $hardness > 8)
$hardness = 0;
}
if (isset($_GET['search']) && trim($_GET['search']) != "") { if (isset($_GET['search']) && trim($_GET['search']) != "") {
$search = "%" . ($_GET['search']) . "%"; $search = "%" . ($_GET['search']) . "%";
$filter_sql = " ( title like ? or source like ?)"; $filter_sql = " ( title like ? or source like ?)";
$filter_params[] = $search;
$filter_params[] = $search;
$limit_sql = " LIMIT " . ($page - 1) * $page_cnt . "," . $page_cnt; $limit_sql = " LIMIT " . ($page - 1) * $page_cnt . "," . $page_cnt;
$postfix = "&search=" . urlencode($_GET['search']); $postfix = "&search=" . urlencode($_GET['search']);
} else if (isset($_GET['list']) && trim($_GET['list'] != "")) { } else if (isset($_GET['list']) && trim($_GET['list'] != "")) {
@@ -61,6 +70,11 @@ if (isset($_GET['search']) && trim($_GET['search']) != "") {
$filter_sql = " 1"; $filter_sql = " 1";
$limit_sql = " LIMIT " . ($page - 1) * $page_cnt . "," . $page_cnt; $limit_sql = " LIMIT " . ($page - 1) * $page_cnt . "," . $page_cnt;
} }
if ($hardness > 0) {
$filter_sql .= " and hardness=?";
$filter_params[] = $hardness;
$postfix .= "&hardness=" . $hardness;
}
//all submit //all submit
//all acc //all acc
@@ -103,10 +117,10 @@ pdo_query("SET sort_buffer_size = 1024*1024"); // Out of sort memory, consider
$sql = "SELECT `problem_id`,`title`,`source`,`submit`,`accepted`,defunct FROM problem A WHERE $filter_sql $order_by $limit_sql "; $sql = "SELECT `problem_id`,`title`,`source`,`submit`,`accepted`,defunct FROM problem A WHERE $filter_sql $order_by $limit_sql ";
$count_sql = "SELECT count(1) from problem where $filter_sql "; $count_sql = "SELECT count(1) from problem where $filter_sql ";
//echo htmlentities( $sql); //echo htmlentities( $sql);
if (isset($_GET['search']) && trim($_GET['search']) != "") { if (count($filter_params) > 0) {
$total = pdo_query($count_sql, $search, $search); $total = pdo_query($count_sql, ...$filter_params);
$cnt = $total[0][0] / $page_cnt; $cnt = $total[0][0] / $page_cnt;
$result = pdo_query($sql, $search, $search); $result = pdo_query($sql, ...$filter_params);
} else { } else {
$total = mysql_query_cache($count_sql); $total = mysql_query_cache($count_sql);
$cnt = $total[0][0] / $page_cnt; $cnt = $total[0][0] / $page_cnt;

View File

@@ -6,7 +6,8 @@
<div class="row" style="white-space: nowrap; "> <div class="row" style="white-space: nowrap; ">
<div class="seven wide column"> <div class="seven wide column">
<form action="" method="get"> <form action="" method="get">
<div class="ui search" style="width: 160px; height: 28px; margin-top: -5.3px; float:left; margin-right: 10px;"> <div class="ui search"
style="width: 160px; height: 28px; margin-top: -5.3px; float:left; margin-right: 10px;">
<div class="ui left icon input" style="width: 100%; "> <div class="ui left icon input" style="width: 100%; ">
<input class="prompt" style="width: 100%; " type="text" placeholder="<?php echo $MSG_TITLE; ?> …" <input class="prompt" style="width: 100%; " type="text" placeholder="<?php echo $MSG_TITLE; ?> …"
name="search" value="<?php if (isset($_GET['search'])) name="search" value="<?php if (isset($_GET['search']))
@@ -16,19 +17,25 @@
<div class="results" style="width: 100%; "></div> <div class="results" style="width: 100%; "></div>
</div> </div>
<!-- 添加难度按钮 --> <div class="ui selection dropdown" style="width: 120px; height: 28px; margin-top: -5.3px; float:left; margin-right: 10px;">
<!-- <div style="display: inline-block; margin-left: 10px; vertical-align: top;"> <input type="hidden" name="hardness" value="<?php
<?php for ($i = 1; $i <= 8; $i++): ?> $selected_hardness = 0;
<button if (isset($_GET['hardness'])) {
type="button" $selected_hardness = intval($_GET['hardness']);
class="ui mini basic button difficulty-btn" if ($selected_hardness < 1 || $selected_hardness > 8)
data-difficulty="<?php echo $i; ?>" $selected_hardness = 0;
style="margin-right: 5px; margin-bottom: 5px;" }
> echo $selected_hardness;
难度<?php echo $i; ?> ?>">
</button> <i class="dropdown icon"></i>
<?php endfor; ?> <div class="default text">难度</div>
</div> --> <div class="menu">
<div class="item" data-value="0">全部难度</div>
<?php for ($h = 1; $h <= 8; $h++) {
echo '<div class="item" data-value="' . $h . '">难度 ' . $h . '</div>';
} ?>
</div>
</div>
<?php if (isset($_SESSION[$OJ_NAME . '_' . 'administrator'])): ?> <?php if (isset($_SESSION[$OJ_NAME . '_' . 'administrator'])): ?>
<div class="ui search" style="width: 120px; height: 28px; margin-top: -5.3px; display: inline-block;"> <div class="ui search" style="width: 120px; height: 28px; margin-top: -5.3px; display: inline-block;">
@@ -123,36 +130,11 @@
</div> </div>
<script> <script>
// 添加难度按钮点击事件 $(function () {
document.querySelectorAll('.difficulty-btn').forEach(button => { $('.ui.dropdown').dropdown({
button.addEventListener('click', function() { onChange: function () {
const difficulty = this.getAttribute('data-difficulty'); $(this).closest('form').submit();
// 创建隐藏表单提交
const form = document.createElement('form');
form.method = 'get';
form.action = '';
// 添加难度参数
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'difficulty';
input.value = difficulty;
form.appendChild(input);
// 保留现有搜索参数
const existingSearch = document.querySelector('input[name="search"]');
if (existingSearch && existingSearch.value) {
const searchInput = document.createElement('input');
searchInput.type = 'hidden';
searchInput.name = 'search';
searchInput.value = existingSearch.value;
form.appendChild(searchInput);
} }
// 提交表单
document.body.appendChild(form);
form.submit();
}); });
}); });
</script> </script>