CodeIgniter Forums

Full Version: How to generate serial number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

How to create code for auto generate number base on quantity,
For example, I put quantity '999' into form and after I click "submit" system auto generate serial number 1~999 to database table (product_serial_number).

database table : product_serial_number

serial_id | serial_group_id | product_number | serial_number

1 | 55 | 54321 | 1
2 | 55 | 54321 | 2
3 | 55 | 54321 | 3
4 | 55 | 54321 | 4
5 | 55 | 54321 | 5
6 | 55 | 51112 | 1
7 | 55 | 51112 | 2
8 | 55 | 51112 | 3
9 | 55 | 51112 | 4
10 | 55 | 51112 | 5

11 | 55 | 51112 | 6
12 | 55 | 51112 | 7
13 | 55 | 51112 | 8
14 | 55 | 51112 | 9

15 | 55 | 51112 | 10
16 | 55 | 51112 | 11


Form stock_serial_generate.php :
PHP Code:
<?php
        
echo form_open(base_url() . 'index.php/admin/stock/do_generate/', array(
            
'class' => 'form-horizontal',
            
'method' => 'post',
            
'id' => 'generate_serial',
            
'enctype' => 'multipart/form-data'
        
));
    
?>
        <div class="panel-body">

            <input type="hidden" name="product" value="<?php echo $product?>">
            <input type="hidden" name="category" value="<?php echo $this->crud_model->get_type_name_by_id('product',$product,'category'); ?>">
            <input type="hidden" name="sub_category" value="<?php echo $this->crud_model->get_type_name_by_id('product',$product,'sub_category'); ?>">

           
            <div class="form-group">
                <label class="col-sm-4 control-label" for="demo-hor-1"><?php echo translate('current_quantity');?></label>
                <div class="col-sm-6">
                    <input type="number" disabled value="<?php echo $this->crud_model->get_type_name_by_id('product',$product,'current_stock'); ?>" class="form-control totals">
                </div>
            </div>
        </div>
    </form> 


In Controller Admin.php :

PHP Code:
} else if ($para1 == 'do_generate') {
 
           $data['serial_group_id']    = $this->input->post('serial_group_id');
 
           $data['product_number']     = $this->input->post('product_number');
 
           $data['serial_number'     $this->input->post('serial_number');
 
           $this->db->insert('product_serial_number'$data);
 
           recache();
        }

} elseif (
$para1 == 'generate') {

  $this->load->view('back/admin/stock_serial_generate'); 


I'm new with codeigniter, I hope some one can help me.
With a for loop or a while loop perhaps, then use batch_update to insert.
May be try some thing like


PHP Code:
public function generate() 
{
    for($i 1$i 10$i++)
    {
        $key strtoupper(substr(sha1(microtime() . $i), rand(05), 25));
        $serial implode("-"str_split($key5));
    }
    
    return $serial
;

It's not about random numbers is it, but a range of numbers, starting with 1 and ending with the quantity that was posted by a form?
PHP Code:
$qty $this->input->post('quantity');
for (
$x 1$x $qty$x++) {
  
//build array with data you want to store in the database here
}
//insert the data into the database with insert_batch(), like PaulD already suggested. 
Hi,

Thanks for reply..

I success to generate row in table,

and now how auto generate serial base on quantity

for example I put quantity 50 and system will create serial number 1 ~ 50

Please correct my code,


PHP Code:
$count $this->input->post('current_stock');
for(
$i=0$i<$count$i++) {
$data = array(
'serial_number' =>($i<$count)
);
$this->db->insert('product_serial_number'$data);



Thanks
Your solution

PHP Code:
$data = array(
'serial_number' =>($i<$count)
); 

won't give the correct result, because ($i < $count) will return the boolean value TRUE, not a number. Replace it with:
PHP Code:
$data = array( 'serial_number' =>$i ); 
(09-05-2016, 08:26 AM)Wouter60 Wrote: [ -> ]Your solution

PHP Code:
$data = array(
'serial_number' =>($i<$count)
); 

won't give the correct result, because ($i < $count) will return the boolean value TRUE, not a number. Replace it with:
PHP Code:
$data = array( 'serial_number' =>$i ); 


Thanks wouter60,

It's work...
* I add code '+ '1'' because system generate serial start from 0 so I put +1 serial start from 1.
PHP Code:
$count $this->input->post('current_stock');
            for(
$i=0$i<$count$i++) {
                
$data = array(
                    
'serial_number' => $i '1',
                    
'product_id' => $id
                
);
                
$this->db->insert('product_serial_number'$data);
            } 

How to set serial number group,
Please correct my code,

PHP Code:
$count $this->input->post('current_stock');
            for(
$i=0$i<$count$i++) {
                
$data = array(
                    
'serial_number' => $i '1',
                    
'product_id' => $id
                
);
                
$this->db->insert('product_serial_number'$data);
            }
            
            
$ser_num_stock $this->db->get_where('product_serial_number', array('serial_number' => ''))->row()->value;
            
$golden = array('1''2''3''4''5''6''7''8''9''11''22''33''44''55''66''77''88''99''111''222''333''444''555''666''777''888''999''1111''2222''3333''4444');
            
$premium = array('10','20','40','50','60','70','80','90','100','101','200','300','303','400','404','500','505','600','606','700','707','800','808','900','909','1000','1001','1010','1100','1122','1133','1144','1155','1166','1177','1188','1199','1212','1221','1313','1331','1414','1441','1515','1551','1616','1661','1717','1771','1818','1881','1919','1991','2000','2002','2020','2112','2121','2200','2211','2233','2244','2255','2266','2277','2288','2299','2323','2332','2424','2442','2525','2552','2626','2662','2727','2772','2828','2882','2929','2992','3000','3003','3030','3113','3131','3232','3344','3355','3366','3377','3388','3399','3434','3443','3535','3553','3636','3663','3737','3773','3838','3883','3939','3993','4000','4004','4040','4114','4141','4224','4242','4334','4343','4400','4411','4242','4433','4455','4466','4477','4488','4499','4545','4554','4646','4664','4747','4774','4848','4884','4949','4994','5000');
            if (
$ser_num_stock == $golden) {
                
$data['serial_group_id']    = '3';
            } elseif (
$ser_num_stock == $premium) {
                
$data['serial_group_id']    = '2';
            } else {
                
$data['serial_group_id']    = '1';
            }
            
$this->db->where('serial_group_id'$data['product_serial_number']);
            
$this->db->update('product_serial_number'$data);
 
           recache(); 

Thanks