You are here:  » Arrays (Tax & Shipping)

Support Forum



Arrays (Tax & Shipping)

Submitted by multiz on Thu, 2007-01-18 20:51 in

Hello David,

I've added fields to add shipping and tax to my site.
For tax, I have 2 fields (state & tax). I put this in the tables by seperating each state and tax percentage by a comma (,). I want to include a $_POST["myState"] for the user to select from a drop-down box and then the appropriate tax amount will be displayed.

One merchant has the following fields:
state = IL,WI,IN,MI
tax = .0825,check site,.06,.06

I was going to explode the state and tax variables by doing this (in html/prices.php) for each merchant:

<?php
$state 
explode(","$product["state"]);
$tax explode(","$product["tax"]);
?>

But I don't know how I can compare the $_POST["myState"] value with each state, and then if a state does match, it'll pull out the correct tax percentage.

You can see what I've done so far on this page:
http://www.zzprices.com/product/CANON_1267B001.html

Thanks for your excellent support,
Michael

Submitted by support on Thu, 2007-01-18 21:12

Hi Michael,

What you can do is use PHP's array_values() function to extract the two arrays that you've created using explode() into numerically indexed arrays, and then combine them to give you a associative array indexed by state.

Here's some example code based on your variables:

<?php
  $product
["state"] = "IL,WI,IN,MI";
  
$product["tax"] = ".0825,check site,.06,.06";
  
$state explode(","$product["state"]);
  
$tax explode(","$product["tax"]);
  
$state_values array_values($state);
  
$tax_values array_values($tax);
  
$tax = array();
  foreach(
$state_values as $i => $state)
  {
    
$tax[$state] = $tax_values[$i];
  }
  
print_r($tax);
?>

You will see that you can then index $tax with the value of your $_POST["state"]. What you would want to do is check whether a value exists for the users' state, and then display it if so, otherwise default to "check site", for example:

<?php
  
if ($tax[$_POST["myState"]])
  {
    print 
$tax[$_POST["myState"]];
  }
  else
  {
    print 
"check site";
  }
?>

Hope this helps!
Cheers,
David.

Submitted by multiz on Thu, 2007-01-18 22:42

Thank you very much David,

It works great!

Michael

ZZPrices - Electronics Price Comparison Shopping