how to update google Sheet to MySQL - javascript

MY table :test_tbl
i need to apply Where condition .
In that code update all row in Price Column .
code:
function UdateRecords() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
conn.setAutoCommit(false);
var start = new Date();
var update = conn.prepareStatement("UPDATE test_table SET Price ='" + "100" + "'");
update.addBatch();
var batch_1 = update.executeBatch();
}
i need to update row where item="cake" replace item="Pizza" Quantity="10" Price="500" and total_price="5000"

UPDATE test_table
SET item = "Pizza"
, Quantity = 10
, Price = 500
, total_price = 5000
WHERE item = "cake"

Related

How to update multiple table rows into mysql table when click save button using Jquery and PHP?

I have a myql table name - invoice_details
invoice_number received_amount receiptID
1000 0.00
1001 0.00
1005 0.00
I have a html table
When clicking the save button, update invoice_details table with received amount and receiptID where invoice number in the html table row (1001,1005) Multiple rows will be there in html table.
appending this html table code:
$('#invoicelist_receipt').find('tbody').remove();
$.each($("input[name='myTextEditBox[]']:checked"), function() {
var data = $(this).parents('tr:eq(0)');
// values += $(data).find('td:eq(0)').text() + "," + $(data).find('td:eq(1)').text() + "," + $(data).find('td:eq(2)').text() + ",";
var t1 = $(data).find('td:eq(0)').text();//invoice date
var t2 = $(data).find('td:eq(1)').text();//invoice no
var t3 = $(data).find('td:eq(2)').text();//invoice amt
trtoggle += "<tr><td class=''>" + t1 + "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>" + t2 + "</td><td class=''>" + t3 + "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";
//values.push({ 'invoicedate':$(data).find('td:eq(0)').text(), 'invoiceno':$(data).find('td:eq(1)').text() , 'invoiceamount':$(data).find('td:eq(2)').text()});
});
$("#invoicelist_receipt").last().append(trtoggle);
when button clicks:(creating a new receipt)
var invoice_no_receipt = []; //where invoice no = 1000,1001,1005 etc..
var invoice_amt_receipt = [];//this received amount i have to update into database - invoice details table
$('.invoice_no_receipt').each(function() {
invoice_no_receipt.push($(this).val());
});
$('.invoice_amt_receipt').each(function() {
invoice_amt_receipt.push($(this).val());
});
$.ajax({
url: base_url + "index.php/welcome/savereciptfinal/",
type: "POST",
data: {
"getinvnumber": invoice_no_receipt,
"getinv_recived_amount": invoice_amt_receipt
},
success: function(data) {
}
});
PHP Codeigniter code
public function savereciptfinal()
$value2 = "0001"; //autogenerate number
$value = $value2;
$data = array(
'rece_No' => $value
);
$insert_id = 0;
if ($this->db->insert("receipt_details", $data)) {
$insert_id = $this->db->insert_id();
}
$data2 = array(
'rece_Amt' => $this->input->post('getrece_amt'),
'receipt_ID' => $value2; //the above auto generated number i need to update invoice_details for column receiptID
);
$this->db->where('invoice_No ', $inv_id);
$this->db->update('invoice_details', $data2);
}
You can get ideas from this code and sync it with your own code
First clear this script:
$('#invoicelist_receipt').find('tbody').remove();
$.each($("input[name='myTextEditBox[]']:checked"), function() {
var data = $(this).parents('tr:eq(0)');
var t1 = $(data).find('td:eq(0)').text();//invoice date
var t2 = $(data).find('td:eq(1)').text();//invoice no
var t3 = $(data).find('td:eq(2)').text();//invoice amt
trtoggle += "<tr><td class=''>" + t1 + "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>" + t2 + "</td><td class=''>" + t3 + "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";
});
$("#invoicelist_receipt").last().append(trtoggle);
Set your save button id instead of 'your_save_button_id'
You can see how send your invoice details in js
$(document).on('your_save_button_id', 'click', function(){
var invoice_no_receipt = [];
$('.invoice_no_receipt').each(function() {
// Json format to add product invoice detail
var invoice_detail = new Object();
// Get invoice number
invoice_detail.no = $(this).text();
// Get parent tr
var parent_tr = $(this).closest('tr');
// Get amount
invoice_detail.amt = $(parent_tr).find('invoice_amt_receipt').val();
// Add invoice detail to invoice_no_receipt
invoice_no_receipt.push(invoice_detail);
});
$.ajax({
url: base_url + "index.php/welcome/savereciptfinal/",
type: "POST",
data: {
"invoice_no_receipt": invoice_no_receipt
},
success: function(data) {
}
});
})
You can see how can get your invoice detail in PHP
IN Your PHP code in ajax function
// Get invoice_no_receipt
$invoice_no_receipt = $_POST('invoice_no_receipt');
foreach( $invoice_no_receipt as $item )
{
// Get invoice number
$invoice_no = intval($item['no']);
// Get invoice amount
$invoice_amt = floatval($item['amt']);
// Update your invoice in db one by one and by use from these variable
// Your update function
}

