如何在Codeigniter中通过ID将JSON数据传递到数据表中?

时间:2018-09-09 08:32:58

标签: php jquery codeigniter codeigniter-3 codeigniter-2

我通过传递generator_id作为参数从数据库中获取数据,我在 Model Controller 中执行了查询,但是如何在< strong> jquery 以基于generator_id将数据提取到数据表中。有人可以帮我吗?

示例:

传递id时,应在数据表中获取ID为1的生成器详细信息。

非常感谢您

我的模型:

generator id 1

我的控制器:-

public function getGeneratorRAReport($param,$generator_id){

    $arOrder = array('','RA_number');
    $this->db->where("RA_status",1);
    $this->db->where("generator_id",$generator_id);

    if ($param['start'] != 'false' and $param['length'] != 'false') {
        $this->db->limit($param['length'],$param['start']);
    }
    $this->db->select('*,DATE_FORMAT(RA_start_date,\'%d-%m-%Y\') as RA_start_date,DATE_FORMAT(RA_end_date,\'%d-%m-%Y\') as RA_end_date');
    $this->db->from('rental_agreement');
    $this->db->join('customer','customer_id = customer_id_fk');
    $this->db->join('generators','generator_id = generator_id_fk');
    $this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
    $this->db->order_by('RA_id', 'DESC');
    $query = $this->db->get();

    $data['data'] = $query->result();
    $data['recordsTotal'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
    $data['recordsFiltered'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
    return $data;

}
public function getGeneratorRAReportTotalCount($param,$generator_id){

    $this->db->where("RA_status",1);
    $this->db->where("generator_id",$generator_id);
    if ($param['start'] != 'false' and $param['length'] != 'false') {
        $this->db->limit($param['length'],$param['start']);
    }
    $this->db->select('*');
    $this->db->from('rental_agreement');
    $this->db->join('customer','customer_id = customer_id_fk');
    $this->db->join('generators','generator_id = generator_id_fk');
    $this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
    $this->db->order_by('RA_id', 'DESC');
    $query = $this->db->get();
    return $query->num_rows();

}

查看:-

public function index()
{
    $template['body'] = 'Generators/Generator_RAReport';
    $template['script'] = 'Generators/Generator_RAReport_script';
    $this->load->view('template', $template);
}

public function get($generator_id){
    $this->load->model('Generator_model');
    $param['draw'] = (isset($_REQUEST['draw']))?$_REQUEST['draw']:'';
    $param['length'] =(isset($_REQUEST['length']))?$_REQUEST['length']:'10'; 
    $param['start'] = (isset($_REQUEST['start']))?$_REQUEST['start']:'0';
    $param['order'] = (isset($_REQUEST['order'][0]['column']))?$_REQUEST['order'][0]['column']:'';
    $param['dir'] = (isset($_REQUEST['order'][0]['dir']))?$_REQUEST['order'][0]['dir']:'';
    $param['searchValue'] =(isset($_REQUEST['search']['value']))?$_REQUEST['search']['value']:'';



    $data = $this->Generator_model->getGeneratorRAReport($param,$generator_id);
    $json_data = json_encode($data);
    echo $json_data;
}

脚本:-

 <div class="box-body table-responsive">
          <table id="RA_details_table" class="table table-bordered table-striped">
            <thead>
            <tr>
              <th>Sl No.</th>
              <th>RA number</th>
              <th>RA type</th>
              <th>Customer</th>
              <th>RA start date</th>
              <th>RA end date</th>
              <th>Description</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
          </table>
        </div>
        <!-- /.box-body -->

2 个答案:

答案 0 :(得分:0)

在这种情况下,您可以在视图中同时使用模型和clientcode(jquery)。 代码点火器不会强迫您使用mvc

$ci =& get_instance();
$ci->load->model('some-model');
$ci->some-model->get();

答案 1 :(得分:0)

在您的html区域中放置此标签。

<input type="hidden" id="gen_id" value="<?=$gen_id?>" />

在您的js代码中...

   var id = document.getElementById('gen_id').value;   


 "ajax": {
            "url": "<?php echo base_url();?>index.php/Generator_RAReport/get/"+id,
            "type": "GET",
            "data" : function (d) {
             }

        },