Batch Process not inserted to the table - php

I have following tables that used to store purchase & issue of items.
store_update_stock Table
+-----------------+----------+-------------------+-----------------+--------+
| update_stock_id | supplier | user order_status | transfer_status | status |
+-----------------+----------+-------------------+-----------------+--------+
store_update_stock_details Table
+-------------------------+-----------------+------+-----+------------+--------+
| update+stock_details_id | update_stock_id | item | qty | unit_price | status |
+-------------------------+-----------------+------+-----+------------+--------+
02) I used Codeigniter for the project and files as follows :
Controller
public function verifyItemReqFromHD()
{
$this->checkPermissions('edit', 'issueApprovedItem');
$bc = array(array('link' => '#', 'page' => 'Item Request From HD'));
$meta = array('page_title' => 'Item Request From HD', 'bc' => $bc);
$this->data['products'] = $this->Item_model->getProducts();
if ($this->form_validation->run() == true) {
$count = count($this->input->post('item'));
$items = $this->input->post('item');
$qts = $this->input->post('qty');
$up = $this->input->post('unit_price');
$total = 0;
for ($x = 0; $x < $count; $x++) {
$details[$x]['update_stock_id'] = null;
$details[$x]['item'] = $items[$x];
$details[$x]['qty'] = $qts[$x];
$details[$x]['unit_price'] = $up[$x];
$details[$x]['status'] = 1;
}
$stock = array(
'supplier' => $this->session->userdata('id_user'),
'user' => ucfirst($this->session->userdata('name')),
'order_status' => 'verifyIssue',
'transfer_status' => 'Verified',
'status' => '1'
);
if ($this->Item_model->addItemReqFromHD($stock, $details)) {
$this->session->set_flashdata('message', 'Successfully Sent Your Request..!!');
redirect('item/verifyItemReqFromHD');
}
} else {
$this->session->set_flashdata('error', validation_errors());
$this->render('item/viewItemtoIssued', $meta, $this->data);
}
}
Model
function addItemReqFromHD($data,$details)
{
$this->db->trans_start();
if ($this->db->insert('store_update_stock', $data)) {
$id = $this->db->insert_id();
foreach ($details as $detail) {
$detail['update_stock_id'] = $id;
$this->db->insert('store_update_stock_details', $detail);
}
}
$this->db->trans_complete();
if ($this->db->trans_status() === true) {
return true;
}
return false;
}
View
<?php
if(!empty($issueData)){
$common=$issueData[0];
}
?>
<script type="text/javascript">
$(document).on("change", "#item", function () {
$.ajax({
'url': '<?=site_url("item/isExistProduct/?q=")?>' + $('#item').val(),
'method': 'GET',
'success': function (data) {
var jData = JSON.parse(data);
if (jData.status == true) {
jData.data.forEach(data => {
$('#request_table').append('<tr>' +
'<td ><span id="product" >' + data.item_name + '</span>' +
'<input type="hidden" id="item[]" name="item[]" value="' + data.item_id + '">' +
'</td>' +
'<td class="text-center">' + data.qty + '</td>' +
'<td class="text-center"><input class="form-control text-right" disabled id="sales_price[]" name="sales_price[]" value="' + data.up+ '"></td>' +
'<td class="text-center"><input class="form-control text-center rquantity" data-qty-bal="' + data.qty + '" autofocus required type="number" step="any" id="qty[]" name="qty[]" ></td>' +
'<td class="text-center" ><i class="fa fa-remove remove" style="cursor: pointer"></i></td>' +
'</tr>');
})
}
},
'error': function () {
}
});
});
$(document).on("click", ".remove", function () {
$(this).closest('tr').remove();
});
var old_row_qty;
$(document).on("focus", '.rquantity', function () {
old_row_qty = $(this).val();
}).on("change", '.rquantity', function () {
var row = $(this).closest('tr');
var issue_q = parseFloat($(this).val());
var available_q = parseFloat($(this).data('qty-bal'));
if (issue_q > available_q) {
console.log("ssss");
$(row).addClass('danger');
$('#add_sale').attr('disabled', true);
alert("Can not proceed your request ..!!. Issue quantity is higher than the available quantity..");
} else {
console.log("remove");
$(row).removeClass('danger');
$('#add_sale').attr('disabled', false);
}
});
</script>
<!-- Main content -->
<section class="invoice">
<!-- title row -->
<div class="row">
<div class="col-xs-12">
</div>
<!-- /.col -->
</div>
<!-- Table row -->
<div class="row" style="margin-top: 2%">
<div class="col-xs-12 table-responsive">
<table class="table table-striped">
<thead>
<tr class="" style="background-color: #33ff99 !important;">
<th>Item</th>
<th class="text-right">Requested Qty</th>
<th class="text-right">Approved Qty</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($issueData)) {
foreach ($issueData as $rows){
?>
<tr>
<td style="width: 40%"><?=$rows->item_name?></td>
<td style="width: 15%" class="text-right"><?=$rows->r_qty+0?></td>
<td style="width: 15%" class="text-right"><?=$rows->ap_qty+0?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<!-- /.col -->
</div>
<!-- /.row -->
<form action="<?= site_url('item/verifyItemReqFromHD') ?>" method="post">
<div class="col-md-5">
<div class="form-group"><label>Select Item</label>
<select name="item" id="item" class="form-control select2" required>
<option value="">Select Item</option>
<?php
if (!empty($products)) {
foreach ($products as $row) {
?>
<option value="<?= $row->item_id ?>"><?= $row->item_name ?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="col-md-12 column">
<div class="col-md-12">
<div class="control-group table-group">
<label class="table-label">Issue Items *</label>
<div class="controls table-controls">
<table id="request_table"
class="table items table-striped table-bordered table-condensed table-hover">
<thead>
<tr class="" style="background-color: #ff66a3 !important;">
<th class="col-md-5">Item Name</th>
<th class="text-center col-md-2">Available Qty</th>
<th class="text-center col-md-2">Unit Price</th>
<th class="text-center col-md-2">Issuing Qty</th>
</th>
<th class="col-md-2" style="width: 30px !important; text-align: center;">
<i class="fa fa-trash-o" style="opacity:0.5; filter:alpha(opacity=50);"></i>
</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr id="tfoot" class="tfoot active">
<th colspan="2">Total</th>
<th class="text-right"></th>
<th class="text-center">0</th>
<th class="text-center"><i class="fa fa-trash-o"
style="opacity:0.5; filter:alpha(opacity=50);"></i>
</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4">
<button type="submit" id="add_sale" class="btn btn-primary btn-block">Issue</button>
</div>
</div>
</form>
</section>
Desired Output
03) Then I need to insert data to the both tables using above codes.
When press "Issue" button in the view the relevant values didn't insert to the tables. What can be going wrong ? Can anyone help me ?

Try
$this->db->trans_commit()

I added the following validation to the controller.
$this->form_validation->set_rules('item', 'Item', 'required');
And also changed the following line in my view.
'<input type="hidden" id="item_id[]" name="item_id[]" value="' + data.item_id + '">' +
Solved the problem .....

Related

How do I stop user from leaving quantity empty?

So this is my order page and I want to add an option which prevents the user from leaving the quantity option empty. I cannot seem to use the required option as it compels the user to fill every box instead of the one just selected.
Here is the code
<body>
<?php include('navbar.php'); ?>
<div class="container">
<h1 class="page-header text-center">ORDER</h1>
<form method="POST" action="purchase.php">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<?php
$sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
$query = $conn->query($sql);
$iterate = 0;
while ($row = $query->fetch_array()) {
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
<td><?php echo $row['catname']; ?></td>
<td><a href="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>"><img src="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>" height="170px" width="80%"></a></td>
<td class="productname1"><?php echo $row['productname']; ?></td>
<td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
<!-->**HERE IS THE CODE THAT NEEDS TO BE FIXED**--> <td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-3">
<input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
</div>
<div class="col-md-3">
<input type="number" name="number" class="form-control" placeholder="Contact Number" required>
</div>
<div class="col-md-2" style="margin-left:-20px;">
<button type="submit" onclick="myFunction()" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
<br />
<br />
<br />
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#checkAll").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
});
});
</script>
</body>
</html>
You can target those fields with jQuery/JavaScript and make it required by focusing on it and then prevent the form from submitting. Try this
$(document).ready(function() {
$('#order-form').submit(function(e){
let $quantities = $(this).find('.table input[type="number"]').filter(function(){
return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
})
if( $quantities.length > 0 ){
e.preventDefault();
$quantities.first().focus()
}
})
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="container">
<form method="POST" action="purchase.php" id="order-form">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<tr>
<td class="text-center"><input type="checkbox" value="1" name="productid[]" checked></td>
<td>Dishes</td>
<td><img src="https://via.placeholder.com/150/0000FF/FFFFFF?text=Bara" height="170px" width="80%"></td>
<td class="productname1">Bara</td>
<td class="price">Rs 79.00</td>
<td><input type="number" class="form-control" name="quantity[]"></td>
</tr>
<tr>
<td class="text-center"><input type="checkbox" value="1" name="productid[]"></td>
<td>Dishes</td>
<td><img src="https://via.placeholder.com/150/FF0000/FFFFFF?text=Chwela" height="170px" width="80%"></td>
<td class="productname1">Chwela</td>
<td class="price">Rs 120.00</td>
<td><input type="number" class="form-control" name="quantity[]"></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
</form>
</div>
Answer in format requested by the OP
<body>
<?php include('navbar.php'); ?>
<div class="container">
<h1 class="page-header text-center">ORDER</h1>
<form method="POST" action="purchase.php" id="order-form">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<?php
$sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
$query = $conn->query($sql);
$iterate = 0;
while ($row = $query->fetch_array()) {
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
<td><?php echo $row['catname']; ?></td>
<td><a href="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>"><img src="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>" height="170px" width="80%"></a></td>
<td class="productname1"><?php echo $row['productname']; ?></td>
<td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
<td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-3">
<input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
</div>
<div class="col-md-3">
<input type="number" name="number" class="form-control" placeholder="Contact Number" required>
</div>
<div class="col-md-2" style="margin-left:-20px;">
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
<br />
<br />
<br />
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#checkAll").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
});
$('#order-form').submit(function(e){
let $quantities = $(this).find('.table input[type="number"]').filter(function(){
return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
})
if( $quantities.length > 0 ){
e.preventDefault();
alert("Quantity is required")
$quantities.first().focus()
}
})
});
</script>
</body>
In case of users who have disabled Javascript, you should also check on the server for missing quantities:
// purchase.php
if (isset($_POST['productid'])) {
for ( $i=0; $i < sizeof($_POST['productid']); $i++ ) {
if ( $_POST['productid'][$i] && ! $_POST['quantity'][$i] ) {
echo "Missing quantity for " . $_POST['productid'][$i] . "<br>";
}
}
}

