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,41 +6,48 @@
<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']))
echo htmlentities($_GET['search'], ENT_QUOTES, 'UTF-8') ?>"> echo htmlentities($_GET['search'], ENT_QUOTES, 'UTF-8') ?>">
<i class="search icon"></i> <i class="search icon"></i>
</div>
<div class="results" style="width: 100%; "></div>
</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> </div>
<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> -->
<?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;">
<div class="ui icon input" style="width: 100%; "> <div class="ui icon input" style="width: 100%; ">
<input class="prompt" style="width: 100%; " type="text" placeholder="用户ID" name="query_user_id" value="<?php if (isset($_GET['query_user_id'])) <input class="prompt" style="width: 100%; " type="text" placeholder="用户ID" name="query_user_id" value="<?php if (isset($_GET['query_user_id']))
echo htmlentities($_GET['query_user_id'], ENT_QUOTES, 'UTF-8') ?>"> echo htmlentities($_GET['query_user_id'], ENT_QUOTES, 'UTF-8') ?>">
<i class="search icon"></i> <i class="search icon"></i>
</div>
<div class="results" style="width: 100%; "></div>
</div> </div>
<div class="results" style="width: 100%; "></div>
</div>
<?php endif; ?> <?php endif; ?>
<button type="submit" class="ui mini button" style="display: none;">搜索</button> <button type="submit" class="ui mini button" style="display: none;">搜索</button>
</form> </form>
</div> </div>
@@ -97,13 +104,13 @@
if (checked) { if (checked) {
document.getElementById('show_solved_style').innerHTML = ''; document.getElementById('show_solved_style').innerHTML = '';
// 显示所有题目 // 显示所有题目
document.querySelectorAll('.solved-problem').forEach(function(row) { document.querySelectorAll('.solved-problem').forEach(function (row) {
row.style.display = ''; row.style.display = '';
}); });
} else { } else {
document.getElementById('show_solved_style').innerHTML = '.solved-problem { display: none; }'; 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'; row.style.display = 'none';
}); });
} }
@@ -123,40 +130,15 @@
</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>
<?php if (!isset($_GET['list'])) { ?> <?php if (!isset($_GET['list'])) { ?>
<div style="margin-bottom: 30px; "> <div style="margin-bottom: 30px; ">
@@ -319,4 +301,4 @@
<script type="text/javascript" src="include/jquery.tablesorter.js"></script> <script type="text/javascript" src="include/jquery.tablesorter.js"></script>
</div> </div>
<?php include("template/$OJ_TEMPLATE/footer.php"); ?> <?php include("template/$OJ_TEMPLATE/footer.php"); ?>