New to codeigniter and trying to get my head around updating checked rows from a user form.
My view generates a form with MySQL data as below:
<?php
echo form_open('masterdata/update_customers');
?>
<table>
<tr>
td> </td><td>Customer Name</td><td>postalcode</td>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>
This works perfectly well as I get my unique name and values for all input fields.
My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.
traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.
my controller function, update_customers at this stage is very basic:
function update_customers()
{
$this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
$this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers");
$this->load->view("master_data/view_master_data_footer");
} else {
$data = array(
'customer_name' => $this->input->post('customer_name'),
'postalcode' => $this->input->post('postalcode'),
);
$this->model_master_data->update_customer_records($data);
$this->customers_updated();
}
}
My model function, update_customer_records is:
function update_customer_records($data)
{
$this->db->where('id',$this->input->post('id'));
$this->db->update('customers',$data);
}
I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is:
name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"
how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?
Any direction or assistance here will be appreciated. thanks a million in advance.
With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.
For example, if you have that in a view:
<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />
From the controller:
public function update_customers() {
$editcustomer = $this->input->post('editcustomer');
}
Return:
Array {
[0] => 1
[1] => 2
[2] => 3
}
Related
How I get my data array from view? and how to insert in my database?
my view, I try much code but still an error, sorry about my question and my bad English
<form role="form" action="<?php echo base_url().'potongan/validasi'?>" action="GET">
<div class="box-body">
<div class="box-header with-border">
<div class="row">
<div class="col-xs-3">
<label >NIM</label>
<input type="text" class="form-control" name="nim" id="nim" placeholder="NIM" value="<?php echo $nime ?>" readonly>
</div>
<div class="col-xs-3">
<label >Nama Mahasiswa</label>
<input type="text" class="form-control" name="namamhs" placeholder="Nama Mahasiswa" value="<?php echo $nim[0]->namamhs ?>" disabled>
</div>
<div class="col-xs-3">
<label >Jurusan</label>
<input type="text" class="form-control" name="jurusan" placeholder="Jurusan" value="<?php echo $nim[0]->jurusan ?>" disabled>
</div>
</div>
<br>
<div class="form-group">
<label >Kode Beasiswa</label>
<input type="text" class="form-control " name="kdbeasiswa" value="<?php echo $kdbeasiswa ?>" readonly>
</div>
<div class="form-group">
<label >Keterangan</label>
<input type="text" class="form-control " name="keterangan" value="<?php echo $keterangan ?>" readonly>
</div>
<div>
<table class="table table-bordered table-striped">
<tr>
<th></th>
<th>Kode</th>
<th>Deskripsi</th>
<th>Tahun Akademik</th>
<th>semester</th>
<th>Tagihan</th>
<th>Beasiswa</th>
<th>Terbayar</th>
<th>Potongan</th>
</tr>
<?php foreach ($nim as $n): ?>
<tr>
<td>
<input type="hidden" class="form-control " name="id[]" value="<?php echo $n->thakad.$n->smtakad.$n->kdkeu; ?>" maxlength="3" >
</td>
<td><input type="hidden" class="form-control " name="kdkeu[]" value="<?php echo $n->kdkeu; ?>" maxlength="3" ><?php echo $n->kdkeu; ?></td>
<td><?php echo $n->deskeu; ?></td>
<td><input type="hidden" class="form-control " name="thakad[]" value="<?php echo $n->thakad; ?>" maxlength="3" ><?php echo $n->thakad; ?></td>
<td><input type="hidden" class="form-control " name="smtakad[]" value="<?php echo $n->smtakad; ?>" maxlength="3" ><?php echo $n->smtakad_view; ?></td>
<td><?php echo $n->tagihan; ?></td>
<td><?php echo $n->beasiswa; ?></td>
<td><?php echo $n->terbayar; ?></td>
<td>
<input type="text" class="form-control " name="prosentase_bea[]" value="<?php echo $n->potongan; ?>" maxlength="3">
</td>
</tr>
<?php endforeach ?>
</table>
</div>
</div>
<br>
<button class="btn btn-info" style="float: right;">submit</button>
</div>
<!-- /.box-body -->
</form>
my controller looks like this when I submit no data insert to the database and error like
Filename: C:/xampp/htdocs/tagihan/system/database/DB_query_builder.php
Line Number: 1498
$data = array();
$count = count($this->input->post('device'));
for($i=0; $i<$count; $i++) {
$data[] = array(
$thakad= $this->input->get('thakad'),
$smtakad= $this->input->get('smtakad'),
$kb = $this->input->get('kdbeasiswa'),
$kk = $this->input->get('kdkeu'),
$nim= $this->input->get('nim'),
$pb = $this->input->get('prosentase_bea'),
$status = 1,
$user = $this->session->userdata('username'),
$kt = $this->input->get('keterangan'),
$curentDate = date("Y-m-d"));
}
$this->db->insert_batch('keu_beasiswapermhs',$data);
redirect('http://localhost/tagihan/potongan');
Make a habit using the proper MVC
VIEW
<form action="URL" method="post">
</form>
CONTROLLER
$this->model->function_name();
redirect('URL');
MODEL
function function_name(){
$dataArr = array(
'field_name'=>$this->input->post('form_input_name'),
);
$this->db->INSERT('tbl_name',$dataArr);
}
You have fields like "namamhs" and "jurusan" that are not being put into the db.
You do not need for or while loops to insert data
You are not assigning what fields in your db each posted item goes to. Your controller has no idea where each value should be inserted. Do not use get, you should use post. Here is an example for your controller you can adapt to your form:
$data = [
'url' => $this->input->post('url'),
'name' => $this->input->post('name'),
'genre' => $this->input->post('genre'),
'counter' => $this->input->post('counter'),
'type' => $this->input->post('type')
];
where 'url' is a field in your db that you are putting $this->input->post('url') into.
Next you check the values:
$this->form_validation->set_rules('url', 'url', 'required|trim');
$this->form_validation->set_rules('name', 'Name', 'required|trim|callback_trims');
$this->form_validation->set_rules('genre', 'Genre', 'required|trim');
$this->form_validation->set_rules('counter', 'Counter', 'numeric');
$this->form_validation->set_rules('type', 'Type', 'required|min_length[3]');
If the values pass the validation, inserting is simple
if( $this->form_validation->run() == FALSE) {
echo validation_errors();
}else
{
$this->db->insert('music', $data);
}
You do not need batch insert. Just use insert
Your $data array is empty...always check if array not empty before insert batch...that cause error DB_query_builder.php Line Number: 1498
I've made page where you can get an overview over invoices.
Everything works great, except from with Internet Explorer...
When I click the see order button, nothing happens in IE.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="window.location.reload()">
<tr><td><?php
$Date = $row['date'];
echo $Date;
?></td><td><?php echo $row['autoid'];?></td>
<input name='implodehideu' type='hidden' value='<?php echo $row['implodeuke'];?>;'>
<input name='implodehidew' type='hidden' value='<?php echo $row['implodehelg'];?>;'>
<input name='personid' type='hidden' value='<?php echo $row['primeid'];?>;'>
<input name='invoiceid' type='hidden' value='<?php echo $row['autoid'];?>'>
<td><input type="submit" name="seeorder" id="submit" value="Order overview"></td>
<td><?php echo $row['firstname'] . " " . $row['surname'];?></td>
<td><?php echo $row['amount'];?></td>
<?php
if ($paidboolean)
echo "<td bgcolor='#3AA849'>YES</td>";
else
echo "<td bgcolor='#e94336'>NO</td>";
if (!$paidboolean) {
?>
<td><input type="submit" name="confirm" id="submit" value="Mark as paid"></td>
<?php
} else {
?>
<td><input type="submit" name="notconfirm" id="submit" value="Mark as not paid"></td>
<?php
}
?>
<td><input type="submit" name="slett" id="submit" value="Slett"></td>
</form>
<?php
if (isset($_POST['seeorder'])) {
.....Things to show up
}
Try to set the id argument of the submit button to something else than 'submit'. That helped me with the same problem.
<input type="submit" name="slett" id="btnSubmit" value="Slett" />
I've been trying to create a joomla form in my view. But when I use the "apply" function and try to retrieve information from the $_POST global I lack two out of three fields. I can't for my life figure out what's wrong.
Since I get one of the fields I assume that I've created the form in a somewhat Joomla fashion.
<form action='<?php echo JRoute::_('index.php?option=com_mycomponent'); ?>' method='post' name="adminForm">
<fieldset><legend><?php echo JText::_('COM_MYCOMPONENT_SETTING_LEGEND'); ?></legend>
<label for=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_USE'); ?>: </label>
<select name=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>' id=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'>
<option value='0'> <?php echo JText::_('COM_MYCOMPONENT_NO')?></option>
<?php
if ($this->m_use_osn)
echo "<option value='1' selected>";
else
echo "<option value='1'>";
?>
<?php echo JText::_('COM_MYCOMPONENT_YES')?></option>
</select>
<br />
<label for='<?php echo JText::_('COM_MYCOMPONENT_URL_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_URL'); ?>: </label>
<?php
if (!empty($this->m_osn_url))
echo "<input type='text' value='".$this->m_osn_url."' /><br />";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_URL_PLACEHOLDER')."' /><br /> ";
?>
<label for='<?php echo JText::_('COM_MYCOMPONENT_KEY_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_KEY'); ?>: </label>
<?php
if (!empty($this -> m_osn_key))
echo "<input type='text' value='" . $this -> m_key . "' /><br /> ";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_KEY_PLACEHOLDER')."' /><br />";
?>
<input type="hidden" name="task" value="" />
<input type='submit' value=' <?php echo JText::_('COM_MYCOMPONENT_SUBMIT'); ?> />
</fieldset>
</form>
However, what a var_dump($_POST) gives me is this:
array(3) {
["mycomponent_use"]=>
string(1) "0"
["task"]=>
string(5) "apply"
["option"]=>
string(14) "com_mycomponent"
}
So, for some reason I'm not getting any of the text input fields. I've tried to get the data through JFactory::getApplication()->input... as well, for some reason it ignore my <input type='text' />. Do I have to register these in the $_POST global in some way? Since I cannot use a regular submit button but must depend on the joomla bar I am getting a bit frustrated with getting a semi empty $_POST.
You have forgot to give the name of input fields in form. Please assign the distinct name for each field.You may refer this.
Dealing with Forms
Codeigniter
Hi. I want to send my form data to a database. The data are checked ok but what to do next?
My controlls
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
My view
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open('form'); ?>
<h5>Wpisz swoje imiÄ™</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />
<h5>Nazwisko</h5>
<?php echo form_error('surname'); ?>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" size="50" />
<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />
<h5>Marka</h5>
<?php echo form_error('brand'); ?>
<input type="text" name="brand" value="<?php echo set_value('brand'); ?>" size="50" />
<h5>Model</h5>
<?php echo form_error('model'); ?>
<input type="text" name="model" value="<?php echo set_value('model'); ?>" size="50" />
<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />
<h5>km</h5>
<?php echo form_error('km'); ?>
<input type="text" name="km" value="<?php echo set_value('km'); ?>" size="50" />
<h5>Rejestracja </h5>
<?php echo form_error('licenceseplate'); ?>
<input type="text" name="licenceseplate" value="<?php echo set_value('licenceseplate'); ?>" size="50" />
<h5>Opis </h5>
<?php echo form_error('description'); ?>
<input type="text" name="description" value="<?php echo set_value('description'); ?>" size="50" />
<h5>Miasto </h5>
<?php echo form_error('city'); ?>
<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
I don't expect the final solution, but I need help what is the next step
My datebase:
After the form validation you can save data
In your controller
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else {
$this->your_model->save($data);
}
your_model.php
public function save($data) {
$this->db->set($data);
$this->db->insert(table name here);
$this->db->insert_id();
}
ok
My view looks like this:
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
/*$this->load->view('formsuccess');*/
$this->load->model ( 'form' );
$this->form->save($data);
}
}
}
?>
my model
class form extends Model{
function form(){
parent::Model();
$this->load->helper('url');
}
public function save($data) {
$this->db->set($data);
$this->db->insert(form);
$this->db->insert_id();
}
}
table in date base name form
I have no idea how you want it
You have to send the data to the model
There you can use active record or normal query to save the data
It must be noted you need to include the model in the controller
$this->load->model ( 'Your model name' ); this should be included in the controller
$this->yourmodelname->functioninmodel this is the controller function call to the model
I have a codeigniter app. My view uses the database row ID to append to the input name to get a unique ID. this allows me to use all inputs in my form action, which is update.
my View syntax:
<?php if(isset($records)) {?>
<table id="hor-minimalist-a">
<tr>
<th> </th><th> </th><th>Customer Name</th><th>postalcode</th>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor('masterdata/confirm_delete_customer/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
</td>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input class="inputwide" type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input class="inputnarrow" type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Checked Customers">
<?php endif; ?>
<?php echo form_close(); ?>
<?php } else {?>
<h4 id="warning"> No Customers currently in database</h4>
<?php } ?>
as you can see the input name's and id's are then unique.
My controller syntax is below:
function manage_customers()
{
$data['title']="Manage Customers";
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records()){
$data['records']=$query;
}
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers",$data);
$this->load->view("master_data/view_master_data_footer");
$editcustomer = $this->input->post('editcustomer');
// single update - working
if( $this->input->post('editcustomer') != false )
{
foreach ($editcustomer as $row_id)
{
$data = array(
'postalcode' => $this->input->post('postalcode_'.$row_id),
'customer_name' => $this->input->post('customer_name_'.$row_id) );
$this->model_master_data->update_customer_records( $row_id, $data );
}
$this->session->set_flashdata('dbaction', 'Selected Records have been updated successfully');
redirect('masterdata/manage_customers', 'refresh');
}
}
How do I make use of the codeigniter validation class to ensure the users modify the input boxes with credible data?
How can the
$this->form_validation->set_rules("primary_contact_tell","Contact Person tell","required|xss_clean|min_length[10]|max_length[14]");
reference the correct dynamic name of the input field? form currently has only customer name and postal code but need to add the rest of the fields.
Thanks in advance, as always.
You can loop through your $records in controller as you are doing it in view to achieve dynamic input validation rules.
foreach($records as $row)
{
$this->form_validation->set_rules("customer_name_" . $row->id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
$this->form_validation->set_rules("postalcode_" . $row->id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
}
Edit:
Think a little. I don't have ability to check what variables in your controller are. As far as I know basing on code you wrote here, this should be working:
foreach($editcustomer as $row_id)
{
$this->form_validation->set_rules("customer_name_" . $row_id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
$this->form_validation->set_rules("postalcode_" . $row_id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
}
Working solution, big thanks to #yabol on this one. I still need to clean up the syntax a little but desired functionality working.
View
<?php
$attributes=array(
'name'=>'updatecustomer',
'id'=>'updatecustomer',
);
echo form_open('masterdata/manage_customers',$attributes);
?>
<div id="validation_failed">
<?php
echo validation_errors();
?>
</div>
<?php if(isset($records)) {?>
<table id="hor-minimalist-a">
<tr>
<th> </th><th> </th><th>Customer Name</th><th>Address Line 1</th><th>Address Line 2</th><th>Suburb</th><th>City</th><th>Postal Code</th><th>Contact Name</th><th>Contact Email</th><th>Contact Tel</th>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor('masterdata/confirm_delete_customer/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
</td>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input class="inputwide" type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="address_line_1_<?php echo $row->id ?>" id="address_line_1_<?php echo $row->id ?>" value="<?php echo $row->address_line_1 ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="address_line_2_<?php echo $row->id ?>" id="address_line_2_<?php echo $row->id ?>" value="<?php echo $row->address_line_2 ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="suburb_<?php echo $row->id ?>" id="suburb_<?php echo $row->id ?>" value="<?php echo $row->suburb ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="city_<?php echo $row->id ?>" id="city_<?php echo $row->id ?>" value="<?php echo $row->city ; ?>" >
</td>
<td>
<input class="inputnarrow" type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_name_<?php echo $row->id ?>" id="primary_contact_name_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_name ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_email_<?php echo $row->id ?>" id="primary_contact_email_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_email ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_tell_<?php echo $row->id ?>" id="primary_contact_tell_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_tell ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table><br>
<input type="submit" value="Update Checked Customers">
<?php endif; ?>
<?php echo form_close(); ?>
Controller
function manage_customers()
{
$data['title']="Manage Customers";
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records()){
$data['records']=$query;
}
$editcustomer = $this->input->post('editcustomer');
if( $this->input->post('editcustomer') != false ){
foreach($editcustomer as $row_id)
{
$this->form_validation->set_rules("customer_name_" . $row_id, "Customer name", "required|min_length[6]");
$this->form_validation->set_rules("address_line_1_". $row_id,"`Address Line 1`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("address_line_2_". $row_id,"`Address Line 2`","xss_clean|min_length[6]");
$this->form_validation->set_rules("suburb_". $row_id,"`Suburb`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("city_". $row_id,"`City`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("postalcode_". $row_id,"`Postal Code`","required|xss_clean|min_length[4]|max_length[5]");
$this->form_validation->set_rules("primary_contact_name_". $row_id,"`Contact Person Name`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("primary_contact_email_". $row_id,"`Contact Person email`","required|valid_email|xss_clean");
$this->form_validation->set_rules("primary_contact_tell_". $row_id,"`Contact Person tell`","required|xss_clean|min_length[10]|max_length[14]");
}
}
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers",$data);
$this->load->view("master_data/view_master_data_footer");
} else {
// single update - working
if( $this->input->post('editcustomer') != false )
{
foreach ($editcustomer as $row_id)
{
$data = array(
'customer_name' => $this->input->post('customer_name_'.$row_id),
'address_line_1' => $this->input->post('address_line_1_'.$row_id),
'address_line_2' => $this->input->post('address_line_2_'.$row_id),
'suburb' => $this->input->post('suburb_'.$row_id),
'city' => $this->input->post('city_'.$row_id),
'postalcode' => $this->input->post('postalcode_'.$row_id),
'primary_contact_name' => $this->input->post('primary_contact_name_'.$row_id),
'primary_contact_email' => $this->input->post('primary_contact_email_'.$row_id),
'primary_contact_tell' => $this->input->post('primary_contact_tell_'.$row_id),
);
$this->model_master_data->update_customer_records( $row_id, $data );
}
$this->session->set_flashdata('dbaction', 'Selected Records have been updated successfully');
redirect('masterdata/manage_customers', 'refresh');
}
}
}