How to insert selected value from select option using checkbox in PHP?

how to insert select option value in database using checkbox?When I click checkbox and click enroll button ,select option doesn't inserting selected value.I have 3 options(Regular,Retake,Recourse) in "examtype" table but when I choose more than one subject select option not working perfectly. Here is my code...
here is my select option image
<?php
if (isset($_POST['enroll']))
{
if (!empty($_POST['chk1']))
{
if (isset($_POST['et']))
{
$roll_no = $_SESSION['roll_no'];
//$selectbox1 = $_POST['et'];
foreach($_POST['chk1'] as $checkbox1)
{
foreach($_POST['et'] as $selectbox1)
//for($i=0;$i<sizeof($selectbox1);$i++)
$values = explode("|" , $checkbox1);
$values1= explode("|" , $selectbox1);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];
$selectbox1=$values1[0];
$sql="INSERT INTO pendingcourse(roll_no,course_id,semester,course_name,exam_type,status) VALUES('$roll_no','$course_id','$semester','$course_name','$selectbox1',0)";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$selectbox1='';
}
header("location:coursetable.php");
}
}
}
?>
<form action="coursetable.php" method="post" enctype="multipart/form-data">
<div class="container mt-3" >
<div class="accordion mb-4" id="accordionExample" >
<div class="accordion-item" style="background-color: rgb(0, 225, 255);">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button collapsed fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne" style="background: #AAFFA9;">
1st Semester
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" >
<div class="accordion-body">
<table class="table table-bordered border-primary text-center">
<thead>
<tr>
<th scope="col-1">Course Id</th>
<th scope="col-1">Semester</th>
<th scope="col-7">Course</th>
<th scope="col-1">Exam Type</th>
<th scope="col-1">Select</th>
<th scope="col-2">Result</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $connect->query("SELECT course_id,semester,course_name FROM coursetable Where semester = '1st' ");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
$course_id = $row['course_id'];
$semester = $row['semester'];
$course_name = $row['course_name'];
?>
<tr>
<td scope="row"> <?php echo $course_id?></td>
<td > <?php echo $semester ?></td>
<td ><?php echo $course_name ?></td>
<td>
<select name="et[]" class="form-select form-select-sm" aria-label=".form-select-sm example">
<?php
$stmt1 = $connect->query("SELECT * from examtype");
$rows1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
foreach($rows1 as $row1) {
?>
<option value="<?php echo $row1['exam_type']; ?>"><?php echo $row1['exam_type']; ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="checkbox" name="chk1[]" value="<?php echo $row['course_id']?>|<?php echo $row['semester']?>|<?php echo $row['course_name']?>|<?php echo $row1['exam_type']?>">
<label class="form-check-label" for="flexCheckDefault">
</label>
</td>
<td></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-2 col-xs-6 offset-md-5 mb-3">
<button type="submit" name="enroll" class="btn btn-warning mt-2">Enroll</button>
</div>
</div>
</div>
</form>
Place the input field inside a form and add
enctype="multipart/form-data"
<select name="et[]" class="form-select form-select-sm" aria-label=".form-select-sm example" multiple>
try this
add multiple in select
SOLVED:
<?php
if (isset($_POST['enroll']))
{
if (!empty($_POST['chk1']))
{
if (!empty($_POST['et']))
{
$roll_no = $_SESSION['roll_no'];
$selectbox1 = $_POST['et'];
$selectbox11=implode(',',array_filter($selectbox1));
$i=0;
foreach($_POST['chk1'] as $checkbox1)
{
$selectbox111 = array_diff(explode(",", $selectbox11),array(""));
$selectbox1111 = $selectbox111[$i];
$values = explode("|" , $checkbox1);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];
$sql="INSERT INTO pendingcourse(roll_no,course_id,semester,course_name,exam_type,status) VALUES('$roll_no','$course_id','$semester','$course_name','$selectbox1111',0)";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$i++;
}
}
}
}
?>

