I wrote a code for one of my clients that calulates the distance from thier house to the clients house. If its over 35 miles away it charges 50 cents a mile. I need someone to make it so that if the address is invalid or mapquest cant find it, that it echos an error. Here is the script::
| Code: |
|
<?php // (c)2007 Aaron Velazquez "warallthetm" //This script may not be used without express permission of Aaron Velazquez "warallthetm" //Calculator class that calculates driving distance via mapquest $address = $_GET["address"]; $zip = $_GET["zip"]; $url = "http://www.mapquest.com/directions/main.adp?go=1&do=nw&1a=28+pewter+circle&1c=chester&1s=ny&1z=10918&2pn=&2a=$address&2c=&2s=&2z=$zip&r=f"; $filepointer = fopen($url,"r"); if($filepointer){ while(!feof($filepointer)){ $buffer = fgets($filepointer, 4096); $file .= $buffer; } fclose($filepointer); } else { die("Could not Connect to Database. Please Try again"); } ?> <?php // /<span class="txt_body"><br>([^`]*?)<\/span>/ preg_match("/<p><b>Total Est. Distance:<\/b> <span>([^`]*?) miles<\/span>/",$file,$match); $result = $match[1]; // now do the calculator $max_before_pay = 35.00; $location = $result; if ($max_before_pay > $location) { print("<br>You live $result miles away<br> <font color=\"green\">Your Shipping is free!</font>"); } else if($max_before_pay < $location) { $miles = $location - 35; $cost=$miles * .50; print("<br>You live $result miles away<br>Your shipping will cost:: <br><font color=\"green\">$ $cost</font>"); } ?> |
