How to insert data into a table using a loop.?
I want to insert data into a database table using a loop. To do this, I first need to get the table data Account_id
from user profile
. When a user is logged into my website, his / her name table is created automatically, I store this table name in Account_id
. why should i need this field.
First I get all registered users Account_id
value. And then I search this table using for loop in the model.
After I find this table, a row should be inserted which is dynamically generated like
$data = array(
'Paper_Name' => $paper_name,
'Paper_Type' => $paper_type,
'paid_type' => $paid_type
);
Here is my model code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class create_main_table_model extends CI_Model {
function login($data){
$query = $this->db->insert('main_table', $data);
$update_userdata = $this->db->select('Account_id')->from('userprofile')->get();
for($i =0; $i < $update_userdata->num_rows(); $i++){
$query = $this->db->insert($update_userdata[$i], $data);
}
return ($this->db->affected_rows() > 0) ? true : false;
}
}
?>
I want to insert data into tables available in Account_id
. If it is possible with another method, please tell me.
source to share
This is the perfect solution.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class create_main_table_model extends CI_Model {
function login($data){
$pass_data = $data;
$query = $this->db->insert('main_table', $data);
$update_userdata = $this->db->select('Account_id')->from('userprofile')->get();
for($i =0; $i < $update_userdata->num_rows(); $i++){
$data1 = $update_userdata->result();
$query = $this->db->insert($data1[$i]->Account_id, $pass_data);
}
return ($this->db->affected_rows() > 0) ? true : false;
}
}
?>
source to share
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class create_main_table_model extends CI_Model {
function login($data){
$query = $this->db->insert('main_table', $data);
$update_userdata = $this->db->select('Account_id')->from('userprofile')->get();
for($i =0; $i < $update_userdata->num_rows(); $i++){
$data['Account_id']=$update_userdata[$i]->Account_id;
$query = $this->db->insert('table_name', $data);
}
return ($this->db->affected_rows() > 0) ? true : false;
}
}
?>
source to share