Hi everyone.
can I ask for help about something? I want to create a user registration form where I allow user to be able to put as many emails and phones as they want just like Google Contacts.
I already create a form that allow users to add or remove the input fields for email and phone dynamically. but I haven't figured it out how to store the data into database.
see attachment for the image of the Form I created.
This is the customer_model.php I created to store the data into database.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer_model extends CI_Model {
protected $table = 'customer';
protected $table_detail = 'address';
protected $table_detail2 = 'phone';
public $avatar;
public $name;
public $address = [];
public $phone = [];
public function __construct()
{
parent::__construct();
$this->flush();
}
private function flush()
{
$this->avatar = '';
$this->name = '';
$this->address = [];
$this->phone = [];
}
private function validate()
{
if (
empty($this->avatar) ||
empty($this->name) ||
empty($this->address) ||
empty($this->phone)
) {
return FALSE;
}
return TRUE;
}
public function all()
{
$this->db->order_by('id', 'desc');
$q = $this->db->get($this->table);
$r = $q->result();
foreach ($r as $key => $value) $r[$key]->address = $this->get_address($value->id);
return $r;
}
public function get_address($customer_id)
{
$this->db->select('address.id, address_title.title, address.address');
$this->db->join('address_title', 'address_title.id = address.title_id');
$this->db->order_by('address.id', 'asc');
$q = $this->db->get_where($this->table_detail, ['customer_id' => $customer_id]);
return $q->result();
}
public function get_title($term = '')
{
$this->db->order_by('id', 'asc');
if (! empty($term)) $this->db->like('LOWER(title)', strtolower($term), 'both');
$q = $this->db->get('address_title');
return $q->result();
}
public function get($id)
{
$q = $this->db->get_where($this->table, ['id' => $id]);
return $q->num_rows() > 0 ? $q->row() : FALSE;
}
public function destroy($id)
{
return $this->db->delete($this->table, array('id' => $id));
}
public function store()
{
if (! $this->validate()) return FALSE;
$this->db->trans_begin();
try {
$this->db->insert($this->table, $this);
$id = $this->db->insert_id();
foreach ($this->address as $key => $value) $this->address[$key]['customer_id'] = $id;
$this->db->insert_batch($this->table_detail, $this->address);
} catch (Exception $e) {
return FALSE;
}
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
public function store2()
{
if (! $this->validate()) return FALSE;
$this->db->trans_begin();
try {
$this->db->insert($this->table, $this);
$id = $this->db->insert_id();
foreach ($this->phone as $key => $value) $this->phone[$key]['customer_id'] = $id;
$this->db->insert_batch($this->table_detail2, $this->phone);
} catch (Exception $e) {
return FALSE;
}
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
}
/* End of file Customer.php */
/* Location: ./application/models/Customer.php */
this is the customer.php (Controller) I created to get the data from View and pass it to customer_model.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper(['form', 'url']);
$this->load->model('customer_model', 'model', TRUE);
}
public function index()
{
$this->load->view('customer.index.php', [
'data' => $this->model->all(),
'notification' => $this->session->flashdata('notification')
]);
}
public function get_avatar($id)
{
header('Content-type : image/jpeg');
echo $this->model->get($id)->avatar;
}
public function get_title()
{
$titles = $this->model->get_title($this->input->get('q', TRUE));
echo json_encode((object)[
'items' => $titles
]);
}
public function store()
{
$this->model->name = $this->input->post('name', TRUE);
$this->model->avatar = file_get_contents($_FILES['userfile']['tmp_name']);
$this->model->address = [];
$this->model->phone = [];
foreach ($this->input->post('title_id', TRUE) as $key => $value) $this->model->address[$key]['title_id'] = $value;
foreach ($this->input->post('address', TRUE) as $key => $value) $this->model->address[$key]['address'] = $value;
foreach ($this->input->post('title_id', TRUE) as $key => $value1) $this->model->phone[$key]['title_id'] = $value1;
foreach ($this->input->post('phone', TRUE) as $key => $value1) $this->model->phone[$key]['phone'] = $value1;
if ($this->model->store() === TRUE) {
if ($this->model->store2() === TRUE) {
$notification = '<div class="alert alert-success">Success creating customer.</div>';
} else {
$notification = '<div class="alert alert-danger">Failed creating customer.</div>';
}
} else {
$notification = '<div class="alert alert-danger">Failed creating customer.</div>';
}
$this->session->set_flashdata('notification', $notification);
redirect(site_url('/'), 'refresh');
}
public function delete($id)
{
if ($this->model->destroy($id))
{
$notification = '<div class="alert alert-success">Success to delete customer.</div>';
} else {
$notification = '<div class="alert alert-danger">Fail to delete customer.</div>';
}
$this->session->set_flashdata('notification', $notification);
redirect(site_url('/'), 'refresh');
}
}
/* End of file Customer.php */
/* Location: ./application/controllers/Customer.php */
I run this code and it gives me error saying Array to String conversion and I am not able to store any data into database. any help would be really appreciated.
here's the error I get.
Code:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 1496
Backtrace:
File: C:\xampp\htdocs\ci-master-detail-trans\application\models\Customer_model.php
Line: 103
Function: insert_batch
File: C:\xampp\htdocs\ci-master-detail-trans\application\controllers\Customer.php
Line: 50
Function: store
File: C:\xampp\htdocs\ci-master-detail-trans\index.php
Line: 292
Function: require_once
any help regarding create a Multi Detail Form like this? I'd be really grateful if someone can help me with this.
Attached Files
Thumbnail(s)