2016-06-08 68 views
0

我正在寻找添加税线以及产品下的总行。税额固定为100美元,直到小计的30%超过100美元为止,此时税额等于30%(小计)。这是我做的,当我最初写在JavaScript形式(但意识到PHP的可能是去了解这个从方式获得去 - 我错了?):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\+\'\.-][email protected][\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; 
} 

我人为地赋予变量“税”和“最后的”税务和总计行,但我甚至确定小计在模板中是如何计算的,我没有看到任何线summi在任何地方。我觉得我可能会有点头痛,但我认为我可以通过一些帮助来解决这个问题。任何人都可以指导我正确设置它吗?

回答

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 
     ) 

) 

EDIT

下面计算在子总税收和分配给具有子总和税收的阵列。税按照规定计算。

$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); 
+0

感谢您的回复......我认为我遇到的问题是,税款需要小计的功能,而不是每个项目,因为它是30%(如果超过100美元)的总订单。 我认为在你写出的情况下,3种产品[1]的总税额应该是300美元,当它应该达到225美元时。那有意义吗? – sixfiveoh

+0

查看我更新的答案。这有帮助吗? – phreakv6

+0

它呢,谢谢!我还有一个问题来自我的原始php - //小计行 $ tbl-> addRow(); $ tbl-> addCell('Equipment Subtotal:','total','data',array('colspan'=> 3)); ('readonly'=> true,'size'=> 8,'class'=>'cur')))$ tbl-> addCell($ frm-> addInput('text','total',0,array,'' ; 究竟在这段代码中的小计是总结?我看到小计值的addinput('text','total',0)行,但没有看到任何对全局或局部变量的引用,这些变量具有总计我的单元总数的公式... – sixfiveoh