How to show image using storage folder for looping data

My image is not displaying. I'm using Storage:: to save my image. Here how my folder looks like and php artisan storage:link has been created. I already try this code {{ asset('storage/complaint' . $c->image)}},{{ storage_path() . '/complaint' .$c->image}}and {{ url('/public/complaint' . $c->image) }} but it seems to be not working at all.
web.php
Route::get('/report-form','ComplaintController#create');
Route::post('/report-create','ComplaintController#store');
Route::get('/report-view','ComplaintController#index');
ComplaintController.php
<?php
namespace App\Http\Controllers;
use Auth;
use Validator;
use Response;
use Carbon\Carbon;
use App\Complaint;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
class ComplaintController extends Controller
{
public function index(Request $request)
{
if (Auth::user()->role == 'buyer')
{
$complaint = Complaint::where('report_by',Auth::user()->id)->get();
return view('buyers.complaints.index',compact('complaint'));
}
}
public function create(Request $request)
{
return view('buyers.complaints.create');
}
public function store(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
if (isset($request->image[$item])) {
$images = $request->file('image');
$image_resize = Image::make($images[$item]->getRealPath());
$image_resize->resize(900, 630);
$filename = $images[$item]->getClientOriginalName();
Storage::put($filename, $image_resize);
Storage::move($filename, 'public/complaint/' . $filename);
}
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString()
);
Complaint::insert($data);
}
}
return redirect('/report-form')->with('success','Your report is submitted!');
}
create.blade.php
<div class="panel">
<form action="/report-create" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<table class="table table-bordered">
<thead>
<tr>
<th><center>Type of Defect</center></th>
<th><center>Image</center></th>
<th><center>Description</center></th>
<th><center>Action</center></th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%">
<select class="form-control" name="defect_id[]">
<option value="" selected>Choose Defect</option>
#foreach(App\Defect::all() as $defect)
<option value="{{$defect->id}}">{{$defect->name}}</option>
#endforeach
</td>
<td width="15%">
<input type="file" class="form-control-file" name="image[]">
</td>
<td width="45%">
<input type="text" class="form-control" name="description[]">
</td>
<td width="10%">
<button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
</td>
</tr>
</tbody>
</table>
<center><button type="submit" class="btn btn-primary">Submit</button></center>
<br>
</form>
</div>
#section('footer')
<script>
$(document).ready(function () {
$('#add-btn').on('click',function () {
var html = '';
html += '<tr>';
html += '<td><select class="form-control" name="defect_id[]"><option value="" selected>Choose Defect</option>#foreach(App\Defect::all() as $defect)<option value="{{$defect->id}}">{{$defect->name}}</option>#endforeach</td>';
html += '<td><input type="file" class="form-control-file" name="image[]"></td>';
html += '<td><input type="text" class="form-control" name="description[]"></td>';
html += '<td><button type="button" class="btn btn-danger btn-sm" id="remove-btn"><i class="glyphicon glyphicon-minus"></i></button></td>';
html += '</tr>';
$('tbody').append(html);
})
});
$(document).on('click','#remove-btn',function () {
$(this).closest('tr').remove();
});
</script>
#stop
index.blade.php
<div class="panel-heading">
<h3 class="panel-title"><strong>List of Report</strong></h3>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Defect Name</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
#foreach($complaint as $c)
<tr class="">
<td>{{$c->defect->name}}</td>
<td>{{$c->description}}</td>
<td><img src="{{ asset('storage/complaint' . $c->image)}}" class="" alt="{{$c->image}}"></td>
</tr>
#endforeach
</table>
</div>
What is my mistakes?
use Storage::url() ref link https://laravel.com/docs/8.x/filesystem#file-urls
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
NOTE: make sure APP_URL=http://exmaplet.test this is set

