【问题标题】:How to create empty rows on user creation in codeigniter如何在codeigniter中创建用户时创建空行
【发布时间】:2015-10-26 11:04:14
【问题描述】:

我正在使用 PHP Codeigniter。在这张表中,我有一个 Retailer 表,其中我有一个字段 No of Users 需要零售商。当管理员创建零售商时,他在字段 no of users 中插入的值是否有可能必须在另一个表 User 中创建那么多行。我只想在用户表中创建那么多空行,这些行映射到零售商 ID。用户表中的用户有一个字段 Key 必须是零售商的唯一键。我是codeigniter的新手,没有任何逻辑如何做到这一点。 以下是我创建零售商的代码: 控制器代码:

<?php

类 Reseller 扩展 Admin_Controller {

public function __construct ()
{
    parent::__construct();
}

public function index ()
{
    // Fetch all users
    $this->data['users'] = $this->reseller_m->get();

    // Load view
    $this->data['subview'] = 'admin/reseller/index';
    $this->load->view('admin/_layout_main', $this->data);
}
    public function reseller ()
{
    // Fetch all users
    $this->data['users'] = $this->reseller_m->get();

    // Load view
    $this->data['subview'] = 'admin/reseller/users'; //Users variable stores the suvbview 
    $this->load->view('admin/_layout_main', $this->data);
}


public function edit ($id = NULL)
{
    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->reseller_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->reseller_m->get_new();
    }

    // Set up the form
    $rules = $this->reseller_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {

$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone',' balance','user_num','address','country','created','modified','status'));

        $data['password'] = $this->reseller_m->hash($data['password']);


        $key=$this->reseller_m->save($data, $id);
        for($i=1; $i<=$data['user_num'];$i++)
            {
            $userdata=array('key'=>$key);
        // here users is taken name of user table with retailer_id is field
            $this->db->insert('users',$userdata);
             }



        redirect('admin/reseller');
    }

    // Load the view
    $this->data['subview'] = 'admin/reseller/edit';
    $this->load->view('admin/_layout_main', $this->data);
}


public function delete ($id)
{
    $this->reseller_m->delete($id);
    redirect('admin/reseller');
}

}

模型:

<?php

类 Reseller_M 扩展 MY_Model {

protected $_table_name = 'reseller';
protected $_order_by = 'name';
protected $_timestamps = TRUE;
public $rules = array(
    'email' => array(
        'field' => 'email', 
        'label' => 'Email', 
        'rules' => 'trim|required|valid_email|xss_clean'
    ), 
    'password' => array(
        'field' => 'password', 
        'label' => 'Password', 
        'rules' => 'trim|required'
    ),

);
public $rules_admin = array(
    'name' => array(
        'field' => 'name', 
        'label' => 'Name', 
        'rules' => 'trim|required|xss_clean'
    ), 
    'email' => array(
        'field' => 'email', 
        'label' => 'Email', 
        'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
    ), 
    'password' => array(
        'field' => 'password', 
        'label' => 'Password', 
        'rules' => 'trim|matches[password_confirm]'
    ),
    'password_confirm' => array(
        'field' => 'password_confirm', 
        'label' => 'Confirm password', 
        'rules' => 'trim|matches[password]'
    ),
    'sip_username' => array(
        'field' => 'sip_username', 
        'label' => 'Sip Username', 
        'rules' => 'trim|required|'
    ),
    'sip_password' => array(
        'field' => 'sip_password', 
        'label' => 'SIP Password', 
        'rules' => 'trim|required|'
    ),


);

function __construct ()
{
    parent::__construct();

}




public function get_new(){
    $user = new stdClass();

//          $user->id = '';
    $user->sip_username='';
    $user->sip_password='';
    $user->key='';
    $user->allocation_block='';
    $user->name='';
    $user->email = '';      
    $user->password = '';
    $user->phone=''; 
    $user->user_num=''; 
    $user->address = '';
    $user->status = '';
    $user->country=''; 
    $user->created = '';
    $user->modified  = '';
    $user->balance = '';
    return $user;
    $this->session->set_userdata($data);

}

public function get_user_profile(){
    $this->db->select('id,sip_password,sip_username,phone,key,address,country,created,modified,user_num,allocation_block,status,email, name, balance');
    $query = $this->db->get_where('reseller', array('id' => $this->session->userdata('id')));
    return $query->result();
}






public function hash ($string)
{
    return hash('sha512', $string . config_item('encryption_key'));
}


public function loggedin ()
{
    return (bool) $this->session->userdata('loggedin');
}

}

