dongsuyou6938 2014-06-24 11:00
浏览 58
已采纳

如何在CodeIgniter中获取和计算post变量(数组)?

I have form in view (i'm using codeigniter), in the form there's an element (array) like this code below:

<table class="table table-stripped table-bordered" id="tbl-obat">
    <colgroup>
        <col class="con0">
        <col class="con1">
        <col class="con0">
        <col class="con1">
        <col class="con0">
    </colgroup>
    <thead>
        <tr>
            <th>No.</th>
            <th>Obat</th>
            <th>Dosis</th>
            <th>Satuan</th>
            <th>Jumlah</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                 <div id="1">1</div>
                 <input type="hidden" name="kd_obat_1" id="kd_obat_1" class="kd_obat" value="02">
            </td>
            <td>
                 <input type="text" name="nama_obat_1" id="nama_obat_1" class="nama_obat input-large ui-autocomplete-input valid" autocomplete="off"><span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
            </td>
            <td>
                <input type="text" name="dosis_1" id="dosis_1" class="dosis input-small valid">
            </td>
            <td>
                <select name="satuan_1" id="satuan_1" class="satuan uniformselect valid" style="width:100px;">
                     <option name="-">Pilih Satuan</option>
                     <option value="KSKO-01">AMPUL</option>
                     <option value="KSKO-02">BUAH</option>
                     <option value="KSKO-03">BOTOL</option>
                     <option value="KSKO-04">BUNGKUS</option>
                     <option value="KSKO-05">KAPLET</option>
                </select>
            </td>
            <td>
                <input type="text" name="jumlah_1" id="jumlah_1" class="jumlah input-large valid">
            </td>
        </tr>
        <tr>
            <td>
                <div id="2">2</div>
                <input type="hidden" name="kd_obat_2" id="kd_obat_2" class="kd_obat" value="01">
            </td>
            <td>
                <input type="text" name="nama_obat_2" id="nama_obat_2" class="nama_obat input-large ui-autocomplete-input valid" autocomplete="off"><span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
            </td>
            <td>
                <input type="text" name="dosis_2" id="dosis_2" class="dosis input-small valid">
            </td>
            <td>
                <select name="satuan_2" id="satuan_2" class="satuan uniformselect valid" style="width:100px;">
                <option name="-">Pilih Satuan</option>
                <option value="KSKO-01">AMPUL</option>
                <option value="KSKO-02">BUAH</option>
                <option value="KSKO-03">BOTOL</option>
                <option value="KSKO-04">BUNGKUS</option>
                <option value="KSKO-05">KAPLET</option>
            </select>
        </td>
        <td>
            <input type="text" name="jumlah_2" id="jumlah_2" class="jumlah input-large valid">
        </td>
    </tr>
</tbody>
</table>

How can i count an array and get post values in controller codeigniter? i try this code doesnt work :

foreach ($_POST as $key=>$val) {
    $$key = $val;
}

$obat = array();

$i = 1;
while(${"nama_obat_".$i}) {
    if(isset(${"nama_obat_".$i}) or !empty(${"nama_obat_".$i}) or ${"nama_obat_".$i} !== "")
    {
         $obat[$i]['kd_trans_pelayanan'] = $trans_id;
         $obat[$i]['nama_obat'] = ${"nama_obat_".$i};
         $obat[$i]['dosis'] = ${"dosis_".$i};
         $obat[$i]['kd_sat_kecil_obat'] = ${"satuan_".$i};
         $obat[$i]['qty'] = ${"jumlah_".$i};

        $i++;
    } else {
        exit(1);
    }
}

echo '<pre>';
print_r($obat);

Thanks in advance!

EDIT :

i need to count field with name 'kd_obat_x' where x = 1 to n, based on user input. i need algorithm to get how many variables posted based on user input and so on get the data. here's my complete code:

view.php

<h4>Step 3: Obat</h4>
<p>
    <table class="table table-stripped table-bordered" id="tbl-obat">
        <colgroup>
            <col class="con0" />
            <col class="con1" />
            <col class="con0" />
            <col class="con1" />
            <col class="con0" />
        </colgroup>
        <thead>
            <tr>
                <th>No.</th>
                <th>Obat</th>
                <th>Dosis</th>
                <th>Satuan</th>
                <th>Jumlah</th>
        </thead>
        <tbody></tbody>
    </table>
</p>
<p>
    <a href="#" id="addObat" class="btn btn-primary btn-rounded"><i class="icon-plus icon-white"></i>&nbsp;Tambah</a>
    <a href="#" id="removeObat" class="btn btn-danger btn-rounded"><i class="icon-trash icon-white"></i>&nbsp;Hapus</a>
</p>

my.js

<script type="text/javascript">
    // Autocomplete Obat
    jQuery(function() {
    var counter3 = 1;
    var options = {
        source: '<?php echo base_url(); ?>admin/autoCompleteObat',
        minLength: 2,
        focus: function( event, ui ) {
                    jQuery('#nama_obat_'+counter3).val(ui.item.value);
                    jQuery(this).closest('tr').find('input.kd_obat').val(ui.item.kd_obat);
        },
        select: function( event, ui ) {
                    //event.preventDefault();
                    jQuery('#nama_obat_'+counter3).val(ui.item.value);
                    jQuery(this).closest('tr').find('input.kd_obat').val(ui.item.kd_obat);                      
        },
        messages: {
                noResults:  ''
        }
    };

    jQuery('input.nama_obat').live("keydown.autocomplete", function() {
        jQuery(this).autocomplete(options);
    });

    var addInput = function() {
        if (counter3 > 1){
            jQuery('a#removeObat').removeAttr('disabled');
        }

        var inputHTML = '<tr><td><div id="'+counter3+'">'+counter3+'</div><input type="hidden" name="kd_obat_'+counter3+'" id="kd_obat_'+counter3+'" class="kd_obat" /></td><td><input type="text" name="nama_obat_'+counter3+'" id="nama_obat_'+counter3+'" class="nama_obat input-large" /></td><td><input type="text" name="dosis_'+counter3+'" id="dosis_'+counter3+'" class="dosis input-small" /></td><td><select name="satuan_'+counter3+'" id="satuan_'+counter3+'" class="satuan uniformselect" style="width:100px;"><option name="-">Pilih Satuan</option><?php foreach($list_satuan_kecil as $lsk) : ?><option value="<?php echo $lsk['kd_sat_kecil_obat']; ?>"><?php echo $lsk['sat_kecil_obat']; ?></option><?php endforeach; ?></select></td><td><input type="text" name="jumlah_'+counter3+'" id="jumlah_'+counter3+'" class="jumlah input-large" /></td></tr>';

        jQuery(inputHTML).appendTo("table#tbl-obat tbody");
        jQuery("input.nama_obat:last").focus();
        jQuery('html, body').animate({
            scrollTop: jQuery("input.nama_obat:last").offset().top
        }, 2000);
        counter3++;
    };

    var removeInput = function() {
        counter3--;
        if(counter3 == 1){
            jQuery('a#removeObat').attr('disabled','disabled');
            //alert("Minimal sisa 1!");
            counter3++;
            //console.log('Jika Counter == 1 :' + counter3);
        }else{
            jQuery("table#tbl-obat tbody tr:last").remove();
            //console.log('Jika Counter != 1 :' + counter3);
        }
        jQuery("input.nama_obat:last").focus();
        jQuery('html, body').animate({
            scrollTop: jQuery("input.nama_obat:last").offset().top
        }, 2000);   
    };

    if (!jQuery("table#tbl-obat tbody").find("input.nama_obat").length) {
        addInput();
    }

    jQuery("a#addObat").click(addInput);
    jQuery("a#removeObat").click(removeInput);
}); 
</script>

admin.php (controller)

function autoCompleteObat()
{
    if (isset($_REQUEST['term'])){
      $q = strtolower($_GET['term']);
      $output = $this->m_crud->get_obat($q);
      $this->output->set_content_type('application/json')->set_output(json_encode($output));
    }
}

/***MASTER PELAYANAN***/
function pelayanan($par1 = '', $par2 = '', $par3 = '')
{
    if (!$this->session->userdata('logged_in') == true)
    {
        redirect('login');
    }

    if ($par1 == 'tambah') {
        $trans_id                           = $this->m_crud->generate_transaksi();
        $pelayanan['kd_trans_pelayanan']    = $trans_id;
        $pelayanan['kd_rekam_medis']        = $this->input->post('kd_rekam_medis');
        $pelayanan['tgl_pelayanan']         = $this->functions->convert_date_sql($this->input->post('tgl_pelayanan'));
        $pelayanan['kd_jenis_layanan']      = $this->input->post('kd_jenis_layanan');
        $pelayanan['kd_unit_pelayanan']     = $this->input->post('kd_unit_pelayanan');
        $pelayanan['kd_puskesmas']          = 'P3271010101'; //$this->session->userdata('kd_puskesmas'); // harusnya diambil dari session
        $pelayanan['kd_dokter']             = $this->input->post('kd_dokter');
        $pelayanan['kd_petugas']            = $this->input->post('kd_petugas');
        $pelayanan['kd_status_pasien']      = $this->input->post('kd_status_pasien'); // blm
        $pelayanan['anamnesa']              = $this->input->post('anamnesa');
        $pelayanan['cat_fisik']             = $this->input->post('cat_fisik');
        $pelayanan['cat_dokter']            = $this->input->post('cat_dokter');
        $pelayanan['kd_bed']                = $this->input->post('kd_bed');
        $pelayanan['no_rujukan']            = $this->input->post('no_rujukan');
        $pelayanan['tempat_rujukan']        = $this->input->post('tempat_rujukan');

        // Pelayanan Obat
        $obat = array();

        $i = 1;
        while(${"nama_obat_".$i}) {
            if(isset(${"nama_obat_".$i}) or !empty(${"nama_obat_".$i}) or ${"nama_obat_".$i} !== ""){
                $obat[$i]['kd_trans_pelayanan'] = $trans_id;
                $obat[$i]['nama_obat'] = ${"nama_obat_".$i};
                $obat[$i]['dosis'] = ${"dosis_".$i};
                $obat[$i]['kd_sat_kecil_obat'] = ${"satuan_".$i};
                $obat[$i]['qty'] = ${"jumlah_".$i};

                   /* insert db here */                                        

                $i++;
            } else {
                exit(1);
            }

        }


        echo '<pre>';
        print_r($obat);
     }
}

and finally my model m_crud.php

function get_obat($q){
    $this->db->select('kd_obat,nama_obat');
    $this->db->like('nama_obat', $q);
    $this->db->or_like('kd_obat', $q);

    $query = $this->db->get('obat');
    $results = array();
    if($query->num_rows > 0){
        $i = 1;
        foreach ($query->result_array() as $row){
            $results[$i]['label'] = $row['kd_obat'].' - '.$row['nama_obat']; //build an array
            $results[$i]['value'] = htmlentities(stripslashes($row['nama_obat']));
            $results[$i]['kd_obat'] = htmlentities(stripslashes($row['kd_obat']));

            $i++;
        }
    }
    return $results;
}    
  • 写回答

4条回答 默认 最新

  • donglian5309 2014-06-25 10:00
    关注

    You can count all element with 'kd_obat_' using this code:

    $count=0;
    foreach($arr as $key=>$value){
    $pos = strpos($key,'kd_obat_');
    if($pos!==false){
        $count++;
    }else{
         continue;  
    }
    }
    echo $count;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序