how to serialize whole table and variable to php page - php

I am creating an inventory control system for my sales. i can serialize complete table successfully i have one problem i have sell_price column in in table, retail price column is not there in the table.how can send the retail price to the php page.because i need to calculating profit at end the day sales. i am sending all record to loading_add.php page through ajex. what i tried so far i wrote it below. how to send retail_price in to loading_add.php page.
retail_price : retail_price
var table_data = [];
$('#product_list tbody tr').each(function(row,tr)
{
var sub = {
'product_id' : $(tr).find('td:eq(1)').text(),
'cat_id' : $(tr).find('td:eq(2)').text(),
'product_name' : $(tr).find('td:eq(3)').text(),
retail_price : retail_price
'sell_price' : $(tr).find('td:eq(4)').text(),
'qty' : $(tr).find('td:eq(5)').text(),
'total_cost' : $(tr).find('td:eq(6)').text(),
};
table_data.push(sub);
});
$.ajax({
type : "POST",
url: '../php/product/loading_add.php',
dataType: 'JSON',
data: {data:table_data},

One way is to take hidden input if you are not want to display the data to user still you needs it.
for example, in your product listing table, you can take hidden input of retail price as you need it but dont have to display it to users.
so after executing mysql select query you can fetch data in your table as:
<table id="product_list" class="table table-bordered table-striped">
<thead>
<tr>
<th>Id</th>
<th>Category</th>
<th>product Name</th>
<th>price</th>
<th>quantity</th>
<th>total</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $row){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['cat_id']; ?></td>
<td><?php echo $row['product_name'] ?></td>
<!-- below in sell price td we will take hidden input for retail price -->
<td><input type="hidden" class="retail_price" value="<?php echo $row['retail_price'] ?>"><?php echo $row['sell_price'] ?></td>
<td><?php echo $row['quantity'] ?></td>
<td><?php echo $row['total_cost']; //or price * quantity ?></td>
</tr>
<?php } ?>
</tbody>
</table>
And in your script you have mistaken as $(tr).find('td:eq(1)').text(),
if you want to fetch first td then you should use $(tr).find('td:eq(0)').text() because here td is array of elements and in array index always start with 0.
so your script should be as:
$(function () {
var table_data = [];
$('#product_list tbody tr').each(function (row, tr)
{
var sub =
{
'product_id': $(tr).find('td:eq(0)').text(),
'cat_id': $(tr).find('td:eq(1)').text(),
'product_name': $(tr).find('td:eq(2)').text(),
'sell_price': $(tr).find('td:eq(3)').text(),
'retail_price': $(tr).find('td:eq(3)').find('.retail_price').val(),
'qty': $(tr).find('td:eq(4)').text(),
'total_cost': $(tr).find('td:eq(5)').text(),
};
table_data.push(sub);
});
//here you can fire ajax request for adding record
});
Another way is to take data attribute for your needed value
in same example as above you can take data attribute in your td as
<td data-retail_price="<?php echo $row['retail_price'] ?>"><?php echo $row['sell_price'] ?></td>
and in your script you can get data attribute's value as in same script as above:
'retail_price': $(tr).find('td:eq(3)').data('retail_price'),

Related

Why does it enter the same id in the database instead of entering one by one each?

Hy,
I want to ban a user when the admin clicks on "bannir" (this will collect the id of the user and put "banned" at 1 in the database)
to print the user informations I'm using a while loop, but when I try to collect the id of the user where in the html tag with the class "idUser", it is always sending the first id and I don't know why..
image of membership area
<div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Prénom</th>
<th>Nom</th>
<th>Email</th>
<th>Admin</th>
<th>Banni</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($userInfo = $req->fetch()){?>
<tr>
<td class="idUser"><?= $userInfo['id_user'] ?></td>
<td><?= $userInfo['last_name'] ?></td>
<td><?= $userInfo['first_name'] ?></td>
<td><?= $userInfo['email'] ?></td>
<td><?php if($userInfo['admin'] == 1){ ?>
<img src="../img/ok.png">
<?php } else { ?>
<img src="../img/no.png">
<?php } ?></td>
<td><?php if($userInfo['banned'] == 1){ ?>
<strong style="color:#E04949;">OUI</strong>
<?php } else { ?>
<strong style="color:#6AC259;">NON</strong>
<?php } ?></td>
<td>Modifier | <?php if($userInfo["banned"] == 0){ ?> Bannir <?php }else{ ?> Débannir <?php } ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$('.banMemberJquery').click(function(){
var idUser = $( ".idUser" ).html();
alert('Utilisateur Numero ' + idUser + ' banni avec succès.');
$.ajax({
url:"banMemberRequest.php",
data:'idUser='+idUser,
}).done(function(data){
$('#result').html(data);
});
});
PS : When I click on "bannir", the request in the file "banMemberRequest.php" is working correctly.
Thank's in advance for helping
The issue is because you're selecting all .idUser elements. Calling html() on that will only read the HTML of the first one in that collection.
To fix this you need to use DOM traversal to find only the .idUser element which is related to the .banMemberJquery element which was clicked. To do that you can use closest() and find(), like this:
$('.banMemberJquery').click(function() {
var idUser = $(this).closest('tr').find('.idUser').text(); // note text(), not html()
$.ajax({
url: 'banMemberRequest.php',
data: { idUser: idUser },
}).done(function(data) {
$('#result').html(data);
});
});

CodeIgniter Delete row from datatables involving mysql database

Hi I am wondering if it's possible to do something like selecting a row within a datatable and have a button "delete" with a method in the controller.
My delete button is to remove the row from the database itself and refresh the page.
This is what my view contains:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#datatables').DataTable();
});
</script>
<table id= "datatables" class = "table">
<thead>
<tr>
<th> Patient Name </th>
<th> Patient`enter code here` ID </th>
</tr>
</thead>
<tbody class = "list">
<?php foreach ($patients as $patient): ?>
<tr>
<td><?=$patient->first_name ?></td>
<td><?=$patient->patientID ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
(delete button)
echo anchor('something/delete', 'Delete', 'class= "some class"');
What i want to do is:
Get the id of the selected row in the datatable and pass it to another page in a controller for processing.
Is that possible?
since you are using DataTables plugin. when defining your columns, add a column for checkboxes, like so:
{
"data": null,
"defaultContent": "",
'class':'user_chkbox',
"searchable": false,
"render": function ( data, type, full, meta ) {
var checkbox = "<input type='checkbox' name='user_ids[]' value='" + data.id + "' />";
return checkbox ;
}
},
you can get the list of id's to delete by accessing the 'user_ids' post data.
$user_ids = $this->input->post('user_ids');
you can then in your submit handler function:
$this->db->where_in('id',$user_ids);
$this->db->delete('user_table');