How to submit multiple rows tabular form data in database using code igniter?

Hello i am creating a school management system and facing a little problem.
The problem is that i am having a complex form containing following fields in a table
I have a table fee structure in which i want to insert data.
having columns
id | classID | sectionID | feetypeID | must | amount | active
1 1 1 1 must 500 yes
2 1 1 2 optional 500 no
so how to create an array such that it uses above form to insert data in table.
Basically i want users to choose feetype they want to have for classes and set their amount and then choose classes they want to apply it for so an entry is created in database for each section and each feetype they select.
I don't know how to make controller and model for this but i have this code of view
View
<form class="form-horizontal" role="form" method="post">
<div class="box-body">
<div class="row">
<div class="col-sm-12">
<h3>Fee Types</h3><br>
<div id="hide-table">
<table id="example1" class="table table-striped table-bordered table-hover dataTable no-footer">
<thead>
<tr>
<th class="col-sm-2"><?=$this->lang->line('slno')?></th>
<th class="col-sm-2"><?=$this->lang->line('feetype')?></th>
<th class="col-sm-2"><?=$this->lang->line('amount')?></th>
<th class="col-sm-2"><?=$this->lang->line('must')?></th>
<th class="col-sm-2"><?=$this->lang->line('active')?></th>
<th class="col-sm-2"><input type="checkbox"> Select All</th>
</tr>
</thead>
<tbody>
<?php if(count($feetypes)) {$i = 1; foreach($feetypes as $feetype) { ?>
<tr>
<td data-title="<?=$this->lang->line('slno')?>">
<?php echo $i; ?>
</td>
<td data-title="<?=$this->lang->line('feetype_name')?>" name="feetypeID" value="<?php echo $feetype->feetypeID;?>">
<?php echo $feetype->feetype; ?>
</td>
<td data-title="<?=$this->lang->line('amount')?>" name="amount">
<input type="text" class="form-control" value="<?php echo $feetype->defaultamount; ?>">
</td>
<td data-title="<?=$this->lang->line('must')?>">
<select class="form-control" name="must">
<option value="MUST">Must</option>
<option value="OPTIONAL">Optional</option>
</select>
</td>
<td data-title="<?=$this->lang->line('active')?>">
<select class="form-control" name="active">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td data-title="<?=$this->lang->line('select')?>">
<input type="checkbox" name="selected">
</td>
</tr>
<?php $i++; }} ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm-8">
<h3> Apply To</h3><br>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<th class="col-sm-3">Class</th>
<th class="col-sm-4">Section</th>
</thead>
<tbody>
<?php
foreach ($categories as $category)
{
?>
<tr>
<td> <input type="checkbox"><span style="font-size:18px;margin-left:10px;"> <?php echo $category->classes; ?></span>
</td>
<td>
<?php
if(!empty($category->subs)) {
foreach ($category->subs as $sub) {
echo "<input type='checkbox'> ".$sub->section.' ' ;
}
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<br><br>
<div class="form-group">
<div class="col-sm-12">
<input type="submit" class="btn btn-success" value="<?=$this->lang->line("add_feestructure")?>" >
</div>
</div>
</div>
</form>
Controller
public function addfeestructure(){
$usertype = $this->session->userdata("usertype");
if($usertype == "Admin" || $usertype == "Accountant"){
$this->data['categories'] = $this->feestructure_m->get_categories();
$this->data['feetypes'] = $this->feestructure_m->get_feetypes();
if($_POST){
}
$this->data['subview'] = "feestructure/addfeestructure";
$this->load->view('_layout_main', $this->data);
}
}
To submit multiple fields of the same name you need to add square brackets to your the name attribute like this; <input type="text" name="fieldName[]">.
Then in your controller you'll use a foreach loop to process the form:
public function submitForm() {
$fieldA = $this->input->post('fieldName'); // this will be an array
for ($i = 0; $i < sizeof($fieldA); $i++) {
$array = array('fieldA' => $fieldA[$i]);
$this->your_model->addRecordToDatabase($tableName, $array);
}
}

How to work with dynamic checkboxes?

I am using code igniter. For employee i need to select possible services they can perform.i have a field service limitation where i need to store many service id that the employee can perform. my view file where i have service list is
<div class="container top">
<ul class="breadcrumb">
<li>
<a href="http://localhost/elfanto/elfanto_billing/admin/employee">
Admin </a>
<span class="divider">/</span>
</li>
<li class="active">
Service </li>
</ul>
<div class="row">
<div class="span12 columns">
<div >
<?php
$attributes = array('class' => 'form-inline reset-margin', 'id' => 'myform');
$options_manufacture = array(0 => "all");
foreach ($category as $row)
{
$options_manufacture[$row['id']] = $row['name'];
}
//save the columns names in a array that we will use as filter
$options_products = array();
foreach ($service as $array) {
foreach ($array as $key => $value) {
$options_products[$key] = $key;
}
break;
}
echo form_open('admin/employee', $attributes);
?>
</div>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
<input type="checkbox" value=""/>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php echo '<div class="pagination">'.$this->pagination->create_links().'</div>'; ?>
</div>
</div>
<div>
<button class="btn btn-primary" type="submit">Save changes</button>
<button class="btn" type="reset">Cancel</button>
</div>
<?php echo form_close(); ?>
here i have multiple check boxes when i choose the check box i need to get the id value and update the employee service limitation field with 4 or more id
You can use input arrays -
<input type="checkbox" value="'.$row['id'].'" name="services[]"/>
And store it in database as json string.

Categories