PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 6, 2022

[FIXED] Creating and adding custom groups for sellers in Codeigniter

 January 06, 2022     backend, codeigniter, crm, php     No comments   

Issue

I'm new to Codeigniter, the task is create and add custom groups for in Perfex CRM based on Codeigniter

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;
}

}

I get this error in the output:

A PHP Error was encountered Severity: Notice

Message: Undefined variable: groups

Filename: groups/profile.php

Line Number: 70

In the database, I created special tables and keys, everything is ok there

Screenshoot:

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


Solution

As I can see from your code in your View you have defined foreach($vendor_groups as $group) and while declaring dropdown "render_select_with_input_groups('groups_in[]',$groups,....)" you have declared $groups instead of $group.

The code should be like below.

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);
      }
 ?>


Answered By - Bhargav Desai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing