我在CI中传递变量存在一些问题。
模型
private function _get_datatables_query()
{
$id = $this->det_survey_id($id);
//$this->db->from($this->table);
$this->db->SELECT('id_pertanyaan,isi_pertanyaan,judul_pilgan,id_survey')
->FROM('pertanyaan')
->JOIN('pilgan','pertanyaan.id_pilgan=pilgan.id_pilgan','inner')
->WHERE('id_survey',$id);
控制器
public function edit($id){
$this->load->model('Survey_Model');
$data=array(
'select_option' => $this->Survey_Model->get_pilgan(),
'id_survey' => $this->Survey_Model->det_survey_id($id),
'judul_survey' => $this->Survey_Model->det_survey_judul(),
);
$this->load->view('include/header.php');
$this->load->view('tambah_pertanyaan',$data);
$this->load->view('include/footer.php');
}
如何将变量从控制器$id
传递到模型$id = $this->det_survey_id($id);
答案 0 :(得分:0)
我的模特
private function _get_datatables_query($id)
{
//$id = 87;//$this->Survey_Model->det_survey_id(87);
//$this->db->from($this->table);
$this->db->SELECT('id_pertanyaan,isi_pertanyaan,judul_pilgan,id_survey')
->FROM('pertanyaan')
->JOIN('pilgan','pertanyaan.id_pilgan=pilgan.id_pilgan','inner')
->WHERE('id_survey',$id);
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables($id)
{
$this->_get_datatables_query($id);
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered($id)
{
$this->_get_datatables_query($id);
$query = $this->db->get();
return $query->num_rows();
}
我的控制器
public function ajax_list($id)
{
$list = $this->pertanyaan->get_datatables($id);
$data = array();
$no = $_POST['start'];
foreach ($list as $survey) {
$no++;
$row = array();
$row[] = $survey->isi_pertanyaan;
$row[] = $survey->judul_pilgan;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$survey->id_pertanyaan."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$survey->id_pertanyaan."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->pertanyaan->count_all(),
"recordsFiltered" => $this->pertanyaan->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
我的观点
$(document).ready(function() {
//datatables
table_pertanyaan = $('#table_pertanyaan').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('pertanyaan/ajax_list/')?>/" + id,
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ -1 ], //last column
"orderable": false, //set not orderable
},
],
Parm控制器public function ajax_list($id)
错误