Google Apps Script - Very Basic Loop Problem Using isChecked()

Column J has checkboxes. Trying to create a loop statement to check if the row has an enabled checkbox (TRUE). Rows 1-3 are checked (TRUE). When I run this statement, logger is showing all rows as null when my expected result is for logger to show rows 1-3 as enabled (TRUE) and 4-500 as null.
function checkRangeTest(){
for(var row = 1; row <=500; row++){
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("J2:J500");
Logger.log("Row "+ row + " is checked? " + range.isChecked());
};
};
Testing the Value of Each Checkbox in column J of the Active Sheet
function checkRangeTest(){
var html="";
var ss=SpreadsheetApp.getActive();
var shsr=2;//start row
var sh=ss.getActiveSheet();
var rg=sh.getRange(shsr,10,sh.getLastRow()-shsr+1,1);
var values=rg.getValues();
values.forEach(function(r,i){
html+=Utilities.formatString('<br />Row: %s Value: %s',i+shsr,r[0]);
});
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Checkboxes");//creates a dialog displaying all of the results
}
Checkboxes:
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("J2:J500").getValues();
for(var row = 2; row <=500; row++){
Logger.log("Row "+ row + " is checked? " + (values[row-2] === true));
};

WooCommerce Add-ons information in Google Sheets

I am exporting WooCommerce orders to a google sheets using WooCommerce Webhooks and Apps Script
I have two queires
1) How do I reference WooCommerce "add-ons" data
2) Parent order ID is not showing in google sheet
function doPost(e) {
var myData = JSON.parse([e.postData.contents]);
var timestamp = new Date();
var order_number = myData.number;
var parent_id= myData.parent_id;
var order_status = myData.status;
var billing_first_name = myData.billing.first_name;
var billing_last_name = myData.billing.last_name;
var billing_phone = myData.billing.phone;
var billing_email = myData.billing.email;
var order_total = myData.total;
var billing_address_1 = myData.billing.address_1;
var billing_address_2 = myData.billing.address_2;
var billing_city = myData.billing.city;
var billing_address = billing_address_1 + ", " + billing_address_2 + ", " + billing_city;
var billing_postcode = myData.billing.postcode;
var shipping_first_name = myData.shipping.first_name;
var shipping_last_name = myData.shipping.last_name;
var shipping_address_1 = myData.shipping.address_1;
var shipping_address_2 = myData.shipping.address_2;
var shipping_city = myData.shipping.city;
var shipping_address = shipping_address_1 + ", " + shipping_address_2 + ", " + shipping_city;
var shipping_postcode = myData.shipping.postcode;
var lineitems=""
for (i in myData.line_items)
{
var product_name = myData.line_items[i].name;
var itemName = myData.line_items[i].name;
var quantity = myData.line_items[i].quantity;
var linetotal = myData.line_items[i].total;
var product_items = quantity + " x " + itemName + ": £"+linetotal +"\n";
var lineitems =lineitems+product_items;
}
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([timestamp,order_number,parent_id,order_status,billing_first_name,billing_last_name,billing_phone,billing_email,lineitems,order_total,billing_address,billing_postcode,shipping_first_name,shipping_last_name,shipping_address,shipping_postcode]);
}
What is missing from your question is:
We don't know from from where and how you get the order data in javascript
We don't know where you need to display the order Number.
So we can only make a generic answer.
Also note that asking multiple questions at once is not allowed in StackOverFlow.
1) The WooCommerce add-ons data within an WooCommerce order is stored as order item custom meta data:
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
// Get the special meta data in an array:
$meta_data = $item->get_meta_data();
echo '<pre>'; print_r($meta_data); echo '<pre>'; // Testing raw output
// Get all additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
echo '<pre>'; print_r($formatted_meta_data); echo '<pre>'; // Testing raw output
// Get the specific meta data from a meta_key:
$meta_value = $item->get_meta( 'custom_meta_key' );
}
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3
2) To get the parent order number from a subscription you will use:
From the subscription ID we can get the order ID very easily:
$order_id = wp_get_post_parent_id( $subscription_id );
From a WC_Subscription Object we can also get the order ID very easily:
$order_id = $subscription->get_parent_id();
Then from this order ID you can get the order number with:
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_mumber = $order->get_order_number();
or with:
$order_mumber = get_post_meta( $order_id, _order_number, true );

