I made a table hobby, in that i took an array named hid[].
In that i passed hid and hnm.
I have another table employee and I take hid as an foreign key.
When I passes the hid from front end as :
hid :
[0] -> 1
[1] -> 2
[2] -> 3
When I passes the hnm from front end as :
[0] -> Reading
[1] -> Swimming
[2] -> Dancing
Code :
Controller code :
<?php
class Employee extends CI_Controller
{
public function __construct()
{
# code...
parent::__construct();
$this->load->database();
$this->load->model('Employee_model');
$this->load->helper('url');
}
public function add()
{
$data['data']=$this->Employee_model->display_records();
$data['ctr_id'] = $this->db->select("*")->from('country')->get()->result();
$data['st_id'] = $this->db->select("*")->from('state')->get()->result();
$data['ct_id'] = $this->db->select("*")->from('city')->get()->result();
$data['hid'] = $this->db->select("*")->from('hobby')->get()->result();
$data['mid'] = $this->db->select("*")->from('meal_preference')->get()->result();
$data['pid'] = $this->db->select("*")->from('payment_mode')->get()->result();
return $this->load->view('employee_add',$data);
}
public function save()
{
$efname=$this->input->post('efname');
$elname=$this->input->post('elname');
$ecompany=$this->input->post('ecompany');
$eadd=$this->input->post('eadd');
$ctr_id=$this->input->post('ctr_id');
$st_id=$this->input->post('st_id');
$ct_id=$this->input->post('ct_id');
$email=$this->input->post('email');
$emobile=$this->input->post('emobile');
$hid=json_encode($this->input->post('hid'));
$oidVal = implode(", ", $this->input->post('hid'));
$data = array('oidVal' => $oidVal);
echo "<pre>"; print_r($_POST);die;
$mid=$this->input->post('mid');
$pid=$this->input->post('pid');
$output = $this->Employee_model->saverecords($efname,$elname,$ecompany,$eadd,$ctr_id,$st_id,$ct_id,$email,$emobile,$hid,$oidVal,$mid,$pid);
redirect('Employee/display');
echo "Employee records saved successfully..!!";
}
public function display()
{
$data['employee']=$this->Employee_model->display_records();
$data['hobby']=$this->Employee_model->display_hobby();
$this->load->view('employee_view',$data);
}
public function edit($id)
{
$data['employee']=$this->Employee_model->displayrecordsById($id);
$data['ctr_id'] = $this->db->select("*")->from('country')->get()->result();
$data['st_id'] = $this->db->select("*")->from('state')->get()->result();
$data['ct_id'] = $this->db->select("*")->from('city')->get()->result();
$data['hid'] = $this->db->select("*")->from('hobby')->get()->result();
$data['mid'] = $this->db->select("*")->from('meal_preference')->get()->result();
$data['pid'] = $this->db->select("*")->from('payment_mode')->get()->result();
return $this->load->view('employee_edit',$data);
}
public function update($id)
{
$efname=$this->input->post('efname');
$elname=$this->input->post('elname');
$ecompany=$this->input->post('ecompany');
$eadd=$this->input->post('eadd');
$ctr_id=$this->input->post('ctr_id');
$st_id=$this->input->post('st_id');
$ct_id=$this->input->post('ct_id');
$email=$this->input->post('email');
$emobile=$this->input->post('emobile');
$hid=json_encode($this->input->post('hid'));
$oidVal = implode(", ", $this->input->post('hid'));
$data = array('oidVal' => $oidVal);
$mid=$this->input->post('mid');
$pid=$this->input->post('pid');
$output = $this->Employee_model->update_records($id,$efname,$elname,$ecompany,$eadd,$ctr_id,$st_id,$ct_id,$email,$emobile,$hid,$oidVal,$mid,$pid);
redirect('Employee/display');
echo "Employee records Updated successfully..!!";
}
public function delete($id)
{
$this->Employee_model->deleterecords($id);
redirect('Employee/display');
echo "Record deleted successfully !";
}
}
?>
Model Code :
<?php
class Employee_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function saverecords($efname,$elname,$ecompany,$eadd,$ctr_id,$st_id,$ct_id,$email,$emobile,$hid,$oidVal,$mid,$pid)
{
# code...
$this->db->set('efname',$efname);
$this->db->set('elname',$elname);
$this->db->set('ecompany',$ecompany);
$this->db->set('eadd',$eadd);
$this->db->set('ctr_id',$ctr_id);
$this->db->set('st_id',$st_id);
$this->db->set('ct_id',$ct_id);
$this->db->set('email',$email);
$this->db->set('emobile',$emobile);
$this->db->set('hid',$hid);
$this->db->set('oidVal',$oidVal);
$this->db->set('mid',$mid);
$this->db->set('pid',$pid);
$this->db->insert('employee');
}
function display_records()
{
$query = $this->db->select('employee.eid,employee.efname,employee.elname,employee.ecompany,employee.eadd,country.ctr_nm,state.st_nm,city.ct_nm,employee.email,employee.emobile,hobby.hnm,employee.oidVal,meal_preference.mnm,payment_mode.pnm');
$this->db->from('employee');
$this->db->join('country', 'employee.ctr_id = country.ctr_id','LEFT');
$this->db->join('state', 'employee.st_id = state.st_id','LEFT');
$this->db->join('city', 'employee.ct_id = city.ct_id','LEFT');
$this->db->join('hobby', 'employee.hid = hobby.hid','LEFT');
$this->db->join('meal_preference', 'employee.mid = meal_preference.mid','LEFT');
$this->db->join('payment_mode', 'employee.pid = payment_mode.pid','LEFT');
$query=$this->db->get();
return $query->result();
}
function display_hobby()
{
$query = $this->db->select('hobby.hid,hobby.hnm');
$this->db->from('hobby');
$query=$this->db->get();
return $query->result();
}
function displayrecordsById($eid)
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('country', 'employee.ctr_id = country.ctr_id','LEFT');
$this->db->join('state', 'employee.st_id = state.st_id','LEFT');
$this->db->join('city', 'employee.ct_id = city.ct_id','LEFT');
$this->db->join('hobby', 'employee.hid = hobby.hid','LEFT');
$this->db->join('meal_preference', 'employee.mid = meal_preference.mid','LEFT');
$this->db->join('payment_mode', 'employee.pid = payment_mode.pid','LEFT');
$this->db->where('eid',$eid);
$query=$this->db->get();
return $query->row();
}
function update_records($eid,$efname,$elname,$ecompany,$eadd,$ctr_id,$st_id,$ct_id,$email,$emobile,$hid,$oidVal,$mid,$pid)
{
$data = array
(
'efname' => $efname,
'elname' => $elname,
'ecompany' => $ecompany,
'eadd' => $eadd,
'ctr_id' => $ctr_id,
'st_id' => $st_id,
'ct_id' => $ct_id,
'email' => $email,
'emobile' => $emobile,
'hid' => $hid,
'oidVal' => $oidVal,
'mid' => $mid,
'pid' => $pid,
);
$this->db->where('eid', $eid);
$this->db->update('employee', $data);
}
function deleterecords($eid)
{
$this->db->where('eid', $eid);
$this->db->delete('employee');
}
}
?>
Add Employee(Add record) code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<h2>Add Employees</h2>
<form method="post" action="save">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Enter First Name </td>
<td width="329"><input type="text" name="efname"></td>
</tr>
<tr>
<td width="230">Enter Last Name </td>
<td width="329"><input type="text" name="elname"></td>
</tr>
<tr>
<td width="230">Enter Company </td>
<td width="329"><input type="text" name="ecompany"></td>
</tr>
<tr>
<td width="230">Enter Adrress </td>
<td width="329"><input type="text" name="eadd"></td>
</tr>
<tr>
<td width="230">Select Country </td>
<td>
<select name="ctr_id">
<?php foreach ($ctr_id as $key => $value) : ?>
<option value="<?php echo $value->ctr_id ?>"><?php echo $value->ctr_nm; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td width="230">Select State </td>
<td>
<select name="st_id">
<?php foreach ($st_id as $key => $value) : ?>
<option value="<?php echo $value->st_id ?>"><?php echo $value->st_nm; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td width="230">Select City </td>
<td>
<select name="ct_id">
<?php foreach ($ct_id as $key => $value) : ?>
<option value="<?php echo $value->ct_id ?>"><?php echo $value->ct_nm; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td width="230">Enter Email </td>
<td width="329"><input type="email" name="email"></td>
</tr>
<tr>
<td width="230">Enter Mobile number </td>
<td width="329"><input type="text" name="emobile"></td>
</tr>
<tr>
<td width="230">Enter Hobby</td>
<td>
<?php foreach ($hid as $key => $value) : ?>
<input type="checkbox" name="hid[]"
value="<?php echo $value->hid; ?>"><?php echo $value->hnm;?>
<?php endforeach; ?>
</td>
</tr>
<tr>
<td width="230"> Enter Meal Preference </td>
<td>
<select name="mid">
<?php foreach ($mid as $key => $value) : ?>
<option value="<?php echo $value->mid ?>"><?php echo $value->mnm; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td width="230">Enter Payment Mode </td>
<td>
<?php foreach ($pid as $key => $value) : ?>
<input type="radio" name="pid"
value="<?php echo $value->pid ?>"><?php echo $value->pnm; ?>
<?php endforeach; ?>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save"/></td>
</tr>
</table>
</form>
</body>
</html>
What I have tried:
I used Implode and Explode because hid is an array.
I can only see either hid or hnm to the view screen after save this data.
I want to store hid to my database and hnm to view screen.
help me with this.