Jump to content

Table-Based Shipping


AdamB

Recommended Posts

Hello,

 

I am trying to create a cart script which offers table based shipping. Basically, the rates are predefined in a MySQL database and follow the format "maximumweight:price," - the fields with data look something like "1:2.00;2:4.00,4:8.00;8:16.00". So, orders up to 1KG cost £2 to ship, orders upto 2kg cost £4 to ship etc. I cant figure out how to get PHP to search through the "shipping data" field in the database. Each shipping method has its own row, and each shipping method has its own set of weights/prices based on the shipping company rates.

 

I know what I'm trying to do in words, but can't seem to get PHP to do it  ???

 

Any help would be much appreciated!

Link to comment
Share on other sites

If I understand you correctly, this should work:

 

<?php

// I assume you have two rows: method (air, fedex etc.) and cost (1:2.00,2:4.00 etc.)

// I also assume you have already connect to the database.

$method = "air";

$q = "SELECT * FROM shipping WHERE method ='".$method."'";
$r = mysql_query($q);
$list = mysql_fetch_array($r);

$arr = explode(",", $list);

foreach($arr as $value)
{
  $arr2 = explode(":", $value);
  echo $arr[0] . "kg costs $" . $arr2[1] . " to ship by " . $method . "<br>";
}

?>

Link to comment
Share on other sites

Initially it looks as though the array isn't "exploding" properly at this point:

 

$arr = explode(",", $list);

 

It reads into the first array (mysql_fetch_array) fine, but at the end returns "Arraykg costs $ to ship by "

Link to comment
Share on other sites

Managed to fix that problem:

 

$arr = explode(",", $list['shippingdata']);

 

Which makes a long list come out, which is a step forward, really what I wanted to try and do was say:

 

"10kg costs £10 to ship by Air"

"10kg costs £5 to ship by Fedex"

 

???

Link to comment
Share on other sites

Well if all you want is something like that (I assume 20kg costs 20 pounds by Air etc.) you can just have an array like this:

 

<?php

$costs = array("Air" => 1, "Fedex" => 0.5);
$kg = 10;
$method = "Air";

echo "It costs $" . $kg * $costs[$method] . " to ship " . $kg . "kg by " . $method;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.