duanjia9577 2014-04-03 07:58
浏览 29

是否有可能变量isset()然后处理第二个函数?

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.

  • 写回答

1条回答 默认 最新

  • douchujian8124 2014-04-03 08:07
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)