我希望在产品下添加税行以及总计行。税收固定为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 ($100 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”分配给税和总行,但我甚至不确定如何在模板中计算小计,我没有看到任何行汇总。我觉得我可能会有点头脑,但我想我可以帮助自己解决这个问题。任何人都可以指导我正确设置吗?
答案 0 :(得分:1)
你可以使用这样的东西来计算和添加税作为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);