Updating a field from database using ajax with codeigniter active record fails

I'm having a problem in updating the qty field in my database using ajax. I don't know where the error occur because when I tried to click the minus button it keeps on subtracting it multiple times. I've google same problem but didn't find any solution. Here is my code.
Here is my CONTROLLER:
public function deduct(){
$this->inpatient_model->deduct();
$data['inpatient'] = $this->inpatient_model->getinpatientlab();
$this->load->view('admin/queryinpatient',$data);
}
Here is my MODEL:
public function deduct(){
$this->db->select('*');
$this->db->from('inpatientlab');
$this->db->where('ilid',$this->input->post('lid'));
$query = $this->db->get();
$row = $query->result_array();
if($query->num_rows() > 0 ){
if($row[0]['qty'] > 1){
$uno = 1;
$this->db->set('qty','qty-'.$uno,FALSE)
->where('ilid',$this->input->post('lid'))
->update('inpatientlab');
}
}
}
Here is my VIEW:
<?php if($inpatient): ?>
<table class="table table-striped">
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
<th>Sub Total</th>
<th>Action</th>
</tr>
<?php foreach($inpatient as $rows): ?>
<tr>
<td><?php echo $rows->ldesc; ?></td>
<td><?php echo $rows->qty; ?></td>
<td><?php echo $rows->lprice; ?></td>
<td><?php echo number_format($rows->qty * $rows->lprice,2); ?></td>
<td>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnRemove"><i class="icon-trash"></i></button>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnMinus"><i class="icon-minus"></i></button>
</td>
</tr>
<?php endforeach; ?>
</table>
<script type="text/javascript" >
(function(){
$(document).on('click','.btnMinus',function(){
var lid = $(this).val(),
pid = $('.pid').val(),
dataString = "id=" + pid + "&lid=" + lid;
console.log(dataString);
$.ajax({
type:"POST",
url: base_url + "inpatient/deduct",
data:dataString,
success:function(data){
$('.pickedlab').html(data);
}
})
return false;
});
})()
My View in here is also loaded via ajax inside a bootstrap-modal. I really have no idea why it keeps on subtracting multiple times. Any help?
Have you tried using the browser debug and find out whether your AJAX is calling multiple times. I am not familiar with your Controller-Model settings, normally my models are pure POCO class only. All computations are done at the controller level.

Disable checkbox on table on load when there is specific value

