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
https://forums.phpfreaks.com/topic/97470-table-based-shipping/
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
https://forums.phpfreaks.com/topic/97470-table-based-shipping/#findComment-498715
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
https://forums.phpfreaks.com/topic/97470-table-based-shipping/#findComment-498744
Share on other sites

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.