How to update multiple rows in codeigniter cart - php

I am using codeigniter cart to store multiple rows. This is a booking system site.
here's my code to retrieve the cart:
<?php if ($cart = $this->cart->contents()): ?>
<table class="booking">
<tr>
<td>#</td>
<td>Room Type</td>
<td>Check In</td>
<td>Checkout</td>
<td>Nights</td>
<td align="right">Price/Night</td>
<td width="10">Rooms</td>
<td align="right">Amount</td>
<td>Options</td>
</tr>
<?php $grand_total = 0; $i = 0; ?>
<?php foreach ($cart as $item): ?>
<tr bgcolor="#FFFFFF">
<td><?php echo $i+1; ?></td>
<td><?php echo $item['name']; ?></td>
<td><?php echo $item['checkin']; ?></td>
<td><?php echo $item['checkout']; ?></td>
<td align="center"><?php echo $item['nights']; ?></td>
<td align="right"><?php echo number_format($item['subtotal'],2); ?></td>
<td><input type="text" name="booking<?php echo $item['id'] ?>" value="<?php echo $item['qty'] ?>" maxlength="3" size="1" style="text-align: right" /></td>
<?php $amount = $item['subtotal'] * $item['qty']; ?>
<?php $grand_total = $grand_total + $amount; ?>
<td align="right"><?php echo number_format($amount,2) ?></td>
<td><?php echo anchor('bookings/remove/'.$item['rowid'],'Cancel'); ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td colspan="2"><b>Order Total: <?php echo number_format($grand_total,2); ?></b></td>
<td colspan="7" align="right">
<input type="button" value="Clear Cart" onclick="clear_cart()">
<input type="button" value="Update Cart" onclick="update_cart()">
<?php
/*if(isset($_SESSION['sess_user_id']) || (trim($_SESSION['sess_user_id']) <> '')) {
$location='booking_checkout.php';
}else{
$location='billing_info.php';
}*/
?>
<input type="button" value="Place Order" onclick="window.location='<?php //echo $location; ?>'"></td>
</tr>
</table>
<?php endif; ?>
The clear_cart() function works fine with the code below:
<script>
function clear_cart() {
var result = confirm('Are you sure want to clear all bookings?');
if(result) {
window.location = "http://localhost/reservation/bookings/remove/all";
}else{
return false; // cancel button
}
}
</script>
And this is the remove function in my controller:
function remove($rowid) {
if ($rowid=="all"){
$this->cart->destroy();
}else{
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
redirect('bookings');
}
But I don't know how to update the cart if for example I have more than 1 row. The important here is to update the number of rooms (i.e. qty) in the cart.

You can use multi-dimensional array
$data = array(
array(
'rowid' => 'b99c',
'qty' => 3
),
array(
'rowid' => 'xw82',
'qty' => 4
),
array(
'rowid' => 'fh4k',
'qty' => 2
)
);
$this->cart->update($data);

Related

Add Tax to shipping cost on opencart(2.0.3.1) order email (client and admin)

I would like to add the handling fee if the text handling fee exist to the order's shipping weight cost. I would also like to avoid editing any controller. How can I do this with just editing the confirm.tpl from my view?
<tfoot>
<?php foreach ($totals as $total) { ?>
<tr>
<td colspan="4" class="text-right"><strong><?php echo $total['title']; ?>:</strong></td>
<td class="text-right"><?php echo $total['text']; ?></td>
</tr>
<?php } ?>
</tfoot>
Here is what I have done so far:
<tfoot>
<?php foreach ($totals as $total) { ?>
<?php
if(strpos($total['title'], "Handling") !== false) {
// handling exists, what now?
}
?>
<tr>
<td colspan="4" class="text-right"><strong><?php echo $total['title']; ?>:</strong></td>
<td class="text-right"><?php echo $total['text']; ?></td>
</tr>
<?php } ?>
</tfoot>
<?php
$totals = array(
array('title' => 'Handling Fee', 'text' => '₱171.33'),
array('title' => 'Sub-Total', 'text' => '₱17,132.85'),
array('title' => 'Nationwide Fee', 'text' => '₱280.00')
);
?>
<tfoot>
<?php $toAdd = 0; foreach ($totals as $total) { ?>
<?php
if(strpos($total['title'], "Handling") !== false) {
$toAdd = (float)preg_replace('/[^.a-zA-Z0-9]/s', '', $total['text']);
} else { ?>
<tr>
<td colspan="4" class="text-right"><strong><?php echo $total['title']; ?>:</strong></td>
<td class="text-right"><?php
if($toAdd && (strpos($total['title'], 'Nationwide') !== false)) {
$nationwide = (float)preg_replace('/[^.a-zA-Z0-9]/s', '', $total['text']);
echo "₱". number_format($nationwide + $toAdd, 2, '.', ',');
} else {
echo $total['text'];
}
?></td>
</tr>
<?php } ?>
<?php } ?>
</tfoot>
Here is one dry and dirty answer. If I can improve this, please let me now. It uses strpos to hide the handling fee and preg_replace casted as float in order for me to perform some basic arithmetic operations.

How to sort MongoDB record using PHP?

I was trying to create a simple web application using MongoDB and PHP. I create a table and successfully inserted data into it. Now I am trying to sort data in asc/desc and alphabetically. But here it is sorting only top 2 records. I tried many methods but all in vain. Please help me!! Here is my code
<form method="post">
<table style="width:180px" border="solid" overflow="hidden" >
<tr>
<th>_id</th>
<th>Author</th>
<th>Subject</th>
<th><button type="submit" name="notes_sort">sort</button>Title</th>
<th>Notes</th>
<th> <button type="submit" name="diff_ascend">Asc</button><button type="submit"
name="diff_descend">Desc</button> Difficulty</th>
<th>Related Topics</th>
<th>Tags</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
date_default_timezone_set("Asia/Kolkata");
$current_date = date('d/m/Y');
$options = array(
"sort" => array("_id" => -1),
);
$data = $collection->find(array('date' => $current_date),$options);
if(isset($_POST['diff_ascend'])){
$options = array(
"sort" => array("difficulty" => 1),
);
$data = $collection->find(array('date' => $current_date),$options);
}
if(isset($_POST['diff_descend'])){
$options = array(
"sort" => array("difficulty" => -1),
);
$data = $collection->find(array('date' => $current_date),$options);
}
if(isset($_POST['notes_sort'])){
$options = array(
"sort" => array("notes" => -1),
);
$data = $collection->find(array('date' => $current_date),$options);
}
foreach($data as $document){
?>
<tr>
<td><?php echo $document['_id'] ?></td>
<td><?php echo $document['author'] ?></td>
<td><?php echo $document["subject"] ?></td>
<td><?php echo $document["title"] ?></td>
<td><?php echo $document["notes"] ?></td>
<td><?php echo $document["difficulty"] ?></td>
<td><?php echo $document["rel_topic"] ?></td>
<td><?php echo $document["tags"]["tag4"].", ".$document["tags"]["tag3"].",
".$document["tags"]["tag2"].", ".$document["tags"]["tag1"]?></td>
<td><?php echo $document["date"] ?></td>
<td><a href='delete.php?id=<?php echo $document["_id"];?>' >Delete</a> <a
href='update.php?id=<?php echo $document["_id"];?>' name='update'>
Update</a></td>
</tr>
</table>
</form>

Values from textbox array not updating

I'm new in this forum and I'm having a hard time updating selected value from checkbox coming from the textbox value.
Scenario:
I want to update selected items, For example, I want to update Item-2 and the textbox from item received will be enabled, I will enter a number for example 1 and the textbox from total receive will automatically sum using AJAX.
The problem is after submitting the value from total receive the records that were updating is blank, but when I try to check and print_r the value is there.
And one more thing if all checkboxes are checked and enter a number for each item received, the value that it only get is the last value and will be updated will all selected checkbox.
Notes:
The checkbox is an array
Textbox from total receive is an array
Here's my UI:
Controller:
public function recitem_insert(){
$this->load->model('dbquery');
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
if(isset($check)){ //Check if check is checked
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
echo "<script type='text/javascript'>
alert('Successfully Added!');
window.close();
</script>";
}else{ //End If
echo 'Please select a checkbox';
}
}
View:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Model:
public function modUpdatedynamicval($table, $column, $data, $equal_to){
$this->db->where($column, $equal_to);
$this->db->update($table, $data);
}
Edit
Let's assume:
I've input 1 in item_receive textbox one and the total receive will be 10,
2 in item_receive textbox two and the total receive will be 11,
3 in item_receive textbox three and the total receive will be 12,
CODE:
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
echo 'Check Value';
print_r($check);
echo '<br><br>';
echo 'Total Recieve';
print_r($total_rec);
OUTPUT:
Check ValueArray ( [0] => 1 [1] => 2 [2] => 3 )
Total RecieveArray ( [0] => 10 [1] => 11 [2] => 12 )
But If I only input the second textbox here's the output:
Check ValueArray ( [0] => 2 )
Total RecieveArray ( [0] => [1] => 11 [2] => )
From what I see, you are having a problem to determine which input index is which on the back end. You could set an index key on each of the input like this :
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $key => $row){ //added key to be used as inputs key
?>
<tr>
<td><input type="checkbox" name="check[<?php echo $key ?>]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $key ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $key ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $key ?>]"></td>
</tr>
<?php
}
?>
</tbody>
After that, then replace the for loop :
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
With this foreach :
foreach ($check as $key => $item) {
$updateData = array('rec_qty' => $total_rec[$key] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$key]);
}
So you don't manually check for the total_rec value but instead it only checks on submitted check post data.
from what i can see, like everybody else is saying, you have trouble in finding the correct rows of checked item.
I suggest you edit the way you name the form in multidimensional array. bet you didnt know that?
so in view.php
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//WTF?? it's 2019. Say hello to SQL Injection
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $i=>$row){
?>
<tr>
<td><input type="checkbox" name="data[<?php echo $i;?>][check]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="data[<?php echo $i;?>][last_item_rec]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="data[<?php echo $i;?>][item_rec]" id="txt" disabled=""></td>
<td><input type="text" name="data[<?php echo $i;?>][total_rec]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
and you can easily cycle through the data for each rows while at the same time validating if the checkbox is checked
<?php
//simple way of traversing the data
if ( isset( $_POST['data'] ) )
{
echo '<table>';
foreach ( $_POST['data'] as $d )
{
//you have all the data here
//start checking on checked box
if(isset($d['check']))
{
echo '<tr>';
echo ' <td>', $d['last_item_rec'], '</td>';
echo ' <td>', $d['item_rec'], '</td>';
echo ' <td>', $d['total_rec'], '</td>';
echo '</tr>';
}
}
echo '</table>';
}
Can try with row key (item id). Update your array key with $row->id.
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $row->id; ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $row->id; ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $row->id; ?>]"></td>
</tr>
<?php
}
?>
</tbody>
Scenraio Like:
1 item Id "2"
2 item Id "3"
3 item Id "5"
If you check first st row and first row item_id ($row->id) is 2 and enter 7 than you will get array like :-
print_r($_POST['total_rec']);
Output Like:
Array([2]=>7)
You can easy to find data using array key
Update:
<form method="post" action="<?php echo base_url(); ?>">
<div class="box">
<div class="box-header">
<h3 class="box-title">
System ID: <b> <?php echo '12'; ?></b>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th> Action</th>
<th>Item Code </th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
$new_item_list = array(
array('id'=> '1','item_code'=> 'ITEM-1','description' => 'ITEM Desc', 'qty_order'=> '2','rec_qty'=> '2'),
array('id'=> '2','item_code'=> 'ITEM-2','description' => 'ITEM Desc 2', 'qty_order'=> '3','rec_qty'=> '3' ),
array('id'=> '3','item_code'=> 'ITEM-3','description' => 'ITEM Desc', 'qty_order'=> '5','rec_qty'=> '4' ),
array('id'=> '4','item_code'=> 'ITEM-4','description' => 'ITEM Desc', 'qty_order'=> '1','rec_qty'=> '3' )
);
foreach ($new_item_list as $row){
?>
<tr>
<td><input type="checkbox" name="<?php echo $row['id']; ?>[check]" id="opt" value="<?php echo $row['id']; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['qty_order']; ?></td>
<td><?php echo $row['rec_qty']; ?></td>
<input type="hidden" name="<?php echo $row['id']; ?>[last_item_rec]" value="<?php echo $row['rec_qty']; ?>">
<td><input type="text" name="<?php echo $row['id']; ?>[item_rec]" id="txt" value='' ></td>
<td><input type="text" name="<?php echo $row['id']; ?>[total_rec]" value=''>
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Submit Action: For example
foreach($_POST as $key => $value){
if(isset($value['check'])){
//Row ID of item table $key
$total = $value['total_rec'] * $value['item_rec'];
echo "Total Number :".$total."</br>";
#here is your update query
}
}
Link:- https://prnt.sc/mswdoc
If check checkbook and submit from then you will get output like
Output:- https://prnt.sc/mswefv
Your checkboxes all have the same ID, which always leads to problems. Change the id of the checkbox in the loop.
<td><input type="checkbox" name="check[]" id="opt-<?php echo $row->id; ?>" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>

Can't delete without getting "Undefined Offset" error

I'm doing a very simple project for my assignment and it's about shopping cart. I've created a cart page by using SESSION with array and index to keep track user's order. The problem is whenever I'm deleting a meal from my cart list, there'll be an error which is "Notice: Undefined offset: 1", "Notice: Undefined offset: 2", etc. I'm attaching my codes here:
This is my cart page. The error said it has something to do on the line that I have commented:
<form method="POST" action="save_cart.php">
<table class="table">
<tbody>
<thead>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</thead>
<?php
//initialize total
$total = 0;
if(!empty($_SESSION['cart'])){
//create array of initail qty which is 1
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php echo number_format($_SESSION['qty_array'][$index]*$row['meal_price'], 2); ?></td>
<?php $total += $_SESSION['qty_array'][$index]*$row['meal_price']; ?>
</tr>
<?php
$index ++;
}
}
else{
?>
<tr>
<td colspan="5" class="text-center" style="text-align: center;">No Meal in Cart</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="left"><b>Total</b></td>
<td><b>RM <?php echo number_format($total, 2); ?></b></td>
</tr>
</tr>
Here's my delete page:
session_start();
//remove the id from our cart array
$key = array_search($_GET['meal_id'], $_SESSION['cart']);
unset($_SESSION['cart'][$key]);
unset($_SESSION['qty_array'][$_GET['index']]);
//rearrange array after unset
$_SESSION['qty_array'] = array_values($_SESSION['qty_array']);
$_SESSION['message'] = "<script>alert('Meal is deleted from your cart');</script>";
header('location: cart.php');
I really need your help. Thanks in advance!
Its an index error because index doesn't exists in the array replace your html php code inside while loop with this one.
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0); ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php
$qty = (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0);
echo number_format($qty *$row['meal_price'], 2); ?></td>
<?php $total += $qty*$row['meal_price']; ?>
</tr>

