如何使用基于数组的内爆分离多个值或拆分多个值?

jk9hmnmh  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(295)

我使用dynamics表单输入数据,其中一列包含使用内爆(,)的多个数据。但是,当我输入时,动态表单会设法执行我想要的任意多个输入,但对于“mekanik”表,它变成了一个。
pict one:输入数据的模式
对于“keluhan”,每个表都是不同的,但“mekanik”变成了一个值。怎么把它分开?
在mechanical列中,第一行应为操作员,第二行应为abdullah muis。
图二:table
这是我的密码:
这是我输入的“keluhan”和“mekanik”模态代码,我使用dynamic\u fuild作为动态形式。
在此处输入图像描述

<?php 
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 2, 7);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$huruf = "SC";
$kodepg1 = $huruf . sprintf("%07s", $urutan);
?>
<?php
include "support/fungsi_romawi.php";
$bulan = date('n');
$romawi = getRomawi($bulan);
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 6, 6);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$tahun = date ('Y');
$nomor = "FORM/SC/".$romawi."/".$tahun."/";
$kode = sprintf("%07s", $urutan);
$noformpg = $nomor. $kode;
?>
<div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <center><h4 class="modal-title" id="myModalLabel">Tambah Data</h4></center>
            </div>
            <div class="modal-body">
            <div class="container-fluid">
            <form method="POST" action="system/add_pg.php" id="form_materi">
            <input class="form-control" type="hidden" name="kodepg" value="<?php echo $kodepg1 ?>" required>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">NO. PENGAJUAN</label>
                    </div>
                    <div class="col-sm-9">
                        <input class="form-control" type="text" name="noformpg" value="<?php echo $noformpg ?>" required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">TANGGAL</label>
                    </div>
                    <div class="col-sm-9">
                        <input class="form-control" type="date" name="waktupg" placeholder="Masukkan Hari dan Tanggal Pengajuan" autocomplete="off" required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">DEPARTEMEN</label>
                    </div>
                    <div class="col-sm-9">
                        <select class="form-control selectpicker" type="text" name="deptpg" id="deptpg" data-live-search="true" required>
                        <option selected value> -- Pilih Departemen -- </option>
                        <?php
                        $query = mysqli_query($connect, "SELECT nama_dept FROM tb_dept");
                        while ($data = mysqli_fetch_array($query)) {
                        ?>
                        <option value="<?=$data['nama_dept'];?>"><?php echo $data['nama_dept'];?></option>
                        <?php
                        }
                        ?>
                        </select>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">LOKASI</label>
                    </div>
                    <div class="col-sm-9">
                        <select class="form-control selectpicker" type="text" name="lokasipg" id="lokasipg" data-live-search="true" required>
                            <option selected value> -- Pilih Lokasi -- </option>
                            <option value="JLA 1">JLA 1</option>
                            <option value="JLA 2">JLA 2</option>
                        </select>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">NAMA MESIN</label>
                    </div>
                    <div class="col-sm-9">
                        <select class="form-control selectpicker" type="text" name="merkpg" id="merkmes" onchange="auto_complete()" data-live-search="true" required>
                        <option selected value> -- Pilih Mesin -- </option>
                        <optgroup label="Mesin Forklift">
                            <?php
                            $query = mysqli_query($connect, "SELECT * FROM tb_mesin_forklift");
                            while ($data = mysqli_fetch_array($query)) {
                            ?>
                            <label><label>
                            <option value="<?=$data['merk_mesin'];?>"><?php echo $data['merk_mesin'];?>   (<?php echo $data['tahun_mesin'];?>)</option>
                            <?php
                            }
                            ?>
                        </optgroup>
                        <optgroup label="Mesin Produksi">
                            <?php
                            $query = mysqli_query($connect, "SELECT * FROM tb_mesin_produksi");
                            while ($data = mysqli_fetch_array($query)) {
                            ?>
                            <label><label>
                            <option value="<?=$data['nama_mesin'];?>"><?php echo $data['nama_mesin'];?> (<?php echo $data['tahun_mesin'];?>)</option>
                            <?php
                            }
                            ?>
                        </optgroup>
                        </select>

                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">TAHUN</label>
                    </div>
                    <div class="col-sm-9">
                        <input class="form-control" type="text" id="tahunmes" name="tahunpg" placeholder="Masukkan Tahun Mesin" required autocomplete="off">
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">OPERATOR</label>
                    </div>
                    <div class="col-sm-9">
                    <select class="form-control selectpicker" type="text" name="oppg[]" id="oppg" multiple data-live-search="true" data-none-selected-text="Pilih Operator">
                        <?php
                        $query = mysqli_query($connect, "SELECT nama_op FROM tb_operator");
                        while ($data = mysqli_fetch_array($query)) {
                        ?>
                        <option value="<?=$data['nama_op'];?>"><?php echo $data['nama_op'];?></option>
                        <?php
                        }
                        ?>
                    </select>
                    </div>
                </div>
                <div id="dynamic_field">
                    <div class="row form-group">
                        <div class="col-sm-3">
                            <label class="control-label modal-label">KELUHAN</label>
                        </div>
                        <div class="col-sm-7">
                            <textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3">
                            <label class="control-label modal-label">MEKANIK</label>
                        </div>
                        <div class="col-sm-7">
                            <select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik">
                                <?php
                                $query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik");
                                while ($data = mysqli_fetch_array($query)) {
                                ?>
                                <option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-sm-2">
                            <button class="btn btn-success" type="button" name="add" id="add">
                            <i class="glyphicon glyphicon-plus"></i>
                            </button>
                        </div>
                    </div>  
                </div>
                <div class="row form-group">
                    <div class="col-sm-3">
                        <label class="control-label modal-label">SHIFT</label>
                    </div>
                    <div class="col-sm-9">
                        <select class="form-control selectpicker" type="text" name="shiftpg" id="shiftpg" data-live-search="true">
                            <option selected value> -- Pilih Shift -- </option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                        </select>
                    </div>
                </div>
                <input type="hidden" class="form-control" name="statuspg" value="PROGRESS">
            </div> 
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
                <button type="submit" name="add" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</a>
            </div>
            </form>
            </div>
        </div>
    </div>
</div>

我使用javascript来添加输入字段,因为它们是动态的。

$(document).ready(function(){  
        var i=1;  
        $('#add').click(function(){  
            i++;  
            $('#dynamic_field').append('<div id="row'+i+'"><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">KELUHAN</label></div><div class="col-sm-7"><textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3" required></textarea></div></div><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">MEKANIK</label></div><div class="col-sm-7"><select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik" required> <?php $query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik"); while ($data = mysqli_fetch_array($query)) { ?> <option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option> <?php } ?></select></div><div class="col-sm-2"><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><i class="fa fa fa-trash"></i></button></div></div></div>');  
            $(".selectpicker").selectpicker('refresh');
            });  
        $(document).on('click', '.btn_remove', function(){  
            var button_id = $(this).attr("id");   
             //  alert(button_id);
            $('#row'+button_id+'').remove();  
        });  
        });

这是我输入过程的代码。

<?php
    session_start();
    include_once('koneksi.php');

    if(isset($_POST['add'])){
    $kodepg         = $_POST['kodepg'];
    $noformpg       = $_POST['noformpg'];
    $waktupg        = $_POST['waktupg'];
    $deptpg         = $_POST['deptpg'];
    $lokasipg       = $_POST['lokasipg'];
    $merkpg         = $_POST['merkpg'];
    $tahunpg        = $_POST['tahunpg'];
    $op             = isset($_POST['oppg']) && is_array($_POST['oppg']) ? $_POST['oppg'] : [];
    $oppg           = implode(", ", $op);
    $keluhanpg      = isset($_POST['keluhanpg']) && is_array($_POST['keluhanpg']) ? $_POST['keluhanpg'] : [];
    $mk             = isset($_POST['mkpg']) && is_array($_POST['mkpg']) ? $_POST['mkpg'] : [];
    $mkpg           = implode(", ", $mk);
    $shiftpg        = $_POST['shiftpg'];
    $statuspg       = $_POST['statuspg'];

        foreach ($_POST['keluhanpg'] as $key => $value) {
        $sql = "INSERT INTO tb_pg_cs (kode_pg,noform_pg,waktu_pg,dept_pg,lokasi_pg,merk_pg,tahun_pg,op_pg,keluhan_pg,mk_pg,shift_pg,status_pg) VALUES ('" . $kodepg . "','".$noformpg . "','".$waktupg . "','".$deptpg . "','".$lokasipg . "','".$merkpg . "','".$tahunpg . "','".$oppg . "','".$_POST['keluhanpg'][$key] . "','".$mkpg. "','".$shiftpg . "','".$statuspg . "')";
            //use for MySQLi OOP
            if($connect->query($sql)){
                $_SESSION['success'] = 'Data added successfully';
            }
            else{
                $_SESSION['error'] = 'Something went wrong while adding';
            }
        }
    }   

    header("location: ".$base_url."index.php?page=pengajuan");
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题