基础模型:

<?php

类 MY_Model 扩展 CI_Model {

protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;

function __construct() {
    parent::__construct();
}

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}


public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }


    return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE){
    $this->db->where($where);
    return $this->get(NULL, $single);
}

public function save($data, $id = NULL){


    $this->db->cache_on();

    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}

public function delete($id){
    $filter = $this->_primary_filter;
    $id = $filter($id);

    if (!$id) {
        return FALSE;
    }
    $this->db->where($this->_primary_key, $id);
    $this->db->limit(1);
    $this->db->delete($this->_table_name);
}

}

【问题讨论】:

  • 为什么要做空行
  • 为每一行插入一个唯一 id 以区分,然后用该唯一 id 替换您的零售,这是 codeiginiter 框架
  • @ArunYokesh 我认为我应该在创建零售商时执行一个 for 循环,但我不知道该怎么做以及在哪里做!
  • @Uchiha 我想让零售商添加其他详细信息。管理员将只提供用户名和密码。因此,当创建经销商时,将仅使用用户名和密码以及其他空白字段创建空行
  • 请发布您的 reseller_m 保存功能,然后我会修改并让您知道在哪里更改

标签: php mysql codeigniter authentication


【解决方案1】:

修改如下函数。

  public function edit ($id = NULL)
 {
// Fetch a user or set a new one
if ($id) {
    $this->data['user'] = $this->reseller_m->get($id);
    count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
    $this->data['user'] = $this->reseller_m->get_new();
}

// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);

// Process the form
if ($this->form_validation->run() == TRUE) {
    $data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email', 'password','phone','user_num','address','status','country','created','modified','balance'));
    $data['password'] = $this->reseller_m->hash($data['password']);
    //modify here
    $retailer_id=$this->reseller_m->save($data, $id); 
    for($i=1; $i<=$data['user_num'],$i++)
    {
        $userdata=array('retailer_id'=>$retailer_id);
        // here users is taken name of user table with retailer_id is field
        $this->db->insert('users',$userdata);
    }

    redirect('admin/reseller');
 }

  // Load the view
  $this->data['subview'] = 'admin/reseller/edit';
  $this->load->view('admin/_layout_main', $this->data);
 }

【讨论】:

  • 您给我的代码插入了在经销商字段中插入的值。我的意思是当我插入经销商时,我填写了一些详细信息并有一个字段 user_num 是他请求的用户数。他将有权访问这些用户。所以我想在用户表中插入一个空白行,但只是希望这些行与 reseller_id 映射我希望你能得到我
  • //这里修改 $retailer_id=$this->reseller_m->save($data, $id); for($i=1; $i$retailer_id); // 这里的 users 是用户表的名称,retailer_id 是字段 $this->db->insert('users',$userdata);这会做到这一点,只需根据您的表更改表名和字段名
  • @shanusigh 我已经更改了凭据,它也动态创建了用户,但例如我插入了 10 作为 user_num。所以它所做的是在我的经销商表中创建了 8 行,在用户表中创建了 2 行。除了密钥(经销商的唯一 ID)之外,用户表也应该是空白的
  • 如果 user_num 为 10,那么它将在用户表中插入 10 行,用户主键和 reseller_id,如果您想添加其他字段,则可以根据该字段进行修改。
  • 是的,它应该按照您的评论执行,但它所做的是在经销商表中插入 8 条记录,在用户表中插入 2 条记录。我已经更新了问题代码..请检查编辑方法并告诉我哪里出错了
猜你喜欢
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 2021-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
相关资源
最近更新 更多