and it also didn't work plus it stop the process half way meaning it got stuck right on the sim.php file but it doesn't show the payment process page.

So it does go across to the payment gateway - just fails to process?

also to explain listItems function better:

$input = array();//create array to store data
while($row = mysql_fetch_array($result)){//loop through all items in the cart
        $input[] = array($row['itemId'],$row['itemName'],$row['qty']);
        //add the data we want for each item into the array
}
//send the data needed in the listItems function to work out what the costs are
$cartdata = listItems($input,$_POST['country'],$sstat);

my listItems function is out of date from yours but should be similar enough to get the idea, that data is then passed to this function:

function listItems($input,$ship_country,$taxcode){
    //3 variables expected when you call listItems
    //$input = array - itemid, itemname & quantities to work out the costs of
    //$ship_country = string - the country worked out in shipping
    //$taxcode = string - the taxes to be applied to this cart

    //CONFIG ARRAY
    $foods = getFoodArray();//get item prices and which category the item is in
    $foodKeyArray = getFoodKeyArray();//dont need this anymore
     //VARIABLES
    $running = array('fruit'=>0,'vegetable'=>0);//define var running total of sales cost
    $output = array('fruit'=>'','vegetable'=>'');//define var html output
    $tax = array('fruit'=>0,'vegetable'=>0);//define var tax
    //LOOP THROUGH INPUT
    foreach($input as $item){//loop through the items in $input (the cart)
        $key = $item[0];//set $key = first value in array (the itemid)
        $qty = $item[1];//set $qty = second value in array (the quantity)
        if(isset($foods['fruit']['items'][$key]) || isset($foods['vegetable']['items'][$key])){
            //if itemid exists in $foods
            $type = (isset($foods['fruit']['items'][$key])) ? 'fruit' : 'vegetable';
            //if exists in fruit, type is fruit else it is a vegetable
            $unit = $foods[$type]['items'][$key];
            //$unit = the value set in the getFoodArray() function
            /*from getFoodArray() -> $foods[$row['cat']]['items'][$row['itemId']] = $row['some_number'];*/
            $cost = number_format($qty * $unit,2);
            //$cost = the quantity of the item times $unit
            $output[$type] .= "<tr><td>$foodKeyArray[$key]</td><td>$qty</td><td>$unit</td><td class=\"right\">$cost</td></tr>";
            //add html row to display in the cart
            $running[$type] += $cost;//$add the cost of this item*quantity to the running total of this type(fruit or veg)
        }
    }
    //loop finished, all the items costs added to running total
    //DO TOTALS FOR OUTPUT
    //1) Tax
    $tax['fruit'] = getTax($taxcode, 'Fruit', $running['fruit']);
    //get the tax for the total fruit cost
    $fruit_tax_label = (($tax['fruit']/$running['fruit'])*100).'%';
    //work out the tax % applied to the fruit

    $tax['vegetable'] = getTax($taxcode, 'vegetable', $running['vegetable']);
    //get the tax for the total veg cost
    $vegetable_tax_label = (($tax['vegetable']/$running['vegetable'])*100).'%';
    //work out the tax % applied to the veg

    $total_tax = $tax['fruit'] + $tax['vegetable'];
    //$total_tax = fruit tax + veg tax
    //2) Sales Total
    $total_sales = $running['vegetable'] + $running['fruit'];
    //$total_sales = total beg cost + total fruit cost (no tax)
    //3) Shipping
    //use $ship_country and $total_sales to work out the shipping
    $domestic = array('United States');
    if(in_array($ship_country,$domestic)){
        //----------Domestic Shipping--------------//
        if ($total_sales > 0 && $total_sales <= 100){$ship = 6.50;}
        if ($total_sales > 100 && $total_sales <= 300){$ship = 10.50;}
        if ($total_sales > 300 && $total_sales <= 500){$ship = 14.00;}
        if ($total_sales > 500 && $total_sales <= 700){$ship = 18.00;}
        if ($total_sales > 700 && $total_sales <= 1000){$ship = 20.00;}
        if ($total_sales > 1000){$ship = 25.00;}
    }else{
        //----------International Shipping--------------//
        if ($total_sales > 0 && $total_sales <= 100){$ship = 30.00;}
        if ($total_sales > 100 && $total_sales <= 300){$ship = 35.00;}
        if ($total_sales > 300 && $total_sales <= 500){$ship = 40.00;}
        if ($total_sales > 500 && $total_sales <= 700){$ship = 45.00;}
        if ($total_sales > 700 && $total_sales <= 1000){$ship = 50.00;}
        if ($total_sales > 1000){$ship = 50.00;}
    }
    //4) Bill Total
    $total_bill = $total_sales + $total_tax + $ship;
    //$total_bill = $total_sales + $total_tax + $ship
    //BUILD TOTAL HTML
    //display this worked out data in a html table
    $total =
    "<tr><td colspan=\"4\"><hr /></td></tr>
    <tr><td colspan=\"3\">Total Fruits</td><td class=\"right\">" . number_format($running['fruit'], 2) . "</td></tr>
    <tr><td colspan=\"3\">Total Vegetables</td><td class=\"right\">" . number_format($running['vegetable'],2) . "</td></tr>
    <tr><td colspan=\"3\">Total Sales</td><td class=\"right\">" . number_format($total_sales,2) . "</td></tr>
    <tr><td colspan=\"3\">Shipping Cost</td><td class=\"right\">" . number_format($ship,2) . "</td></tr>

    <tr><td colspan=\"4\"><hr /></td></tr>
    <tr><td colspan=\"3\">Fruit Tax @ $fruit_tax_label</td><td class=\"right\">" . number_format($tax['fruit'], 2) . "</td></tr>
    <tr><td colspan=\"3\">Vegetable Tax @ $vegetable_tax_label</td><td class=\"right\">" . number_format($tax['vegetable'], 2) . "</td></tr>
    <tr><td colspan=\"3\">Total Tax</td><td class=\"right\">" . number_format($total_tax, 2) . "</td></tr>
    <tr><td colspan=\"4\"><hr /></td></tr>
    <tr><td colspan=\"3\">Total Bill</td><td class=\"right\">" . number_format($total_bill, 2) . "</td></tr>";

    $returndata = array();create array to return data to the script that called it
    $returndata['html'] = "<tr><th>ITEM</th><th>QTY</th><th>UNIT/\$</th><th>COST/\$</th></tr>" . $output['fruit'] . $output['vegetable'] . $total;
    //$returndata['html'] = the html to show the contents of the cart worked out previously
    $returndata['veg_tax_label'] = $vegetable_tax_label;
    //the tax % for veg
    $returndata['fruit_tax_label'] = $fruit_tax_label;
    //the tax % for fruit
    $returndata['shipping'] = $ship;
    //$returndata['shipping'] = the shipping for this cart, as worked out above
    $returndata['totaltax'] = $total_tax;
    //$returndata['totaltax'] = the tax for this cart, as worked out above
    $returndata['totalsales'] = $total_sales;
    //$returndata['totalsales'] = the sales cost for this cart(no tax), as worked out above
    $returndata['totalbill'] = $total_bill;
    //$returndata['totalbill'] = the sales cost + tax + shipping for this cart, as worked out above
    return $returndata;//return the array $returndata for use by the calling script
}

