PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] How to show auto generate id automatically in view codeigniter

 January 10, 2022     codeigniter, php     No comments   

Issue

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.


Solution

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.



Answered By - Dhefry
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing