简体   繁体   中英

How to show auto generate id automatically in view codeigniter

I am a newbie in CI and I got stuck with some issues on my current project. So here is my question:

1. I already succeded create auto generate number function for a text field, but I don't know how to show or place it automatically at my specific text field in view page whenever I hit "Create New" button.

https://ibb.co/mUr899
https://ibb.co/j9BVNU

2. And let say I have used/saved one of my auto generate number on my database, I want it can check if it already used or not, if the number already used then it should generate new number.

And here is my code:
My Controller

//------------------- input data SPL -------------------------//
public function input()
{
    $this->autogenerate();
    $this->load->view('spl/spl_add_view');
}

//----------------------- auto generate no_spl -------------------------//
function autogenerate()
{
    $kode_spl = 'SPL';
    $tgl = date("ymd");

    for ($counter = 1; $counter <= 10; $counter++) {
        $spl_no = date('ymd', strtotime($tgl)) . str_pad($counter, 3, 0, STR_PAD_LEFT);
        echo $kode_spl . $spl_no . '<br />';
    }
}

My View:

        <div class="form-group">
            <label class="col-sm-2 control-label">No. SPL</label>
            <div class="col-sm-4">
                <input type="text" name="inospl" class="form-control" placeholder="No. SPL" value="<?php echo set_value('inospl'); ?>">
            </div>
        </div>

can anyone help me? thank you very much.

Your autogenerate() function echo 10 codes as a string, but doesn't return anything back to the controller.
You should return the codes generated in a more useful format, like an array and the pass it to the view using $this->load->view('spl/spl_add_view', $generated_codes);

So you will end with something like that

function autogenerate() {
    $kode_spl = 'SPL';
    $tgl = date("ymd");
    //create the empty array we are going to fill with the codes
    $codes = array();

    for ($counter = 1; $counter <= 10; $counter++) {
        $spl_no = date('ymd', strtotime($tgl)) . str_pad($counter, 3, 0, STR_PAD_LEFT);
        //insert the generated code into the $codes array
        array_push($codes, $kode_spl . $spl_no);
    }
    //return the codes created
    return $codes;
}

Now we are going to pass the results to the view, so:

public function input() {
    //save the codes into a variable for later use
    $data['generated_codes'] = $this->autogenerate();
    $this->load->view('spl/spl_add_view', $data);
}

Now your codes are accessible in the view with a simple loop on $generated_codes

Regarding the last question if you want to check the results you should query your database and loop through the results checking if the newly generated code already exists. But it's not really efficient since you don't know how many records you are going to cycle in the future. Have you tried something so far?

Finally I found the answer and this case is closed. here's what I've changed in my code:

My Model:

public function auto_generate() 
{
    $tgl = date("ymd"); //date(Ymd) : jika mau tahun 4 digit

    $this->db->select('RIGHT(tbl_spl.no_spl,3) as kode', FALSE);
    $this->db->order_by('no_spl', 'DESC');
    $this->db->limit(1);
    $query = $this->db->get('tbl_spl');
    if($query->num_rows() <> 0) {
        $data = $query->row();
        $kode = intval($data->kode) + 1;
    }
    else {
        $kode = 1;
    }

    $kodemax = date('ymd', strtotime($tgl)) . str_pad($kode, 3, 0, STR_PAD_LEFT); 
    $kodejadi = "SPL". $kodemax;

    return $kodejadi;
}

My controller:

public function input()
{
    $data['autogen'] = $this->M_spl->auto_generate();
    $this->load->view('spl/spl_add_view', $data);
}

My View:

        <div class="form-group">
            <label class="col-sm-2 control-label">No. SPL</label>
            <div class="col-sm-4">
                <input type="text" name="inospl" class="form-control" placeholder="No SPL" value="<?php echo $autogen; ?>" readonly>
            </div>
        </div>

and voilaa!! whenever I hit "Create New" button, the auto generate number in my inospl text field is automatically showed up and increased.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM