AdamB Posted March 23, 2008 Share Posted March 23, 2008 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! Quote Link to comment Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Share Posted March 23, 2008 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
AdamB Posted March 23, 2008 Author Share Posted March 23, 2008 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 " Quote Link to comment Share on other sites More sharing options...
AdamB Posted March 23, 2008 Author Share Posted March 23, 2008 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" ??? Quote Link to comment Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Share Posted March 23, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.