Jump to content

Function not working


Lostnode

Recommended Posts

I am still new to programming PHP and this is the first time I am creating a function.  Here is where I pull the function (scroll down to commented area)

 

<table width="250" border="0" cellpadding="2" cellspacing="0">
  <tr>
    <td width="145"><strong>Province:</strong></td>
    <td colspan="2" align="right"><? echo $prov ?></td>
  </tr>
  <tr>
    <td><strong>Postal:</strong></td>
    <td colspan="2" align="right"><? echo $postal ?></td>
  </tr>
  <tr>
    <td><strong>Lbs:</strong></td>
    <td colspan="2" align="right"><? echo $lbs ?></td>
  </tr>
  <tr>
    <td colspan="3"> </td>
  </tr>
  </table>
  <img src="/images/carriers/canpar.png" border="0"/>
<table width="250" border="0" cellpadding="2" cellspacing="0">
<?php if ($debugmode == 'on') { ?>
  <tr>
    <td><strong>Cost:</strong></td>
    <td width="14" align="right"><strong>$</strong></td>
    <td width="71" align="right"><? echo number_format($dzone, 2, '.', '') ?></td>
  </tr>
    <?php if ($cp_markup != "") { ?><tr>
    <td><strong>Markup:</strong></td>
    <td align="right"><strong>$</strong></td>
    <td align="right"><? echo number_format($cp_mark, 2, '.', '') ?></td>
  </tr><?php } ?>
  <? 
  $price = ( number_format($cp_mark, 2, '.', '') +number_format($dzone, 2, '.', '') );
  } Else { ?> 
  <tr>
    <td><strong>Price:</strong></td>
    <td align="right"><strong>$</strong></td>
    <td align="right"><? 
    $price = ( number_format($cp_mark, 2, '.', '') +number_format($dzone, 2, '.', '') );
    echo number_format($price, 2, '.', ''); ?></td>
  </tr>
    <? } ?>
  <?php if ($cp_fuelcharge != "") { ?><tr>
    <td><strong>Fuel Charge:</strong></td>
    <td align="right"><strong>$</strong></td>
    <td align="right"><? echo number_format($cp_fuelcharge, 2, '.', '') ?></td>
  </tr><?php } ?>
<?php 
taxcalc(); //Here is the function pull
echo $taxrow; // this is supposed to be defined in the function
?>
  <tr>
    <td class="priceresults"><strong>Total:</strong></td>
    <td align="right"class="priceresults"><strong>$</strong></td>
    <td align="right"class="priceresults"><?php
$total = ( number_format($price, 2, '.', '') + number_format($tax, 2, '.', '') );
echo  number_format($total, 2, '.', '');
?></td>
  </tr>
</table>

 

Here is my taxcalc() function:

 

