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 = "";
$limit_sql = "";
$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']) != "") {
$search = "%" . ($_GET['search']) . "%";
$filter_sql = " ( title like ? or source like ?)";
$filter_params[] = $search;
$filter_params[] = $search;
$limit_sql = " LIMIT " . ($page - 1) * $page_cnt . "," . $page_cnt;
$postfix = "&search=" . urlencode($_GET['search']);
} else if (isset($_GET['list']) && trim($_GET['list'] != "")) {
@@ -61,6 +70,11 @@ if (isset($_GET['search']) && trim($_GET['search']) != "") {
$filter_sql = " 1";
$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 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 ";
$count_sql = "SELECT count(1) from problem where $filter_sql ";
//echo htmlentities( $sql);
if (isset($_GET['search']) && trim($_GET['search']) != "") {
$total = pdo_query($count_sql, $search, $search);
if (count($filter_params) > 0) {
$total = pdo_query($count_sql, ...$filter_params);
$cnt = $total[0][0] / $page_cnt;
$result = pdo_query($sql, $search, $search);
$result = pdo_query($sql, ...$filter_params);
} else {
$total = mysql_query_cache($count_sql);
$cnt = $total[0][0] / $page_cnt;

View File

@@ -6,7 +6,8 @@
<div class="row" style="white-space: nowrap; ">
<div class="seven wide column">
<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%; ">
<input class="prompt" style="width: 100%; " type="text" placeholder="<?php echo $MSG_TITLE; ?> …"
name="search" value="<?php if (isset($_GET['search']))
@@ -16,19 +17,25 @@
<div class="results" style="width: 100%; "></div>
</div>
<!-- 添加难度按钮 -->
<!-- <div style="display: inline-block; margin-left: 10px; vertical-align: top;">
<?php for ($i = 1; $i <= 8; $i++): ?>
<button
type="button"
class="ui mini basic button difficulty-btn"
data-difficulty="<?php echo $i; ?>"
style="margin-right: 5px; margin-bottom: 5px;"
>
难度<?php echo $i; ?>
</button>
<?php endfor; ?>
</div> -->
<div class="ui selection dropdown" style="width: 120px; height: 28px; margin-top: -5.3px; float:left; margin-right: 10px;">
<input type="hidden" name="hardness" value="<?php
$selected_hardness = 0;
if (isset($_GET['hardness'])) {
$selected_hardness = intval($_GET['hardness']);
if ($selected_hardness < 1 || $selected_hardness > 8)
$selected_hardness = 0;
}
echo $selected_hardness;
?>">
<i class="dropdown icon"></i>
<div class="default text">难度</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'])): ?>
<div class="ui search" style="width: 120px; height: 28px; margin-top: -5.3px; display: inline-block;">
@@ -97,13 +104,13 @@
if (checked) {
document.getElementById('show_solved_style').innerHTML = '';
// 显示所有题目
document.querySelectorAll('.solved-problem').forEach(function(row) {
document.querySelectorAll('.solved-problem').forEach(function (row) {
row.style.display = '';
});
} else {
document.getElementById('show_solved_style').innerHTML = '.solved-problem { display: none; }';
// 隐藏已解决的题目
document.querySelectorAll('.solved-problem').forEach(function(row) {
document.querySelectorAll('.solved-problem').forEach(function (row) {
row.style.display = 'none';
});
}
@@ -123,36 +130,11 @@
</div>
<script>
// 添加难度按钮点击事件
document.querySelectorAll('.difficulty-btn').forEach(button => {
button.addEventListener('click', function() {
const difficulty = this.getAttribute('data-difficulty');
// 创建隐藏表单提交
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);
$(function () {
$('.ui.dropdown').dropdown({
onChange: function () {
$(this).closest('form').submit();
}
// 提交表单
document.body.appendChild(form);
form.submit();
});
});
</script>