javascript - How to save a random number to mysql in codeigniter -
please can want generate randoms strings base on requested numbers , store them table in mysql database using mvc pattern of codeigniter. controller
public function generate() { $this->news_model->generate_pin(); $this->load->helper('string'); echo random_string('alnum',15);
the model
public function generate_pin(){ $this->load->helper('string'); $this->db->insert('pin', $number);
but nothing happening want run 10 array @ time
you should handle application logic in controller instead of in model. example insert 10 randomly generated strings database table:
// controller public function generate() { // load string helper $this->load->helper('string'); $pins = array(); // holder of pin codes $num_pins = 10; // number of pin codes want generate $len_pins = 15; // length of pin codes want generate // loop , generate pin codes for($i = 0; $i < $num_pins; $i++) { //replace 'pin_fieldname' whatever field name have in table 'pin' $pins[] = array('pin_fieldname'=>random_string('alnum', $len_pins)); //if want have different lengths of pin codes //you can put logic here change $len_pins // $len_pins = {whatever number}; } // insert pins database if($this->news_model->insert_pins($pins)){ // success insert... something... } else { // failed insert.. something... } }
and model:
// model public function insert_pins($pins){ // load database want use (if not done elsewhere) $this->db = $this->load->database(); //https://www.codeigniter.com/user_guide/database/connecting.html return $this->db->insert_batch('pin', $pins); }
Comments
Post a Comment