循环返回超过预期

时间:2018-01-18 11:44:47

标签: codeigniter-3

我有一种方法可以计算和更新赞助商但是确实它确实有效,但它的回报率更高,而且计数不准确。

这是我的代码

public function get_sponsor($downline_username,$user_name){
        if(!isset($downline_username)){
            return;
        }
        //prevent sponsor to count itself
        if ($downline_username == $user_name) {
            return;
        }

        //first update this sponsor before update sponsor sponsor
        $counter    =$this->count_dis_sponsor($downline_username);
        $counter    =$counter+1;
        $data   =array('counter'=>$counter);
        $this->db->set($data);
        $this->db->where('downline_name',$downline_username);
        $this->db->update('downlines_counter');


        //look for sponsor sponsor
        $this->db->where('downline_username',$downline_username);
        $query  =$this->db->get('downlines');
        if($query->num_rows() > 0){
            foreach ($query->result_array() as $row){
                $sponsor_id     =$row['sponsor_id_fk'];
                echo $sponsor_id.' sponsor';
                if($sponsor_id !='' && isset($sponsor_id)) {
                    //update the counter table
                    $counter = $this->count_dis_sponsor($sponsor_id);
                    $counter = $counter + 1;
                    $data = ['counter' => $counter];
                    $this->db->set($data);
                    $this->db->where('downline_name', $sponsor_id);
                    $this->db->update('downlines_counter');

                    if ($sponsor_id != '' && isset($sponsor_id)) {
                        $this->get_sponsor($sponsor_id,$user_name);
                    }
                }
            }
        }
    }

所以它继续两次打电话给赞助商赞助商 感谢

1 个答案:

答案 0 :(得分:0)

在赞助商赞助商代码区块,也许您可​​以添加条件验证,就像当前赞助商一样:

    ...
    //look for sponsor sponsor
    $this->db->where('downline_username',$downline_username);
    $query  =$this->db->get('downlines');
    if($query->num_rows() > 0){
        foreach ($query->result_array() as $row){
            $sponsor_id     =$row['sponsor_id_fk'];
            echo $sponsor_id.' sponsor';
            if($sponsor_id !='' && isset($sponsor_id)) {
                //prevent sponsor sponsor to count itself
                if ($sponsor_id == $user_name) {
                    return;
                }
                //update the counter table
                $counter = $this->count_dis_sponsor($sponsor_id);
                $counter = $counter + 1;
                $data = ['counter' => $counter];
                $this->db->set($data);
                $this->db->where('downline_name', $sponsor_id);
                $this->db->update('downlines_counter');

                // if ($sponsor_id != '' && isset($sponsor_id)) {
                    $this->get_sponsor($sponsor_id,$user_name);
                // }
            }
        }
    }
    ...

在该代码块的内部,这种情况毫无用处:

if ($sponsor_id != '' && isset($sponsor_id)) {

因为它位于完全相同的条件逻辑的内部,并且$sponsor_id根本没有改变,所以它总是会被传递。