now we are back into checkout2.php

$cartdata = listItems($input,$_POST['country'],$sstat);
//$cartdata = $returndata from the function, eg. $cartdata['totalbill'] == $returndata['totalbill']
$totalCost += $cartdata['totalsales'];
//$totalCost = the total sales cost of the cart
$ship = $cartdata['ship'];
//$ship = the shipping cost of the cart
$taxx = $cartdata['taxx'];
//$taxx = the total tax of the cart
//----------store important values in session--------------//
$_SESSION['tcost'] = $totalCost;//store it in the session
$_SESSION['ship'] = $ship;//store it in the session
$_SESSION['taxx'] = $taxx;//store it in the session
//----------store important values in session--------------//
echo $cartdata['html'];//print out the html to show the cart

as you can see it is doing the same thing yours did, the only question i have is has this line been corrected in getFoodArray() $foods[$row['cat']]['items'][$row['itemId']] = $row['some_number'];

but when i asked you this bit earlier:

This what it dump, it's duplicated

 string(36) "SELECT * from orders where id = 2255"
 array(46) {
   [0]=>
   string(4) "2255"
   ["id"]=>
   string(4) "2255"
   [1]=>
   string(4) "3348"
   ["cust_id"]=>
   string(4) "3348"
   [2]=>
   NULL
   ["user"]=>
   NULL
   [3]=>
   string(6) "186.50"
   ["cart_total"]=>
   string(6) "186.50"
   [4]=>
   string(4) "10.5"
   ["shipping"]=>
   string(4) "10.5"
   [5]=>
   string(2) "no"
   ["express"]=>
   string(2) "no"
   [6]=>
   string(8) "Jonny"
   ["s_first"]=>
   string(8) "Jonny"
   [7]=>
   string(7) "LAst"
   ["s_last"]=>
   string(7) "LAst"

i saw the cart_total is being saved OK so there isn't anything wrong on checkout2.php, it's doing exactly what its supposed to(though you didnt post everything i assume its in), the error must be further along if it contains all the data required for authorise.net to process an order.

btw the duplication comes from using mysql_fetch_array(), it returns the data as a number and as the column name, if you just want the column name use mysql_fetch_assoc() or just numbers use mysql_fetch_array($res,MYSQL_NUM);

LastMitch commented: Thanks for explaining the similarity of the script +0
Member Avatar for LastMitch

Hi Biiim,

So it does go across to the payment gateway - just fails to process? Yes

When I switch the

<body onLoad="document.form1.submit()">

to

<body onLoad="document.getElementById('form1').submit()">

and click "Place Order"

it didn't stalled instead

It was trying to go across to the payment gateway, the page was blank or plain white (it didn't have data on the page) - just fails to process to the payment page.

I didn't used this code:

<?php
    function getFoodArray(){
    $query = "SELECT * from items";//pulls all data in the items table
    $result = mysql_query($query) or die(mysql_error());//run query on the mysql server, end the script and show the error message on error
    if($result !== false){//if a mysql query errors or pulls no data it returns false
    //$result has some data
    $foods = array();//declare empty $foods array
    while($row = mysql_fetch_assoc($result)){//loop through every item pulled from mysql table, $row contains all the data for each item and is an array
    $foods[$row['cat']]['items'][$row['itemId']] = $row['some_number'];
    //tell it to insert each item into the foods array
    //first by category, into items, then referenced by the itemId
    //eg. $foods['fruit']['items']['A-C01'] = 35;
    }
    }else{
    //$result has no data
    $foods = false;
    }
    return $foods;//send the $foods variable back as the result of the function
    }
?>

I went back to this:

<?php
function getFoodArray(){
$foods = array(
"fruit"=> array("items"=>array(
//Fruit--------------------
'A-C01'=>35, 'A-C02'=>50, 'A-C03'=>75,
'A-G01'=>35, 'A-G02'=>50, 'A-G03'=>75,
'G-W01'=>20, 'G-W02'=>30, 'G-W03'=>50,
),"tax_ex_limit"=> 30, "tax_ex_rate"=>4.5, "tax_rate"=>8.75),
"vegetable"=> array("items"=>array(
//Vegetable--------------------
'A-A01'=>35, 'A-A02'=>55, 'A-A03'=>80,
'B-B01'=>35, 'B-B02'=>55, 'B-B03'=>80,
'C-A01'=>35, 'C-C02'=>55, 'C-C03'=>80,
),"tax_ex_limit"=> false, "tax_ex_rate"=>0, "tax_rate"=>8.75));
return $foods;
}

the error must be further along if it contains all the data required for authorise.net to process an order.

I don't know how further it is? is it the 'sim.php'?

If I put put your code with the arrays will it work?

 <?php
        function getFoodArray(){
        $query = "SELECT * from items";
        $result = mysql_query($query) or die(mysql_error());
        if($result !== false){
        $foods = array(
       "fruit"=> array("items"=>array(
        //Fruit--------------------
       'A-C01'=>35, 'A-C02'=>50, 'A-C03'=>75,
       'A-G01'=>35, 'A-G02'=>50, 'A-G03'=>75,
       'G-W01'=>20, 'G-W02'=>30, 'G-W03'=>50,
       ),"tax_ex_limit"=> 30, "tax_ex_rate"=>4.5, "tax_rate"=>8.75),
      "vegetable"=> array("items"=>array(
       //Vegetable--------------------
       'A-A01'=>35, 'A-A02'=>55, 'A-A03'=>80,
       'B-B01'=>35, 'B-B02'=>55, 'B-B03'=>80,
       'C-A01'=>35, 'C-C02'=>55, 'C-C03'=>80,
      ),"tax_ex_limit"=> false, "tax_ex_rate"=>0, "tax_rate"=>8.75));
        while($row = mysql_fetch_assoc($result)){
        $foods[$row['cat']]['items'][$row['itemId']] = $row['some_number'];
        }
        }else{
        //$result has no data
        $foods = false;
        }
        return $foods;
        }
    ?>

Right now, I'm lost. You explained very clearly and still I can't pinpoint the problem. I really appreciated that you taking your time to explained things and help me learn. This thread I learn alot about arrays and if & else staements.

I appreciated help and your insight again.

It was trying to go across to the payment gateway, the page was blank or plain white (it didn't have data on the page) - just fails to process to the payment page.

It sounds like the form is submiting then, thats not the problem i would just leave it as it is:

<body onLoad="document.form1.submit()">

Have you not checked if the merchant account you are using has some issues, has this sim.php ever worked?

you could check your code with the code on their site: https://developer.authorize.net/integration/fifteenminutes/#hosted

This page i got from there will replace sim.php:

<?php
session_start();
require_once 'anet_php_sdk/AuthorizeNet.php'; // Include the SDK you downloaded in Step 2
$api_login_id = 'YOUR_API_LOGIN_ID';
$transaction_key = 'YOUR_TRANSACTION_KEY';
$amount = $_SESSION['tcost']+$_SESSION['ship']+$_SESSION['taxx'];
$fp_timestamp = time();
$fp_sequence = $_SESSION['oid']; // Enter an invoice or other unique number.
$fingerprint = AuthorizeNetSIM_Form::getFingerprint($api_login_id,
  $transaction_key, $amount, $fp_sequence, $fp_timestamp)
?>

<form method='post' action="https://test.authorize.net/gateway/transact.dll">
<input type='hidden' name="x_login" value="<?php echo $api_login_id?>" />
<input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" />
<input type='hidden' name="x_amount" value="<?php echo $amount?>" />
<input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" />
<input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" />
<input type='hidden' name="x_version" value="3.1">
<input type='hidden' name="x_show_form" value="payment_form">
<input type='hidden' name="x_test_request" value="false" />
<input type='hidden' name="x_method" value="cc">
<input type='submit' value="Click here for the secure payment form">
</form>

Remember to download the file from their site in step 2

commented: Thanks for the info +0
Member Avatar for LastMitch

Hi Biiim,

Thanks, I will look over it and match the code I have to theirs again.

Member Avatar for LastMitch

Hi Biiim,

Have you not checked if the merchant account you are using has some issues, has this sim.php ever worked?

Yes, the merchant account is working.

I download the file and put it on my server and put the new sim.php file that you created.

It's doing the same thing, it won't redirect to payment page:

checkout2.php(process the order then redirect to payment page) -> sim.php

I will try to modify the changes from the new sim.php to my script and test it out again.

Is it possilbe that the

<body onLoad="document.form1.submit()">

is not loading the form?

How do I create a new

<body onLoad="document.form1.submit()">

?

I'm not familiar other types onLoad function

When I used the one you created:

<body onLoad="document.getElementById('form1').submit()">

When I click "Place Order" button, it didn't stalled instead it was trying to go across to the payment gateway.

The page was blank or plain white (it didn't have data on the page) - just fails to process to the payment page.

So you're right about the "Place Order" button actually works fine in the checkout2.php file

So it's the 'sim.php' file after all.

As you can see the 'sim.php' file is the same file I used for my old checkout2.php but won't work for the new checkout2.php

I will modify the changes of the new 'sim.php' file that you created to my old and test it out again.

If that doesn't work, then what should I do next?

I'll work on it over the weekend and let you know if I access it or not.

I appreciated help and your insight again.

Member Avatar for LastMitch

Hi Biiim,

I got it working now! The script is working now. I test the it out on PayPal, then AlertPay and I finaly Autherized.Net and it works fine.

For Autherized.net, I figure out that I need have a good eye and to be more organized like you!

In the sim.php on my computer. I have 5 different kind because I make changes and I put the wrong sim.php on the server

The one I put on the server in the file

I typo:

$amunt = $x_Amount; 

it should be:

$amount = $x_Amount; 

and this

<body onLoad="document.form1.submit()"> 

it should be

<body onLoad="document.form.submit()"> 

that's why it didn't go through at all.

Now I have some minor issues.

When I add a item to the cart and I click go back shopping and I add another item to the cart and go back to the main page and I click 'View Cart' again to see the 2 items. Now I click 'Checkout' and fill out form and click "Next" I see those 2 items appear 3 times (total 6 items, it duplicate twice)? But the total cost is the same just the 2 items and the shipping and taxes are fine.

How can I fixed the duplicates not to appear on the final checkout sheet?

I'm actually done but I didn't expect some minor issues like that one. I will test it out more to see if there's any other issue but so far everything is good for now. The calucation is working! If there any other issue I'll post it.

Thanks for being a good mentor!

I appreciated help and your insight again!

REPROGRAM EVERYTHING AND USE DATABASE TABLE FOR CONDITIONS VALUES

Member Avatar for LastMitch

@urtrivedi

Thanks for posting suggestion on this thread again. I don't want to be rude. But I'm close to be done with this project and Biiim is helping me out with this.

It was Ardav idea but he left me the array method and didn't explain the code but do get credit for the idea and without the idea this thread wouldn't last this long.

I kept focusing on the array method and Biiim to used the method and explain the code clearly to me to understand how it works. He also created a few scripts to modify to the script I have and rewrote a few scripts base on the array method too, to adjust to the script I have. Biiim did a lot work and I appreciated that he taking his time to help me out. Biiim is a good mentor!

Ardav is right!

Like Ardav said

Biiim deserve a lot of credit for his hard work and a lot a repect for being patience and helpful to resolve this issue.

It's paying off because I'm close to be done! That's all it matters.

It's like Biiim said

work what you have.

commented: politely put :) +14
Member Avatar for diafol

Well done guys, you've pushed this thread to almost a 100 posts! And judging by the length of some of these posts, Dani will have to buy a new server! There's nothing like getting your teeth into a problem, but as I and now utrivedi have stated, a DB solution would have been a lot less hassle. However, your static array method should be a work of art once you get it working properly. :)

Member Avatar for LastMitch

@ardav

I can't believe it's close a 100 post either. I'm very close to be done. Thanks for the code!

It's not a question of where ardav grips it! It's a simple question of weight ratios! A five ounce ardav could not carry a one pound tomato!

-

Listen. In order to maintain air-speed velocity, ardav needs to beat itself forty-three times every second, right?

-

Only forty-three times per second?

= )

Member Avatar for LastMitch

@Biiim

I'm letting you the script is working and the calucation is working fine!

I have two more minor issue left:

Here is the First:

When I add a item to the 'Shopping Cart' and I click 'Go Back Shopping' and I add another item to the 'Shopping Cart' and go back to the main page and I click 'View Cart' again to see the 2 items. Now I click 'Checkout' and fill out form and click "Next" I see those 2 items appear 3 times (total 6 items, it duplicate twice)? But the 'Total Cost' is the same just the 2 items and the 'Shipping' and 'taxes' are fine.

How do I fixed the duplicate issue?

Here is the code from the functions.php I think it might be the issue but I'm not sure:

function listItems($input,$ship_country){

//CONFIG ARRAY
$foods = getFoodArray();

//VARIABLES
$running = array('fruit'=>0,'vegetable'=>0);
$output = array('fruit'=>'','vegetable'=>'');
$tax = array('fruit'=>0,'vegetable'=>0);

//LOOP THROUGH INPUT
foreach($input as $item){$key = $item[0];$qty = $item[1];
if(isset($foods['fruit']['items'][$key]) || isset($foods['vegetable']['items'][$key])){
$type = (isset($foods['fruit']['items'][$key])) ? 'fruit' : 'vegetable';
$unit = $foods[$type]['items'][$key];
$totalCost = number_format($qty * $price,2);
$output[$type] .= "<tr><td>$key</td><td>$name</td><td>$qty</td><td class=\"right\">$$totalCost</td></tr>";
$running[$type] += $totalCost;
}
}

$returndata = array();
$returndata['html'] = "" . $output['fruit'] . $output['vegetable'];
$returndata['ship'] = $ship;
$returndata['taxx'] = $taxx;
$returndata['totalsales'] = $total_sales;
$returndata['totalbill'] = $total_bill;
return $returndata;

Here is the code from Checkout2.php

if (mysql_num_rows($result)==0){echo mysql_num_rows($result);}

$_SESSION["mthis"] ="";
      $input = array();

while($row = mysql_fetch_array($result)){
      $input[] = array($row['color'],$row['itemName'],$row['size'],$row['qty']);
      $cartdata = listItems($input,$_SESSION['country'],$sstat); 
      $totalCost = $cartdata['totalsales'];
      $ship = $cartdata['ship'];//shipping cost
      $taxx = $cartdata['taxx'];//tax
    //----------store important values in session--------------//
    $_SESSION['tcost'] = $totalCost;
    $_SESSION['ship'] = $ship;
    $_SESSION['taxx'] = $taxx;
    //----------store important values in session--------------//
    echo $cartdata['html'];
}

I appreciated help and your insight again!

Member Avatar for LastMitch

@Biiim

Here is the Second:

The "Express Shipping" is $20 dollars and the "Gift Wrap" is $5 dollars.

When I check "Express Shipping" the $20 dollars added to the Total Cost.

When I check "Gift Wrap" the $5 dollars added to the Total Cost.

This issue is when I check "Express Shipping" and then "Gift Wrap" it should be $25 dollars.

But on the Total Cost it only show $20 dollars instead $25 dollars.

Here is the code from Checkout2.php

<script language="javascript">
function calcFees(obj,type){if (obj.checked){
document.getElementById("stotal").innerHTML = formatCurrency(<?php echo $totalCost;?> + parseInt(obj.value));
}else{
document.getElementById("stotal").innerHTML = formatCurrency(<?php echo $totalCost;?>);}}
</script>

EXPRESS DELIVERY:

<input name="express" type="checkbox" id="express" onClick="calcFees(this);" value="20"/>

GIFT WRAP ($5):

<input name="gift_wrap" type="checkbox" id="gift_wrap" onClick="calcFees(this);" value="5"/>

TOTAL:

<?php echo number_format($totalCost + $ship + $taxx, 2, ".", ","); ?>

I appreciated help and your insight again!

Sorry for disappearing over the weekend.

I'm glad its started working now
i didnt quite think that gift wrap/express cost through did i - heres something that should do it:

<script type="text/javascript">
var giftBool = false;
var expressBool = false;
var cartTotal = <?php echo $totalCost;?>;
var displayCost = 0;
function calcFees(type){
    var giftObj = document.getElementById('gift_wrap');
    var expressObj = document.getElementById('express');
    if(type == 'gift'){
        if(giftBool){
            giftBool = false;
        }else{
            giftBool = true;
        }
    }else{
        if(expressBool){
            expressBool = false;
        }else{
            expressBool = true;
        }
    }
    displayCost = cartTotal;
    if(expressBool){
        displayCost = displayCost + 20;
    }
    if(giftBool){
        displayCost = displayCost + 5;
    }
    document.getElementById("stotal").innerHTML = formatCurrency(displayCost);
}
</script>

inputs:

<input name="express" type="checkbox" id="express" onClick="calcFees('express');" value="20"/>
<input name="gift_wrap" type="checkbox" id="gift_wrap" onClick="calcFees('gift');" value="5"/>

The duplicate items in the cart is a bit worrying, if it was doubling the items it would make sense but triple?

while($row = mysql_fetch_array($result)){
  $input[] = array($row['color'],$row['itemName'],$row['size'],$row['qty']);

update to:

while($row = mysql_fetch_assoc($result)){
    $input[] = array($row['itemId'],$row['itemName'],$row['qty']);
}

that would make each item repeat itself, mysql_fetch_array() pulls each row as its name and as an integar(eg. $row[0] & $row['itemId']).

Maybe its cause you dont close the while loop where you should, actually looking at it i think it is, only the 1 $input line should be in the while loop or its going to run all of it for each item:

while($row = mysql_fetch_array($result)){
      $input[] = array($row['color'],$row['itemName'],$row['size'],$row['qty']);
      $cartdata = listItems($input,$_SESSION['country'],$sstat); 
      $totalCost = $cartdata['totalsales'];
      $ship = $cartdata['ship'];//shipping cost
      $taxx = $cartdata['taxx'];//tax
    //----------store important values in session--------------//
    $_SESSION['tcost'] = $totalCost;
    $_SESSION['ship'] = $ship;
    $_SESSION['taxx'] = $taxx;
    //----------store important values in session--------------//
    echo $cartdata['html'];
}

to:

while($row = mysql_fetch_array($result)){
      $input[] = array($row['color'],$row['itemName'],$row['size'],$row['qty']);
}
$cartdata = listItems($input,$_SESSION['country'],$sstat); 
$totalCost = $cartdata['totalsales'];
$ship = $cartdata['ship'];//shipping cost
$taxx = $cartdata['taxx'];//tax
//----------store important values in session--------------//
$_SESSION['tcost'] = $totalCost;
$_SESSION['ship'] = $ship;
$_SESSION['taxx'] = $taxx;
//----------store important values in session--------------//
echo $cartdata['html'];

It was Ardav idea but he left me the array method and didn't explain the code but do get credit for the idea and without the idea this thread wouldn't last this long.

I jumped in too quick for him!

a DB solution would have been a lot less hassle. However, your static array method should be a work of art once you get it working properly. :)

I think the food data is from the database now - at least my copy is. I didnt fancy the challenge of getting a tax table setup, i think that would be his next step once hes got it working and had time to relax haha

Member Avatar for LastMitch

Hi Biiim,

It's the weekend, you didn't disapear. I understand.

Thanks for the update code.

I test out the code, you help me fixed the duplicate issue. The duplicate is gone!

Thanks again!

I'll test out the gift wrap/express now

Member Avatar for LastMitch

Hi Biiim,

The gift wrap/express didn't work. It's not adding to the together. But it's adding to the Total Cost separately.

It's like

Express: $20 - Giftwrap: $5 = $20 + Total Cost

Express: $20 + Giftwrap: $5 = $25 + Total Cost <-- This is the correct one I'm trouble creating that formula

My old code that I did does the samething I been trying to fixed that.

I like your code alot:

<script language="javascript">
function calcFees(obj,type){if (obj.checked){
document.getElementById("stotal").innerHTML = formatCurrency(<?php echo $totalCost;?> + parseInt(obj.value));
}else{
document.getElementById("stotal").innerHTML = formatCurrency(<?php echo $totalCost;?>);}}
</script>

I appreciated help and your insight again!

did the javascript i wrote above not work?

I just tested it and the code i wrote above worked fine for me, it's a lot more complex but means you can add other costs to it quite easily as well.

In the old one the 2 input's (gift & express) were completely unaware of each other, so when you check either gifts or express, it has no idea if the other is checked or not.

var giftBool = false;
var expressBool = false;
var cartTotal = <?php echo $totalCost;?>;
var displayCost = 0;

The 2 bool vars set express & gifts to "off" (you can set these true for on but make sure the input box is checked or the order will be submitted wrong)

cartTotal is the value of the cart with no express or gifts, never change that

displayCost is the variable to hold the cost to show, it works out whether to add the gift & express to the original cart cost and displays that value

if(expressBool){
    displayCost = displayCost + 20;
}
if(giftBool){
    displayCost = displayCost + 5;
}

That adds the cost to the cartTotal if the box is checked, change the numbers if you want to change the price

Member Avatar for LastMitch

Hi Biiim,

No, your javascript works! I just like how origanized! The one I created is what you explained:

In the old one the 2 input's (gift & express) were completely unaware of each other, so when you check either gifts or express, it has no idea if the other is checked or not.

You're right! I run the test again. I think I need to take a closer look to understand how it works.

I'll get back to you when I'm done.

Member Avatar for LastMitch

Hi Biiim,

I got it working!

The express & gifts script that you wrote it great! I don't know how to you write these scripts but it works!

Express: $20 + Giftwrap: $5 = $25 + Total Cost <-- It's working and it's correct now!

This is mines:

function calcFees(chk,cost){
if (chk.checked == 1){document.getElementById("stotal").innerHTML = formatCurrency(cost + 15);
}else{document.getElementById("stotal").innerHTML = formatCurrency(cost);}}

-

function calcFees1(chk,cost){
if (chk.checked == 1){document.getElementById("stotal").innerHTML = formatCurrency(cost + 5);
}else{document.getElementById("stotal").innerHTML = formatCurrency(cost);}}

In the old one the 2 input's (gift & express) were completely unaware of each other, so when you check either gifts or express, it has no idea if the other is checked or not.

It's true! If I check either gifts or express, it has no idea if the other is checked or not.

Member Avatar for LastMitch

Hi Biiim,

I have a tax question, the tax I have is working but I want to know how your tax script works because it's if & else statement . The tax I have doesn't fit well with the fckeditor because it's hard to modfiy it unless it's on a editor. I try to run it and it won't response. I have a fckeditor in the admin section I think your code fits well with the fckeditor if I was to create a page that let me enter my own numbers (ie 5%) and see the code works.

function getTax($taxcode,$cat,$amount){

if((!ctype_digit($amount) && !is_int($amount)) || $amount < 0){

//not a number or less than 0, exit
return false;}

if($amount == 0){//return 0 tax if no value
return 0;}

if($taxcode == 'CT'){//Crown Colony, tell it how to work out tax for this state

if($cat == 'Fruit'){//if crown colony and fruit

if($amount > 50){//if crown colony and fruit value over $50

$tax = round($amount * .0635,2);

}else{//crown colony and fruit under $50

$tax = 0;}

}elseif($cat == 'vegetable'){//crown colony vegetables

//Gifts containing candy, cookies, nuts, or any other items besides fruit & vegetables are subject to 6.35% sales tax (not 8.75%).

//means vegetables are full 8.75% tax?
$tax = round($amount * .0875,2);

}else{//unknown category in crown colony

$tax = round($amount * .0875,2);}

}else{//any unspecified taxcode, apply a general tax rate

$tax = round($amount * .0875,2);}

return $tax;}

I appreciated help and your insight again!

Member Avatar for LastMitch

You mention that

I think ill have to separate the tax out of ardav's function now cause its not really been written in mind to having so many variables on the tax.

Can it still function even it have so many variables on the tax?

I mean it's working fine. I am just curious that's all.

If the array method is working what is the drawback?

Is it data entry?

Can it still function even it have so many variables on the tax?

I mean it's working fine. I am just curious that's all.

If the array method is working what is the drawback?

Is it data entry?

Yeah its just data entry really, it can also get so big if you have many different if's and else's that you can easily make errors, and some changes may need to be done where you have to update ALL of the entries.

It's actually more stable being in an array, it needs no database connection it's just the managing it and updating things in it.

If you need to add a few more different tax codes(5+) i would make the jump to a mysql database for it, i've not actually done anything tax based for a website but could work it out

I have a tax question, the tax I have is working but I want to know how your tax script works because it's if & else statement . The tax I have doesn't fit well with the fckeditor because it's hard to modfiy it unless it's on a editor. I try to run it and it won't response. I have a fckeditor in the admin section I think your code fits well with the fckeditor if I was to create a page that let me enter my own numbers (ie 5%) and see the code works.

I thought the comments explained what was going on quite well. Not too sure what fckeditor is, do you not use ftp to your website? I started in frontpage with html then started to use notepad once i started using php - until i found zend.

The idea of the tax function is to handle working out the tax and return it, so from the calling scripts viewpoint, it just needs :

$tax = getTax($taxcode,$cat,$amount);

And it now has the tax to carry on.

The contents of the function was better to understand when i first posted it(formatting), basically the first check is to find:

  1. which tax code they are in
  2. find out if its fruit or veg (if needed)
  3. then the cost of it (if needed)

I do it like that cause it's not hard to upgrade it into a mysql database at a later date, not thought it through properly but you could just make a mysql table with the columns "taxcode","category","min_amount","max_amount","tax_rate"

mysql code:

CREATE TABLE `tax_table`(     `id` INT(5) NOT NULL AUTO_INCREMENT ,     `taxcode` VARCHAR(3) ,     `category` VARCHAR(20) ,     `min_amount` DECIMAL(7,2) ,     `max_amount` DECIMAL(7,2) ,     `tax_rate` DECIMAL(5,3) ,     PRIMARY KEY (`id`)  );
ALTER TABLE `tax_table` ADD INDEX `taxcode` (`taxcode`);
ALTER TABLE `tax_table` ADD INDEX `NewIndex1` (`max_amount`);
ALTER TABLE `tax_table` ADD INDEX `NewIndex2` (`min_amount`);
ALTER TABLE `tax_table` ADD INDEX `NewIndex3` (`category`);
INSERT INTO `tax_table`(`taxcode`,`category`,`min_amount`,`max_amount`,`tax_rate`) 
VALUES ('CT','fruit','0','50','0')
,('CT','Fruit','50','99999','0.625')
,('CT','vegetable','0','99999','0.875')
,('CT','','0','99999','0.875')
,('','','0','99999','0.875');

then add a updated function to replace the the old tax function:

function getTaxDB($taxcode,$cat,$amount){
    if((!ctype_digit($amount) && !is_int($amount)) || $amount < 0){
        //not a number or less than 0, exit
        return false;
    }
    if($amount == 0){//return 0 tax if no value
        return 0;
    }
    $Q = "SELECT `tax_rate` FROM `tax_table` WHERE `taxcode` = '{$taxcode}' AND `category` = '{$cat}' AND `min_amount` <= {$amount} AND `max_amount` > {$amount}";
    $R = mysql_query($Q);
    if($R !== false){
        $data = mysql_fetch_assoc($R);
        $tax = round($amount*$data['tax_rate'],2);
    }else{
        $tax = false;
        //$tax = round($amount*0.0875,2);//default tax if not in DB?
    }
    return $tax;
}

There might be a better way to store the tax data but i've never done it before so not experienced it

commented: Thanks for Clarifying the Array & Taxes! Thanks for helping me solve my issue! +2
Member Avatar for LastMitch

@Biiim,

I just want to ‘Thank You’ for helping me to understand in depth about “Arrays” & “if & else” statement.

I appreciated that you taking you time to write those script so I can understand it!

I learn a lot from you. You are Good Mentor! Thanks!

Like Ardav said

Biiim deserve a lot of credit for his hard work and a lot a repect for being patience and helpful to resolve this issue.

Like Ardav said

All power to you!

I thought maybe Ardav can take you go the pub and buy you a drink!

But when he mention something like this:

‘You guys need to get a room :)’

My first thought is I didn’t know Ardav is interested those kinda of stuff?

But anyway…

You deserve a Victoria Cross!

Only the Queen can honor you this prestigious awards.

So Dani ‘aka 'The Queen’ will give you the ‘Victoria Cross’ for all your hard work to resolve this thread!

You are now recognizes as a noble member of DaniWeb for being Humble and Understanding and of course, Patience!

As for Ardav, not much to say about beside he got promote to be a Moderator and left me hanging with the array and also receive a bag of ‘Holy Tomato of Antioch' countesy from DaniWeb!

Member Avatar for LastMitch

@ Ardav

Hope you enjoy this post:

Ardav: Mitch, you understand how the arrays works now.? It took you that long time to understand an ‘array’?

Mitch: Ardav, I did learn a lot about ‘array' and 'if & else' statement. Biiim is a Good Mentor!

Ardav: OK, All power to him, then can you write one now?

Mitch: Sure, here it is:

<?php 
function getArdavArray(){
$great = array(

$1 = array("item"=>array('Ardav thinks it’s too small’=> o,), 

$2 = array("item"=>array('Ardav thinks it’s perfect fit’=> O,),

return $great;}

$great = getArdavArray();

$running = array('1'=>0,'2'=>0);
$output = array('1'=>'','2'=>'');

foreach($input as $item){
$key = $item[0];
$qty = $item[1];

if(isset($great ['1']['item'][$key]) || isset($great ['2']['item'][$key])){
$type = (isset($great ['1']['item'][$key])) ? '1' : '2';

$unit = $great[$type]['item'][$key];

$output[$type] .= "<tr><td>$key</td><td>$unit</td></tr>";}}

$input = array(array('Ardav thinks it’s too small’=> o), 
               array('Ardav thinks it’s perfect fit’=> O));
 ?>

Ardav: Nice work! I’m gonna test it now.

Mitch: OK

Ardav: I’ll pick $1

Mitch: OK

Mitch: 'Ardav thinks it’s too small=> o?

Mitch: Ardav did I compile correctly?

Ardav: Yeah, but there’s a little issue?

Mitch:What is it?

Ardav: Mitch take look here:

Mitch: OK

Ardav: Do you see this integer ‘o’.

Mitch: OK

Ardav:Here is my array ‘=>’.

Mitch:?

Ardav:My array ‘=>’ can’t fit into that integer ‘o’ because it’s a bit to small!

Mitch: OK?

Ardav:That’s good Mitch you got the script working correctly!

Mitch: OK?

Ardav:So you got only 2 integer in the script?

Mitch:Yes!

Ardav:I’m gonna to test out the second!

Mitch: OK!

Ardav: I’ll pick $2.

Mitch: OK

Mitch: 'Ardav thinks it’s perfect fit=> O?

Mitch: Ardav did I compile correctly?

Ardav: Yeah, it’s perfect?

Mitch: Why?

Ardav: Mitch take look here:

Mitch: OK

Ardav: Do you see this integer ‘O’.

Mitch: OK

Ardav:Here is my array ‘=>’ again.

Mitch: ?

Ardav:My array ‘=>’ fits perfectly with this ’O

Mitch: OK?

Ardav: You did a good work on creating these scripts!

Mitch:Thanks!

Ardav:Let me tell you something, my favorite sports is Rugby.

Mitch: OK

Ardav:Beside Rugby my second sport I love is Squash!

Mitch:Is that a Fruit or Vegetable?

Ardav:What is wrong with you? I think this thread make you mad!

Ardav: Squash is a sport.

Mitch:Really?

Ardav:At first it hurts a bit but after a while it feels good!

Mitch:You know you’re like an expert in the “array

Ardav:Well, yes I am! =)

Mitch:I never knew your “array” is so popular among DaniWeb members.

Ardav:Of course, Mitch that why I got 1,156 Reputation Points .

Mitch:You actually keep count?

Ardav:Yeah.

Mitch:Wow but anyway, ArdavThanks for the Idea”! =)

@LastMitch

Thanks for the generous praising! I hope Ardav doesn't take too much offence from your post haha, i think he had just left on holiday soon after the thread started

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.