If I have a function like this:
public function access_1()
{
$this->db->set('Name', $name);
$this->db->set('Description', $desc);
$this->db->insert('access_user');
$id_access_1 = $this->db->insert_id();
return (isset($id_access_1)) ? _________ : FALSE;
}
In return (isset($id_access_1))
then in the blank (_____) it will process the second function and set the $id_access_1
in that function.
Like this:
public function access_2()
{
$this->db->set('Access_1_ID', ____________);
$this->db->set('Dept', $dept);
$this->db->set('Section', $section);
$this->db->insert('access_dept');
$id_access2 = $this->db->insert_id();
return (isset($id_access2)) ? $id_access2 : FALSE;
}
Below is the controller, the function access_2()
is inside for each()
, like this one:
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
$this->form_validation->set_rules('desc', 'Description', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$this->structure_management->insert_batch($data);
$data = $this->input->post('data');
$access_2Data = json_decode($data,TRUE);
foreach ($access_2Data as $getAll)
{
$this->structure_management->access_2($getAll);
}
else{
}
In the program I have right now, when I click SUBMIT button it will process the two functions but then I figure out how will I get the ID of function access_1()
if it is in AUTO_INCREMENT
. And you will notice in the seconde function access_2()
, there's a blank (__________) in $this->db->set('Access_1_ID', ____________);
because there I will input the ID
in the first function access_1()
. It will set only one row for the function access_1()
and in function access_2()
it will iterate because it is inside for each()
. But every iteration I will only get only one ID which has in access_1()
when I set in database.
If you have questions, if you think my explanation seems not understandable, feel free to ask me. Thank you in advance.
You seem to be confused about what isset
function does. You already have
$id_access_1 = $this->db->insert_id();
As a result of this call, $id_access_1
will always be set; instead you need to check what it is set to. I don't know the details of your $this->db
class, but for the sake of the argument, I'll assume that method insert_id
returns the ID of the newly inserted row or FALSE
on error (as is the norm in PHP).
Then what you need to do is check whether you got FALSE
on the first call; if not, then invoke the second function and return its result, otherwise return FALSE. In the second function you don't need to check anything at all, as simply returning the result of call to insert_id
will either return FALSE
if something went wrong or the actual ID - hence you can simply return that result.
You would end up with something like this:
public function access_1()
{
$this->db->set('Name', $name);
$this->db->set('Description', $desc);
$this->db->insert('access_user');
$id_access_1 = $this->db->insert_id();
return $id_access_1 !== FALSE ? access_2($id_access_1) : FALSE;
}
public function access_2($id)
{
$this->db->set('Access_1_ID', $id);
$this->db->set('Dept', $dept);
$this->db->set('Section', $section);
$this->db->insert('access_dept');
$id_access_2 = $this->db->insert_id();
return $id_access_2;
//this will return either FALSE or the proper ID
//based on the result of $this->db->insert_id() call
}