You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中实现可选择SUM/AVG/MAX聚合函数的UI方案咨询

没问题!咱们一步步来实现这个可以选择聚合函数的查询界面,从视图、模型到控制器逐个修改,确保功能安全又好用~

实现可选择聚合函数的银行账户查询界面

1. 视图层:创建用户选择界面(bank_query_view.php)

先做一个简单的表单,让用户能选择要执行的聚合函数,同时预留结果展示区域:

<form method="post" action="<?= base_url('bank_query_controller') ?>">
    <label for="aggregate_function">选择聚合类型:</label>
    <select name="aggregate_function" id="aggregate_function">
        <option value="SUM">账户余额总和(SUM)</option>
        <option value="AVG">账户平均余额(AVG)</option>
        <option value="MAX">最高账户余额(MAX)</option>
    </select>
    <button type="submit">执行查询</button>
</form>

<!-- 结果展示区域 -->
<?php if(isset($result)): ?>
    <div style="margin-top:20px;padding:10px;border:1px solid #eee;">
        <h3>查询结果</h3>
        <p>您选择的聚合函数: <strong><?= $selected_function ?></strong></p>
        <p>bank_balance的计算结果: <strong><?= number_format($result, 2) ?></strong></p>
    </div>
<?php endif; ?>

<?php if(isset($error)): ?>
    <p style="color:red;margin-top:10px;"><?= $error ?></p>
<?php endif; ?>

2. 模型层:动态执行安全的聚合查询(bank_query_model.php)

这里要重点做安全验证,用白名单限制允许的聚合函数,防止SQL注入风险:

class Bank_query_model extends CI_Model {
    public function get_aggregate_result($function) {
        // 白名单:只允许指定的三个聚合函数
        $allowed_functions = ['SUM', 'AVG', 'MAX'];
        $valid_function = strtoupper($function);
        
        if(!in_array($valid_function, $allowed_functions)) {
            return false;
        }

        // 执行查询并返回结果
        $query = $this->db->query("SELECT {$valid_function}(bank_balance) AS calculation_result FROM bank_account");
        $row = $query->row();
        
        // 处理空表的情况
        return $row ? $row->calculation_result : 0;
    }
}

3. 控制器层:处理表单请求与数据传递(bank_query_controller.php)

控制器负责接收用户的选择、调用模型、把结果传给视图:

class Bank_query_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('bank_query_model');
        $this->load->helper('url'); // 用于生成base_url
    }

    public function index() {
        $data = [];

        // 处理表单提交请求
        if($this->input->post()) {
            $selected_function = $this->input->post('aggregate_function');
            $calculation_result = $this->bank_query_model->get_aggregate_result($selected_function);
            
            if($calculation_result !== false) {
                $data['selected_function'] = $selected_function;
                $data['result'] = $calculation_result;
            } else {
                $data['error'] = "无效的聚合函数选择,请重新选择";
            }
        }

        // 加载视图并传递数据
        $this->load->view('bank_query_view', $data);
    }
}

额外注意事项

  • 确保你的CodeIgniter配置文件中base_url已经正确设置,否则表单提交路径会出错;
  • 如果需要支持更多聚合函数,只需要在模型的$allowed_functions数组里添加即可,不用改其他逻辑;
  • 可以根据需求给视图添加样式,让界面更美观。

内容的提问来源于stack exchange,提问作者Bernando Purba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:06:00