How to save dynamic generated table data to mysql database in php codeigniter

I had Created a Web App in PHP Code igniter which generates table containing calculation of employees earning, deduction.
Since I am newbie to CI. I am trying to save the dynamic generated table data to database, but failed.
I am not getting that how to POST table data to Controller.
Any help is welcome.
This is My Controller To generate dynamic table
function index()
{
if($this->input->post("submit"))
{
$vals=$this->cpanel_model->esic();
if($vals){
$data['w_share']=$vals->w_share;
$data['e_share']=$vals->e_share;
}
$data["wshare"]="";
//echo $data['w_share'];
$city=$_POST["city"];
$v1= $data['w_share'];
$v2= '100';
$v3= $data['e_share'];
$arr=$_POST["wid"];
$arr1=$_POST["days"];
$arr2=$_POST["payment"];
$arr3=$_POST["wdays"];
for ($i=0; $i <count($arr1) ; $i++)
{
$net[$i]=($arr2[$i]/$arr3[$i])*$arr1[$i];
$wshare[$i]=($net[$i]/$v2)*$v1;
$eshare[$i]=($net[$i]/$v2)*$v3;
$total[$i]=$eshare[$i]+$wshare[$i];
$wdays[$i]=$arr3;
$days[$i]=$arr1;
}
$data["net"]=$net;
$data["wshare"]=$wshare;
$data["total"]=$total;
$data["eshare"]=$eshare;
$data["wdays"]=$arr3;
$data["days"]=$arr1;
$data["list"]=$this->payment_model->search($city);
$this->load->view("secure/php-version/esic",$data);
}
This Is My View to echo table values
<?php echo form_open('esic/insert',array("role"=>"form")); ?>
<div class="table-responsive">
<table border="1" id="t01" class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>Worker_id</th>
<th>Worker Name</th>
<th>Worker City</th>
<th>Designation</th>
<th>Working Days</th>
<th>Basic Payment</th>
<th>Net Payment</th>
<th>Present Days</th>
<th>Worker Share (1.75%)</th>
<th>Employee Share (4.75%)</th>
<th>Total</th>
</tr>
<?php $i=0; $sum2=0; ?>
<?php if(count($list)>0) { foreach ($list as $std) {?>
<tr>
<?php echo form_hidden("wid[]",$std["wid"]); ?>
<?php echo form_hidden("wdays[]",$wdays); ?>
<?php echo form_hidden("payment[]",$std["payment"]); ?>
<td><?php echo $std["wid"]; ?></td>
<td><?php echo $std["wname"]; ?></td>
<td><?php echo $std["city"]; ?></td>
<td><?php echo $std["designation"]; ?></td>
<td><?php echo $wdays[$i]; ?></td>
<td><?php echo $std["payment"]; ?></td>
<td><?php echo $net[$i]; ?></td>
<td><?php echo $days[$i]; ?></td>
<td><?php echo $wshare[$i]; ?></td>
<td><?php echo $eshare[$i]; ?></td>
<td><?php echo $total[$i]; ?></td>
<?php $sum2 += $std["payment"]; ?>
</tr>
<?php $i++;}}?>
<tr>
<?php
$sum=0;
$sum1=0;
$sum3=0;
$sum4=0;
$sum5=0;
$i=0;
foreach ($total as $value) {
$sum = $sum + $value;
$i++;
}
foreach ($days as $value) {
$sum1 = $sum1 + $value;
$i++;
}
foreach ($eshare as $value) {
$sum3 = $sum3 + $value;
$i++;
}
foreach ($wshare as $value) {
$sum4 = $sum4 + $value;
$i++;
}
foreach ($net as $value) {
$sum5 = $sum5 + $value;
$i++;
}
?>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum2; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum5; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum1; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum4; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum3; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum; ?>
</td>
</tr>
</table>
</div>
<?php echo form_submit(array("name"=>"submit","type"=>"submit","value"=>"Create ESIC")); ?>
<?php echo form_close(); ?>
Its not too clear on how you are currently generating your table, but regardless, when you generate your table you have to place it in a form, and assign keys to inputs. Try this approach:
HTML (these are the results you should get when you generate your table)
<form action="<?= base_url() ?>table_data" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Earnings</th>
<th>Deduction</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="table[0][name]" value="name1">name1</td>
<td><input type="hidden" name="table[0][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[0][deduc]" value="deduc1">deduc1</td>
</tr>
<tr>
<td><input type="hidden" name="table[1][name]" value="name1">name1</td>
<td><input type="hidden" name="table[1][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[1][deduc]" value="deduc1">deduc1</td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit this table">
</form>
Controller:
function table_data() {
if ($this->input->post()) {
$table_data = $this->input->post('table');
$this->load->model('model_name');
$this->model_name->insert_table_data($table_data);
}
}
Model:
function insert_table_data($table_data){
$this->db->insert_batch('mytable', $table_data);
}

Categories