gpt4 book ai didi

javascript - PHP 订单 - 固定/可变小计行?

转载 作者:行者123 更新时间:2023-12-03 06:47:04 25 4
gpt4 key购买 nike

我希望在产品下添加税项和总计项。税费固​​定为 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”分配给税收和总计行,但我什至不确定如何在模板中计算小计,我在任何地方都看不到任何行求和。我觉得我可能有点难以理解,但我想我可以在一点帮助下强制自己解决这个问题。谁能指导我正确设置它?

最佳答案

您可以使用类似的方法来计算税金并将其添加为 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);

关于javascript - PHP 订单 - 固定/可变小计行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37693798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com