PHP 订单 - Fixed/variable 小计行?
PHP Order Form - Fixed/variable subtotal line?
我想在产品下添加税收行和总计行。税收固定为 100 美元,直到小计的 30% 超过 100 美元,此时税收等于 30%(小计)。这就是我最初在 javascript 中编写表格时的做法(但意识到 php 可能是从一开始就解决这个问题的方法——我错了吗?):
var setup = 0;
if(gt*.30>100){
setup = gt*.3;
}
else if (gt*.3<100 && gt>0){
setup = 100;
}
else if (gt =0) {
setup = 0;
}
这是我正在使用的模板:http://www.dyn-web.com/php/order_form/example2.php
目前的php代码:
<?php
$PRODUCTS = array(
// product abbreviation, product name, unit price
// follow valid name/ID rules for product abbreviation
array('prod1', '20" 4:3 (1600 x 1200)', 150),
array('prod2', '24" 16:9 (1920 x 1200)', 250),
array('prod3', '32" 16:9 (1920 x 1080)', 300),
array('prod4', '40" 16:9 (1920 x 1080)', 450),
array('prod5', '46” LCD 16:9 (1920 x 1080)', 600),
);
// functions for example 2 order form
function getOrderForm2() {
global $PRODUCTS;
$tbl = new HTML_Table('', 'demoTbl');
$frm = new HTML_Form();
// header row
$tbl->addRow();
$tbl->addCell('Product', 'first', 'header');
$tbl->addCell('Price', '', 'header');
$tbl->addCell('Quantity', '', 'header');
$tbl->addCell('Totals', '', 'header');
// display product info/form elements
foreach($PRODUCTS as $product) {
list($abbr, $name, $price) = $product;
// quantity text input
$qty_el = $frm->addInput('text', $abbr . '_qty', 0,
array('size'=>4, 'class'=>'cur', 'pattern'=>'[0-9]+', 'placeholder'=>0,
'onchange'=>'getProductTotal(this)',
'onclick'=>'checkValue(this)', 'onblur'=>'reCheckValue(this)') );
// total text input
$tot_el = $frm->addInput('text', $abbr . '_tot', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') );
// price hidden input
$price_el = $frm->addInput('hidden', $abbr . '_price', $price);
$tbl->addRow();
$tbl->addCell($name);
$tbl->addCell('$' . number_format($price, 2) . $price_el, 'cur' );
$tbl->addCell( $qty_el, 'qty');
$tbl->addCell( $tot_el );
}
// subtotal row
$tbl->addRow();
$tbl->addCell( 'Equipment Subtotal: ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'total', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
//tax row
$tbl->addRow();
$tbl->addCell( 'Delivery/Set-Up/Pick-Up: 30% of Equipment Subtotal (0 minimum): ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'tax', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
// total row
$tbl->addRow();
$tbl->addCell( 'Grand Total: ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'final', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
// additional fields for contact info
$tbl->addRow();
$tbl->addCell('First Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'first_name', '', array('size'=>36 ) ),
'', 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Last Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'last_name', '', array('size'=>36) ),
'', 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Email: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'email', '', array('size'=>36,
'pattern' => '^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$',
'required' => true
) ), '', 'data',
array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Phone: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'phone', '', array('size'=>36) ),
'last', 'data', array('colspan'=>3)
);
// submit button
$tbl->addRow();
$tbl->addCell( $frm->addInput('submit', 'submit', 'Submit'),
'submit', 'data', array('colspan'=>4) );
$frmStr = $frm->startForm('ex2_result.php', 'post', '', array('onsubmit'=>'return checkSubmit(this);') ) .
$tbl->display() . $frm->endForm();
return $frmStr;
}
我任意将变量 "tax" 和 "final" 分配给税收和总计行,但我什至不确定模板中的小计是如何计算的,我看不到任何地方的任何线路求和。我觉得我可能有点不知所措,但我想我可以在一点帮助下强迫自己解决这个问题。谁能指导我正确设置它?
您可以使用类似这样的方法来计算税费并将其添加为 php 中的第四个参数。
foreach($PRODUCTS as $key => $product) {
$unit_price = (int) $product[2];
$tax = $unit_price * 0.3;
if($tax < 100) {
$tax = 100;
}
$PRODUCTS[$key][3] = $tax;
}
print_r($PRODUCTS);
这将给出类似
的输出
Array
(
[0] => Array
(
[0] => prod1
[1] => 20" 4:3 (1600 x 1200)
[2] => 150
[3] => 100
)
[1] => Array
(
[0] => prod2
[1] => 24" 16:9 (1920 x 1200)
[2] => 250
[3] => 100
)
[2] => Array
(
[0] => prod3
[1] => 32" 16:9 (1920 x 1080)
[2] => 300
[3] => 100
)
[3] => Array
(
[0] => prod4
[1] => 40" 16:9 (1920 x 1080)
[2] => 450
[3] => 135
)
[4] => Array
(
[0] => prod5
[1] => 46” LCD 16:9 (1920 x 1080)
[2] => 600
[3] => 180
)
)
编辑
下面计算小计的税金并分配给一个包含小计税金的数组。税金按规则计算。
$sub_total = 0;
foreach($PRODUCTS as $key => $product) {
$unit_price = (int) $product[2];
$sub_total += $unit_price;
}
$tax = $sub_total * 0.3;
if($tax < 100) {
$tax = 100;
}
$totals = [$sub_total,$tax];
print_r($totals);
我想在产品下添加税收行和总计行。税收固定为 100 美元,直到小计的 30% 超过 100 美元,此时税收等于 30%(小计)。这就是我最初在 javascript 中编写表格时的做法(但意识到 php 可能是从一开始就解决这个问题的方法——我错了吗?):
var setup = 0;
if(gt*.30>100){
setup = gt*.3;
}
else if (gt*.3<100 && gt>0){
setup = 100;
}
else if (gt =0) {
setup = 0;
}
这是我正在使用的模板:http://www.dyn-web.com/php/order_form/example2.php
目前的php代码:
<?php
$PRODUCTS = array(
// product abbreviation, product name, unit price
// follow valid name/ID rules for product abbreviation
array('prod1', '20" 4:3 (1600 x 1200)', 150),
array('prod2', '24" 16:9 (1920 x 1200)', 250),
array('prod3', '32" 16:9 (1920 x 1080)', 300),
array('prod4', '40" 16:9 (1920 x 1080)', 450),
array('prod5', '46” LCD 16:9 (1920 x 1080)', 600),
);
// functions for example 2 order form
function getOrderForm2() {
global $PRODUCTS;
$tbl = new HTML_Table('', 'demoTbl');
$frm = new HTML_Form();
// header row
$tbl->addRow();
$tbl->addCell('Product', 'first', 'header');
$tbl->addCell('Price', '', 'header');
$tbl->addCell('Quantity', '', 'header');
$tbl->addCell('Totals', '', 'header');
// display product info/form elements
foreach($PRODUCTS as $product) {
list($abbr, $name, $price) = $product;
// quantity text input
$qty_el = $frm->addInput('text', $abbr . '_qty', 0,
array('size'=>4, 'class'=>'cur', 'pattern'=>'[0-9]+', 'placeholder'=>0,
'onchange'=>'getProductTotal(this)',
'onclick'=>'checkValue(this)', 'onblur'=>'reCheckValue(this)') );
// total text input
$tot_el = $frm->addInput('text', $abbr . '_tot', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') );
// price hidden input
$price_el = $frm->addInput('hidden', $abbr . '_price', $price);
$tbl->addRow();
$tbl->addCell($name);
$tbl->addCell('$' . number_format($price, 2) . $price_el, 'cur' );
$tbl->addCell( $qty_el, 'qty');
$tbl->addCell( $tot_el );
}
// subtotal row
$tbl->addRow();
$tbl->addCell( 'Equipment Subtotal: ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'total', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
//tax row
$tbl->addRow();
$tbl->addCell( 'Delivery/Set-Up/Pick-Up: 30% of Equipment Subtotal (0 minimum): ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'tax', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
// total row
$tbl->addRow();
$tbl->addCell( 'Grand Total: ', 'total', 'data', array('colspan'=>3) );
$tbl->addCell( $frm->addInput('text', 'final', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
// additional fields for contact info
$tbl->addRow();
$tbl->addCell('First Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'first_name', '', array('size'=>36 ) ),
'', 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Last Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'last_name', '', array('size'=>36) ),
'', 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Email: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'email', '', array('size'=>36,
'pattern' => '^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$',
'required' => true
) ), '', 'data',
array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Phone: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'phone', '', array('size'=>36) ),
'last', 'data', array('colspan'=>3)
);
// submit button
$tbl->addRow();
$tbl->addCell( $frm->addInput('submit', 'submit', 'Submit'),
'submit', 'data', array('colspan'=>4) );
$frmStr = $frm->startForm('ex2_result.php', 'post', '', array('onsubmit'=>'return checkSubmit(this);') ) .
$tbl->display() . $frm->endForm();
return $frmStr;
}
我任意将变量 "tax" 和 "final" 分配给税收和总计行,但我什至不确定模板中的小计是如何计算的,我看不到任何地方的任何线路求和。我觉得我可能有点不知所措,但我想我可以在一点帮助下强迫自己解决这个问题。谁能指导我正确设置它?
您可以使用类似这样的方法来计算税费并将其添加为 php 中的第四个参数。
foreach($PRODUCTS as $key => $product) {
$unit_price = (int) $product[2];
$tax = $unit_price * 0.3;
if($tax < 100) {
$tax = 100;
}
$PRODUCTS[$key][3] = $tax;
}
print_r($PRODUCTS);
这将给出类似
的输出Array
(
[0] => Array
(
[0] => prod1
[1] => 20" 4:3 (1600 x 1200)
[2] => 150
[3] => 100
)
[1] => Array
(
[0] => prod2
[1] => 24" 16:9 (1920 x 1200)
[2] => 250
[3] => 100
)
[2] => Array
(
[0] => prod3
[1] => 32" 16:9 (1920 x 1080)
[2] => 300
[3] => 100
)
[3] => Array
(
[0] => prod4
[1] => 40" 16:9 (1920 x 1080)
[2] => 450
[3] => 135
)
[4] => Array
(
[0] => prod5
[1] => 46” LCD 16:9 (1920 x 1080)
[2] => 600
[3] => 180
)
)
编辑
下面计算小计的税金并分配给一个包含小计税金的数组。税金按规则计算。
$sub_total = 0;
foreach($PRODUCTS as $key => $product) {
$unit_price = (int) $product[2];
$sub_total += $unit_price;
}
$tax = $sub_total * 0.3;
if($tax < 100) {
$tax = 100;
}
$totals = [$sub_total,$tax];
print_r($totals);