在 Codeigniter 中为卖家创建和添加自定义组

Creating and adding custom groups for sellers in Codeigniter

我是 Codeigniter 的新手,任务是基于 Codeigniter 在 Perfex CRM 中创建和添加自定义组

view.php

 <?php 
                 $selected = array();
                 if(isset($vendor_groups)){
                   foreach($vendor_groups as $group){
                      array_push($selected,$group['groupid']);
                   }
                 }
                 if(is_admin() || get_option('staff_members_create_inline_customer_groups') == '1'){
                  echo render_select_with_input_groups('groups_in[]',$groups,array('id','name'),'vendor_groups',$selected,'<a href="#" data-toggle="modal" data-target="#customer_group_modal"><i class="fa fa-plus"></i></a>',array('multiple'=>true,'data-actions-box'=>true),array(),'','',false);
                  } else {
                    echo render_select('groups_in[]',$groups,array('id','name'),'vendor_groups',$selected,array('multiple'=>true,'data-actions-box'=>true),array(),'','',false);
                  }
                 ?>

model.php

 public function get_vendor_groups($id)
{
    $this->db->where('vendor_id', $id);

    return $this->db->get(db_prefix().'vendor_groups')->result_array();
}

/**
 * Get all customer groups
 * @param  string $id
 * @return mixed
 */
public function get_groups($id = '')
{
    if (is_numeric($id)) {
        $this->db->where('id', $id);

        return $this->db->get(db_prefix().'vendorrs_groups')->row();
    }
    $this->db->order_by('name', 'asc');

    return $this->db->get(db_prefix().'vendors_groups')->result_array();
}

/**
 * Edit customer group
 * @param  array $data $_POST data
 * @return boolean
 */
public function edit($data)
{
    $this->db->where('id', $data['id']);
    $this->db->update(db_prefix().'vendors_groups', [
        'name' => $data['name'],
    ]);
    if ($this->db->affected_rows() > 0) {
        log_activity('Customer Group Updated [ID:' . $data['id'] . ']');

        return true;
    }

    return false;
}

/**
 * Delete customer group
 * @param  mixed $id group id
 * @return boolean
 */
public function delete($id)
{
    $this->db->where('id', $id);
    $this->db->delete(db_prefix().'vendors_groups');
    if ($this->db->affected_rows() > 0) {
        $this->db->where('groupid', $id);
        $this->db->delete(db_prefix().'vendor_groups');

        hooks()->do_action('vendor_group_deleted', $id);

        log_activity('Customer Group Deleted [ID:' . $id . ']');

        return true;
    }

    return false;
}

/**
* Update/sync customer groups where belongs
* @param  mixed $id        customer id
* @param  mixed $groups_in
* @return boolean
*/
public function sync_customer_groups($id, $groups_in)
{
    if ($groups_in == false) {
        unset($groups_in);
    }
    $affectedRows    = 0;
    $vendor_groups = $this->get_vendor_groups($id);
    if (sizeof($vendorr_groups) > 0) {
        foreach ($vendor_groups as $vendor_group) {
            if (isset($groups_in)) {
                if (!in_array($vendor_group['groupid'], $groups_in)) {
                    $this->db->where('vendor_id', $id);
                    $this->db->where('id', $vendorr_group['id']);
                    $this->db->delete(db_prefix().'vendor_groups');
                    if ($this->db->affected_rows() > 0) {
                        $affectedRows++;
                    }
                }
            } else {
                $this->db->where('vendor_id', $id);
                $this->db->delete(db_prefix().'vendor_groups');
                if ($this->db->affected_rows() > 0) {
                    $affectedRows++;
                }
            }
        }
        if (isset($groups_in)) {
            foreach ($groups_in as $group) {
                $this->db->where('vendor_id', $id);
                $this->db->where('groupid', $group);
                $_exists = $this->db->get(db_prefix().'vendor_groups')->row();
                if (!$_exists) {
                    if (empty($group)) {
                        continue;
                    }
                    $this->db->insert(db_prefix().'vendor_groups', [
                        'vendor_id' => $id,
                        'groupid'     => $group,
                    ]);
                    if ($this->db->affected_rows() > 0) {
                        $affectedRows++;
                    }
                }
            }
        }
    } else {
        if (isset($groups_in)) {
            foreach ($groups_in as $group) {
                if (empty($group)) {
                    continue;
                }
                $this->db->insert(db_prefix().'vendor_groups', [
                    'vendor_id' => $id,
                    'groupid'     => $group,
                ]);
                if ($this->db->affected_rows() > 0) {
                    $affectedRows++;
                }
            }
        }
    }

    if ($affectedRows > 0) {
        return true;
    }

    return false;
}

}

我在输出中得到这个错误:

A PHP Error was encountered Severity: Notice

Message: Undefined variable: groups

Filename: groups/profile.php

Line Number: 70

数据库中,我创建了特殊的表和键,那里一切正常

屏幕截图:

https://i.stack.imgur.com/BrLnq.png

正如我从您在视图中的代码中看到的那样,您已将 foreach($vendor_groups 定义为 $group) 并在声明下拉菜单时 "render_select_with_input_groups('groups_in[]',$groups, ....)" 你已经声明了 $groups 而不是 $group.

代码应该如下所示。

view.php

 <?php 
     $selected = array();
     if(isset($vendor_groups)){
       foreach($vendor_groups as $group){
          array_push($selected,$group['groupid']);
       }
     }
     if(is_admin() || get_option('staff_members_create_inline_customer_groups') == '1'){
      echo render_select_with_input_groups('groups_in[]',$group,array('id','name'),'vendor_groups',$selected,'<a href="#" data-toggle="modal" data-target="#customer_group_modal"><i class="fa fa-plus"></i></a>',array('multiple'=>true,'data-actions-box'=>true),array(),'','',false);
      } else {
        echo render_select('groups_in[]',$groups,array('id','name'),'vendor_groups',$selected,array('multiple'=>true,'data-actions-box'=>true),array(),'','',false);
      }
 ?>