javascript - 如何在私有(private)变量中进行查询 select_sum

标签 javascript ajax codeigniter datatable

我在 codeigniter 中有这样的私有(private)变量:

private $table = 'phone';
private $column_order = array(null, 'name', 'price');
private $type = array('type');
private $battery_consumption = array('battery_consumption');

我想在我的私有(private)模型中使用我的变量:

private get_data_query(){
   $this->db->select($this->column_order);
   $this->db->select_sum($this->battery_consumption);
   $this->db->from($this->table);
   $this->db->group_by($this->type);
}

然后我将该私有(private)函数调用到我的公共(public)函数中以用于获取表中的数据:

function get_datatables(){
   $this->_get_data_query();
   if($_POST['length'] != -1)
   $this->db->limit($_POST['length'], $_POST['start']);
   $query = $this->db->get();
   return $query->result();
}

function count_filtered()
{
    $this->_get_data_query();
    $query = $this->db->get();
    return $query->num_rows();
}

public function count_all()
{
    $this->db->from($this->table);
    return $this->db->count_all_results();
}

在 Controller 中:

public function get_data_phone()
{
    $list = $this->m_phone->get_datatables();
    $data = array();
    $no = $_POST['start'];

    foreach ($list as $field) {
        $row[] = $no;
        $row[] = $field->name;
        $row[] = $field->type;
        $row[] = $field->price;
        $row[] = $field->battery_consumption;
        $data[] = $row;
    }

    $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_request->count_all(),
        "recordsFiltered" => $this->m_request->count_filtered(),
        "data" => $data,
    );
    echo json_encode($output);
}

查看中:

<div class="card-block">
    <div class="dt-responsive table-responsive">
        <table id="phone" class="ui celled table" style="width:100%">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Phone Name</th>
                    <th>Phone Type</th>
                    <th>Phone Price</th>
                    <th>Battery Health</th>
                </tr>
            </thead>
        </table>
    </div>
</div>

数据表查询:

let table;

$(document).ready(function() {
   phone_data();

   function phone_data() {
      table = $('#phone').DataTable({
      "processing": true,
      "serverSide": true,
      "order": [],

      "ajax": {
         "url": "<?= site_url('phone/get_data_phone') ?>",
         "type": "POST",
      },

      "columnDefs": [{
         "targets": [0],
         "orderable": false,
      }, ],
    });  
   }
});

但是我在模型中的查询使用 select_sum() 仍然出错,我该怎么办?

最佳答案

对我来说,你似乎忘记了函数 get_data_query 中的 $this 。函数体中没有 $battery_conspiration 数组。而且你也没有在那里放置函数关键字,即使没有运行任何东西,我的 PHP IDE 也会标记错误。

关于javascript - 如何在私有(private)变量中进行查询 select_sum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60348685/

相关文章:

javascript - 需要在页面加载之前将值添加并读取到 cookie 中,因为页面项目需要根据值显示

javascript - 在弹出窗口中显示带有 Ajax 数据源的 jQuery DataTables

codeigniter - 将 CMS 子系统添加到 Codeigniter 站点

javascript - 为什么我的 ajax.beginform 操作调用的部分 View 没有出现在页面上?

ruby-on-rails - Rails Acts_as_votable Gem Like/Unlike 按钮与 Ajax

mySQL连接问题

php - 如何从db中获取记录?

javascript - 控制angularjs如何将日期转换为json字符串

javascript - VS Code 向我在 JavaScript 和 TypeScript 中的函数调用添加了奇怪的文本

javascript - 删除重复项并合并 JSON 对象