【发布时间】: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