在 Codeigniter 中为卖家创建和添加自定义组
Posted
技术标签:
【中文标题】在 Codeigniter 中为卖家创建和添加自定义组【英文标题】:Creating and adding custom groups for sellers in Codeigniter 【发布时间】:2021-05-05 20:32:47 【问题描述】:我是 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
【问题讨论】:
如何将变量发送到视图?显示控制器的基本部分(顺便说一句,将显示的模型代码也减少到基本部分) 我们需要看看 Filename: groups/profile.php 中第 70 行附近发生了什么。 【参考方案1】:正如我在视图中的代码中看到的那样,您已经定义了 foreach($vendor_groups as $group) 并且在声明下拉菜单 "render_select_with_input_groups('groups_in[]',$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);
?>
【讨论】:
以上是关于在 Codeigniter 中为卖家创建和添加自定义组的主要内容,如果未能解决你的问题,请参考以下文章
如何在Active Admin中为自定义生成的页面添加导出为csv选项
我想在电子js中为关于应用程序(对于windows)创建自定义子窗口
如何在 CodeIgniter 中为 403 禁止错误设置自定义页面