I want to check the table row value on page load..
Table example:
Name || Status || Set
John || Pass || [ ] (checkbox)
Chris || Fail || [ ] (checkbox)
When the status is 'Fail' I want to disable the checkbox..
Now I'm using this jQuery:
<script>
$(document).ready(function() {
if(getElementsByClassName('paket_ava').value=='kosong'))
{
document.getElementById("checkboxx").disabled=true;
}
});
</script>
and this is my PHP table code :
<tr>
<td><?php echo $paket['id_paket'];?></td>
<td><?php echo $paket['nama_paket'];?></td>
<td><?php echo $paket['keterangan_paket'];?></td>
<td><?php echo $paket['harga'];?></td>
<td><img src='<?php echo $paket['gambar_paket']?>' width='120' height='120'></td>
<td class="paket_ava"><?php echo $paket['ketersediaan_paket'];?></td>
// class on the table data
<td><?php echo $paket['status_harian_paket'];?></td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value="<?=$paket["id_paket"];?>"></td>
<?php
echo ("<td><a href='edit_data_paket.php?id_paket=$paket[id_paket]'>Edit</a></td>");
?>
</tr>
The code above is not working, but if I change to:
if(getElementsByClassId('paket_ava').value=='kosong'))
{
document.getElementById("checkboxx").disabled=true;
}
(of course I change the class in the table into Id)
When the page load its acting strange and the checkbox on the first data is disabled..
How to do this properly?
Try like below.... It will help you...
Fiddle Example: http://jsfiddle.net/68wbx/126/
Suppose your HTML Table was like below:
HTML:
<table id="datapaket" border="1">
<tr>
<th>Name</th><th>Status</th><th>Set</th>
</tr>
<tr>
<td>John</td><td class="paket_ava">Pass</td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value='sam'/></td>
</tr>
<tr>
<td>Chris</td>
<td class="paket_ava">Fail</td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value='sam'/></td>
</tr>
</table>
and try the Below Jquery :
$(document).ready(function() {
$('#datapaket tr').each(function() { //Looping Every Table Row
//Get the TD Value that have Classname ".paket_ava"
var str = $(this).find('.paket_ava').html();
if(typeof str !== 'undefined'){
if (str.indexOf("Fail") >= 0)
$(this).find('td:nth-child(3) input:checkbox').attr("disabled", true);
};
});
});
traverse through each element having class 'paket_ava' and do your stuff inside it. like
$('.paket_ava').each(function(i, obj) {
// your stuff...
});
Reference : jQuery to loop through elements with the same class

jquery how to get value in a table

I have the following code that generates a table:
<table class="table table-bordered table-striped" id="assignedvs">
<thead>
<tr>
<th>VlId</th>
<th>Name</th>
<th>Status</th>
<th>Voice</th>
<th>Jumbo</th>
<th>Mode</th>
</tr>
</thead>
<tbody>
<?php foreach ($vln as $vlndetail): ?>
<tr>
<td id='vlid'><?php echo $vlndetail['VlId'] ?></td>
<td><?php echo $vlndetail['Name'] ?></td>
<td><?php echo $vlndetail['Status'] ?></td>
<td><?php echo $vlndetail['Voice'] ?></td>
<td><?php echo $vlndetail['Jumbo'] ?></td>
<td><?php echo $vlandetail['Mode'] ?></td>
</tr>
<?php endforeach ?>
I need to find the row where the VlId matches what the user has specified in a text box. Once I've found this record, I want to grab value in the mode column for the particular row.
here's what i've written so far:
$('#delete').live('click', function() {
//get a count of all records. only allowed to delete if you have more than one + the header.
var reccount = $('#assignedvs tr').length;
if (reccount > 2)
{
//loop through the table
$('#assignedvs tr').each(function() {
var temp = $(this).find(".vlid").html();
console.log(temp);
if (temp == $('#uservalue').val){
//grab mode column
}
});
}
else
{
alert("error: must have at least 1 record.");
}
});
problem - the code i have to reference the vlid column is incorrect. it always prints a null to the console.
Can you tell me what I've done wrong?
thanks.
EDIT 1
I changed my id to a class and changed my jquery back to the original code I had. it's working - except for the fact that I think it's including the header . I have 4 rows + header. When i check the console results, the first print out is always NULL and then the correct value for the 4 records. how do it get it to ignore the header?
That's because you are finding by className, not by id. To find by id, use the following instead:
$(this).find("#vlid").html();
However, since ids should be unique across the entire document, a better solution would be to maintain your current code, and instead of using <td id='vlid'>, use <td class='vlid'>.
Also note that val() is a function. Thus, to get the value of a given input, you should use $('#uservalue').val().
EDIT: To exclude the header, use the $('#assignedvs tbody tr') selector. This way, you only get rows that are descendants of tbody, thus ignoring the header rows, which descend from thead.
couple of changes:
<?php echo $vlndetail['VlId']; //missing semi-colon ?>
var temp = $(this).find("#vlid").html(); vlid is an id
You can easily do this via datatables at http://datatables.net
var temp = $(this).find("#vlid").html(); // .vlid (by class) changed to #vlid (by id)
An even easier solution.
Assign an ID to each row with the vlId (possibly prepend tr_ to the ID to avoid duplication).
Assign a class with the column name to each datacell.
Like so:
<?php foreach ($vln as $vlndetail): ?>
<tr id='tr_<?php echo $vlndetail['VlId'] // set the ID ?>'>
<td class='vlid'><?php echo $vlndetail['VlId'] ?></td>
<td class='Name'><?php echo $vlndetail['Name'] ?></td>
<td class='Status'><?php echo $vlndetail['Status'] ?></td>
<td class='Voice'><?php echo $vlndetail['Voice'] ?></td>
<td class='Jumbo'><?php echo $vlndetail['Jumbo'] ?></td>
<td class='Mode'><?php echo $vlandetail['Mode'] ?></td>
</tr>
<?php endforeach ?>
Then to get the Name of the selected vlID just do this JQUERY:
var rowID = "#tr_" + $('#uservalue').val(); // this is optional. I prefer to cache values for increased code readability.
$(rowID + " td.Mode").dostuffhere(); // this selector returns the Mode cell for the row indicated by the user
This will grab the Mode column of that specific row.

Categories