autoincrement id in javascript variable

didn't really succes with assigning values to new ids in my while(row) statement:
Here is an easy example, hope you understand what I want to do. Thanks
<?php...
$id=0;
while(rows = mysql_fetch_data){
$id = $id + 1;
$teamname = rows['team'];
?>
<script>
var team = '<?php echo $teamname; ?>';
var id = 'id_<?php echo $id; ?>';
//Here I want to save the teamnames as javascript variable "id_1", "id_2" etc, which I can use outside the while statement.
//For each row id = id_1, id_2, id_3, id_4.
//I want to make id_1 a variable which outputs teamname
//So that
//var id_1 = team; //team 1
//var id_2 = team; //team 2
<?php
}
?>
var id_1;
var id_2;
document.write("Teamname 1 = " + id_1 + "</br> Teamname 2 = " + id_2); //Here I want output of teamname 1 and 2.
</script>
I would recommend using an array rather than individual variables, as you have a list of team names:
<?php
$teamnames = [];
while(rows = mysql_fetch_data){
$teamnames[] = rows['team'];
}
?>
<script>
var teamnames = <?php echo json_encode($teamnames); ?>;
</script>
Then you end up with a client-side JavaScript array of team names. While you could then output them with document.write, there's probably a better option.
But here's your document.write updated to use the array:
var n;
for (n = 0; n < teamnames.length; ++n)
{
// Note: There's almost certainly a better choice than `document.write`
document.write("Teamname " + (n + 1) + " = " + teamnames[n] + "</br>");
}
Replace your code with the following and it should give you the output that you're after...
<script>
<?php
//...
$id_counter = 1; //Initialise counter
while($row = mysql_fetch_assoc()){
echo 'var id_'.$id_counter.' = "'.$rows['team'].'";'; //Echo row in the format: var id_X = "team_name";
$id_counter++; //Increment the counter
}
?>
document.write("Teamname 1 = " + id_1 + "</br> Teamname 2 = " + id_2); //Here I want output of teamname 1 and 2.
</script>

Binding change event to a select

I am attempting to build a Selectmenu for my Jquery Mobile 1.3.1 app. I want to trigger a change event to update totals based on the items selected from the Selectmenu.
First I build the list of options that I re-use:
var options = "<option value='NR'>Not Reached</option> ";
options += "<option value='ALT'>Alternate</option> ";
Then I build a callback that will update the totals
var selects = new Array();
//callback to update the totals on change
var changeCallback = function(event, ui) {
var totals = new Object();
selects.forEach(function(select) {
value = select.value;
total = totals.value;
if(total) {
++total;
totals.value = total;
}
else {
totals.value = 1;
}
})
_updateTotals(totals);
};
Then I loop through to build the select boxes. Here's where I build the select box and add the callback:
var select = "<select name=" + selectName + " id=" + selectName + "> ";
select += options;
select += "</select>";
select.on( "change", changeCallback);
select = jQuery.parseHTML(select)[0];
selects.push(select);
Problem is that when the bind gets executed, it jumps from the loop to the return, which I'm assuming indicates an error.

Categories

Resources