function taxcalc() {
    if ($prov == "AB") {
        $gst = '5%';
        $gstcalc = '0.05';
        $tax = $price * $gstcalc;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "BC") {
        $hst = '12%';
        $hstcalc = '0.12';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td>HST ($hst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }    
    if ($prov == "MB") {
        $gst = '5%';
        $pst = '7%';
        $gstcalc = '0.05';
        $pstcalc = '0.07';
        $gtax = $price * $gstcalc;
        $ptax = $price * $pstcalc;
        $tax = $gtax + $ptax;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($gtax, 2, '.', '') . "</td></tr><tr><td>PST ($pst):</td><td align='right'><strong>$</strong></td><td>" . number_format($ptax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "ON") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td>HST ($hst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "NB") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td>HST ($hst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "NL") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td>HST ($hst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "NS") {
        $hst = '15%';
        $hstcalc = '0.15';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td>HST ($hst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "NT") {
        $gst = '5%';
        $gstcalc = '0.05';
        $tax = $price * $gstcalc;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "NU") {
        $gst = '5%';
        $gstcalc = '0.05';
        $tax = $price * $gstcalc;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "PE") {
        $gst = '5%';
        $pst = '10%';
        $gstcalc = '0.05';
        $pstcalc = '0.10';
        $gtax = $price * $gstcalc;
        $ptax = $price * $pstcalc;
        $tax = $gtax + $ptax;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($gtax, 2, '.', '') . "</td></tr><tr><td>PST ($pst):</td><td align='right'><strong>$</strong></td><td>" . number_format($ptax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "QC") {
        $gst = '5%';
        $pst = '7.5%';
        $gstcalc = '0.05';
        $pstcalc = '0.075';
        $gtax = $price * $gstcalc;
        $ptax = ($price + $gtax) * $pstcalc;
        $tax = $gtax + $ptax;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($gtax, 2, '.', '') . "</td></tr><tr><td>PST ($pst):</td><td align='right'><strong>$</strong></td><td>" . number_format($ptax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "SK") {
        $gst = '5%';
        $pst = '5%';
        $gstcalc = '0.05';
        $pstcalc = '0.05';
        $gtax = $price * $gstcalc;
        $ptax = $price * $pstcalc;
        $tax = $gtax + $ptax;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($gtax, 2, '.', '') . "</td></tr><tr><td>PST ($pst):</td><td align='right'><strong>$</strong></td><td>" . number_format($ptax, 2, '.', '') . "</td></tr>";
    }
    if ($prov == "YT") {
        $gst = '5%';
        $gstcalc = '0.05';
        $tax = $price * $gstcalc;
        $taxrow = "<tr><td>GST ($gst):</td><td align='right'><strong>$</strong></td><td>" . number_format($tax, 2, '.', '') . "</td></tr>";
    }

}

 

I am not getting any errors, just nothing is showing up.

 

My results show up as:

 

Province: 	ON
Postal: 	K2C
Lbs: 	25

Cost: 	$ 	22.57
Markup: 	$ 	0.00
Fuel Charge: 	$ 	5.00
Total: 	$ 	22.57

 

After Fuel Charge should be my taxes but they do not show up

 

Link to comment
https://forums.phpfreaks.com/topic/218982-function-not-working/
Share on other sites

The include is a few lines above the table

 

include("./inc/functions.php");

 

what do you mean pass the variables?  Could you give me an example?

 

its all based off of $prov which is defined in my sql pull and works in the table how do I get the function to use it?

Ok, I did that and moved the echo to the function itself... This seemed to work, but I want to use the variables in the function after the function has been called... I.E. I want to use the $tax later on, is this possible?  Seeing as I could not Echo $taxrow outside the function, how to I pass the functions out?

Ok, not sure how this works... I am using Ontario so I will only copy the new updated snipet from my php function

 

function taxcalc($prov,$price) {
        if ($prov == "ON") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td><strong>HST ($hst):</strong></td><td align='right'><strong>$</strong></td><td align='right'>" . number_format($tax, 2, '.', '') . "</td></tr>";
        echo $taxrow;
        return $tax;
    }
}

 

How ever How do I use $tax afterwards? I have later on in my table

<tr>
    <td class="priceresults"><strong>Total:</strong></td>
    <td align="right"class="priceresults"><strong>$</strong></td>
    <td align="right"class="priceresults"><?php
$total = ( number_format($price, 2, '.', '') + number_format($tax, 2, '.', '') );
echo  number_format($total, 2, '.', '');
?></td>
  </tr>

 

I need to be able to add the tax calculation using the tax calculated in the funciton, but use the value outside the function.  It diesn;t seem to worj the way I have it here.

 

The example you show is

<?php
function addit($first_number,$second_number){
    $total_sum = $first_number + $second_number;
    return $total_sum;
}

$first_number = "1";
$second_number ="2";
$total = addit($first_number,$second_number);
echo "$total";
?> 

 

I cannot do that as I have an echo now in my function.  Making $tax = taxcalc($prov,$price); will not work as I have that echo in there, what happens is it outputs my tax twice on screen..

 

Cost: 	$ 	22.57
Markup: 	$ 	0.00
Fuel Charge: 	$ 	5.00
HST (13%):	$	3.58
HST (13%):	$	3.58
Total: 	$ 	31.15

 

The calculation is right but it echos out the tax entry twice...

 

try...

 

function taxcalc($prov,$price) {
        if ($prov == "ON") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td><strong>HST ($hst):</strong></td><td align='right'><strong>$</strong></td><td align='right'>" . number_format($tax, 2, '.', '') . "</td></tr>";
        $my_values = $taxrow . "|" . $tax;
        return $my_values;
    }
}

$my_array = explode("|", taxcalc($prov,$price));
$tax = $my_array[1];
$tax_row = $my_array[0];

 

then use those two variables where you want

A similar solution involves returning an array and using list to create the variables to use:

<?php
function taxcalc($prov,$price) {
        if ($prov == "ON") {
        $hst = '13%';
        $hstcalc = '0.13';
        $tax = $price * $hstcalc;
        $taxrow = "<tr><td><strong>HST ($hst):</strong></td><td align='right'><strong>$</strong></td><td align='right'>" . number_format($tax, 2, '.', '') . "</td></tr>";
        return array($tax,$taxrow);
    }
}

list($tax,$tax_row) = taxcalc($prov,$price);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.