 |
|
|
12-14-2005, 08:46 AM
|
Must Clean Code
|
Posts: 70
|
I have a form calculation script here and it needs cleaning up. Can anyone help me? It's just that each and every variable has been explicitly defined, and i know that you can use loops to automatically assign variable names and the like. But i don't know exactly how to do it. The code is below:
PHP Code:
// Start a session session_start(); header("Cache-control: private"); // Capture the variables from the posted form $fname = $HTTP_POST_VARS['fname']; $lname = $HTTP_POST_VARS['lname']; $address = $HTTP_POST_VARS['address']; $city = $HTTP_POST_VARS['city']; $state = $HTTP_POST_VARS['state']; $country = $HTTP_POST_VARS['country']; $pcode = $HTTP_POST_VARS['pcode']; $email = $HTTP_POST_VARS['email']; $phone = $HTTP_POST_VARS['phone']; $fax = $HTTP_POST_VARS['fax']; $cctype = $HTTP_POST_VARS['cctype']; $ccnum = $HTTP_POST_VARS['ccnum']; $ccexmonth = $HTTP_POST_VARS['ccexmonth']; $ccexyear = $HTTP_POST_VARS['ccexyear']; $ccname = $HTTP_POST_VARS['ccname']; $prod1quantity = $HTTP_POST_VARS['prod1quantity']; $prod2quantity = $HTTP_POST_VARS['prod2quantity']; // Import the class file into the php script include 'classorder.php'; // Assign variables to the class functions $newOrder = new order(); $newOrder -> setFname($fname); $newOrder -> setLname($lname); $newOrder -> setAddress($address); $newOrder -> setCity($city); $newOrder -> setState($state); $newOrder -> setCountry($country); $newOrder -> setPcode($pcode); $newOrder -> setEmail($email); $newOrder -> setPhone($phone); $newOrder -> setFax($fax); $newOrder -> setCctype($cctype); $newOrder -> setCcnum($ccnum); $newOrder -> setCcexmonth($ccexmonth); $newOrder -> setCcexyear($ccexyear); $newOrder -> setCcname($ccname); $newOrder -> setProd1quantity($prod1quantity); $newOrder -> setProd2quantity($prod2quantity); // Assign session variables $HTTP_SESSION_VARS['fname'] = $fname; $HTTP_SESSION_VARS['lname'] = $lname; $HTTP_SESSION_VARS['address'] = $address; $HTTP_SESSION_VARS['city'] = $city; $HTTP_SESSION_VARS['state'] = $state; $HTTP_SESSION_VARS['country'] = $country; $HTTP_SESSION_VARS['pcode'] = $pcode; $HTTP_SESSION_VARS['email'] = $email; $HTTP_SESSION_VARS['phone'] = $phone; $HTTP_SESSION_VARS['fax'] = $fax; $HTTP_SESSION_VARS['cctype'] = $cctype; $HTTP_SESSION_VARS['ccnum'] = $ccnum; $HTTP_SESSION_VARS['ccexmonth'] = $ccexmonth; $HTTP_SESSION_VARS['ccexyear'] = $ccexyear; $HTTP_SESSION_VARS['ccname'] = $ccname; $HTTP_SESSION_VARS['prod1quantity'] = $prod1quantity; $HTTP_SESSION_VARS['prod2quantity'] = $prod2quantity; // Set data types for product quantities settype($prod1quantity,int); settype($prod2quantity,int); // Validate form data if($fname == "") {header("Location: ../ordererror.php"); exit;} if($lname == "") {header("Location: ../ordererror.php"); exit;} if($address == "") {header("Location: ../ordererror.php"); exit;} if($city == "") {header("Location: ../ordererror.php"); exit;} if($state == "") {header("Location: ../ordererror.php"); exit;} if($country == "Select Your Country") {header("Location: ../ordererror.php"); exit;} if($pcode == "") {header("Location: ../ordererror.php"); exit;} if($email == "") {header("Location: ../ordererror.php"); exit;} if($phone == "") {settype($phone,string); $phone = "Undisclosed";} if($fax == "") {settype($fax,string); $fax = "Undisclosed";} if($cctype == "NULL") {header("Location: ../ordererror.php"); exit;} if($ccnum == "") {header("Location: ../ordererror.php"); exit;} if($ccname == "") {header("Location: ../ordererror.php"); exit;} if($prod1quantity == 0 && $prod2quantity == 0) {header("Location: ../ordererror.php"); exit;} // Set data types for text input settype($fname,string); settype($lname,string); settype($address,string); settype($city,string); settype($state,string); settype($country,string); settype($pcode,string); settype($email,string); settype($phone,string); settype($fax,string); settype($ccnum,string); settype($ccname,string); // Perform mathematical operations for total quantity of tubes $tubesquantity = $prod1quantity + $prod2quantity; // Perform mathematical operation for total cost of tubes if($tubesquantity == 1) {$totaltubes = 8.85;} elseif($tubesquantity == 2) {$totaltubes = $tubesquantity * 8.00;} elseif($tubesquantity <= 14) {$totaltubes = $tubesquantity * 7.50;} elseif($tubesquantity >= 15) {$totaltubes = $tubesquantity * 7.15;} // Conditions for GST if($country == "Australia") {$addgst = "Yes"; $totalgst = $totaltubes * 0.1;} else {$addgst = "No"; $totalgst = 0.00;} // Assigning numerical values to freight zones if($country == "Australia") {$freightzone = 1;} elseif($country == "New Zealand") {$freightzone = 2;} elseif($country == "Papua New Guinea") {$freightzone = 3;} elseif($country == "Philippines") {$freightzone = 3;} elseif($country == "Japan") {$freightzone = 3;} elseif($country == "China") {$freightzone = 3;} elseif($country == "Korea, Republic Of") {$freightzone == 3;} elseif($country == "Thailand") {$freightzone = 3;} elseif($country == "United States") {$freightzone = 4;} elseif($country == "Canada") {$freightzone = 4;} elseif($country == "Mexico") {$freightzone = 4;} elseif($country == "United States Minor Outlying Islands") {$freightzone = 4;} else {$freightzone = 5;} // Conditions for freight charges in Australia if($freightzone == 1 && $tubesquantity == 0) {$totalfreight = 0;} elseif($freightzone == 1 && $tubesquantity <= 2) {$totalfreight = 4.5;} elseif($freightzone == 1 && $tubesquantity <= 5) {$totalfreight == 5.5;} elseif($freightzone == 1 && $tubesquantity >= 6) {$totalfreight = 9;} // Conditions for freight charges in New Zealand elseif($freightzone == 2 && $tubesquantity == 0) {$totalfreight = 0;} elseif($freightzone == 2 && $tubesquantity <= 2) {$totalfreight = 7;} elseif($freightzone == 2 && $tubesquantity <= 5) {$totalfreight = 10;} elseif($freightzone == 2 && $tubesquantity <= 8) {$totalfreight = 13.5;} elseif($freightzone == 2 && $tubesquantity <= 11) {$totalfreight = 16.5;} elseif($freightzone == 2 && $tubesquantity <= 14) {$totalfreight = 20;} elseif($freightzone == 2 && $tubesquantity <= 17) {$totalfreight = 23;} elseif($freightzone == 2 && $tubesquantity <= 19) {$totalfreight = 26.5;} elseif($freightzone == 2 && $tubesquantity <= 22) {$totalfreight = 29.5;} elseif($freightzone == 2 && $tubesquantity <= 24) {$totalfreight = 33.5;} elseif($freightzone == 2 && $tubesquantity <= 26) {$totalfreight = 37.5;} // Conditions for freight charges in the Asia Pacific Zone elseif($freightzone == 3 && $tubesquantity == 0) {$totalfreight = 0;} elseif($freightzone == 3 && $tubesquantity <= 2) {$totalfreight = 8;} elseif($freightzone == 3 && $tubesquantity <= 5) {$totalfreight = 12;} elseif($freightzone == 3 && $tubesquantity <= 8) {$totalfreight = 16.5;} elseif($freightzone == 3 && $tubesquantity <= 11) {$totalfreight = 20.5;} elseif($freightzone == 3 && $tubesquantity <= 14) {$totalfreight = 25;} elseif($freightzone == 3 && $tubesquantity <= 17) {$totalfreight = 29;} elseif($freightzone == 3 && $tubesquantity <= 19) {$totalfreight = 33.5;} elseif($freightzone == 3 && $tubesquantity <= 22) {$totalfreight = 37.5;} elseif($freightzone == 3 && $tubesquantity <= 24) {$totalfreight = 42.5;} elseif($freightzone == 3 && $tubesquantity <= 26) {$totalfreight = 48;} // Conditions for freight charges in the USA zone elseif($freightzone == 4 && $tubesquantity == 0) {$totalfreight = 0;} elseif($freightzone == 4 && $tubesquantity <= 2) {$totalfreight = 9;} elseif($freightzone == 4 && $tubesquantity <= 5) {$totalfreight = 14;} elseif($freightzone == 4 && $tubesquantity <= 8) {$totalfreight = 19.5;} elseif($freightzone == 4 && $tubesquantity <= 11) {$totalfreight = 24.5;} elseif($freightzone == 4 && $tubesquantity <= 14) {$totalfreight = 30;} elseif($freightzone == 4 && $tubesquantity <= 17) {$totalfreight = 35;} elseif($freightzone == 4 && $tubesquantity <= 19) {$totalfreight = 40.5;} elseif($freightzone == 4 && $tubesquantity <= 22) {$totalfreight = 45.5;} elseif($freightzone == 4 && $tubesquantity <= 24) {$totalfreight = 53;} elseif($freightzone == 4 && $tubesquantity <= 26) {$totalfreight = 60.5;} // Conditions for freight charges anywhere else in the world elseif($freightzone == 5 && $tubesquantity == 0) {$totalfreight = 0;} elseif($freightzone == 5 && $tubesquantity <= 2) {$totalfreight = 10.5;} elseif($freightzone == 5 && $tubesquantity <= 5) {$totalfreight = 16.5;} elseif($freightzone == 5 && $tubesquantity <= 8) {$totalfreight = 23;} elseif($freightzone == 5 && $tubesquantity <= 11) {$totalfreight = 29;} elseif($freightzone == 5 && $tubesquantity <= 14) {$totalfreight = 35.5;} elseif($freightzone == 5 && $tubesquantity <= 17) {$totalfreight = 41.5;} elseif($freightzone == 5 && $tubesquantity <= 19) {$totalfreight = 48;} elseif($freightzone == 5 && $tubesquantity <= 22) {$totalfreight = 54;} elseif($freightzone == 5 && $tubesquantity <= 24) {$totalfreight = 63.5;} elseif($freightzone == 5 && $tubesquantity <= 26) {$totalfreight = 73;} // Calculate the total cost of the order $totalcost = $totaltubes + $totalgst + $totalfreight; // Assign calculated values to session variables $HTTP_SESSION_VARS['tubesquantity'] = $tubesquantity; $HTTP_SESSION_VARS['totaltubes'] = $totaltubes; $HTTP_SESSION_VARS['totalgst'] = $totalgst; $HTTP_SESSION_VARS['totalfreight'] = $totalfreight; $HTTP_SESSION_VARS['totalcost'] = $totalcost; // Save the session session_write_close(); // Redirect the user to the desired page header("Location: ../ordercomplete.php"); exit;
As you can tell, it really does need cleaning. In addition, there is an annoying bug in the code which I will post a thread on when the other code is cleaned up. Any help would be ever so greatly appreciated.
Last edited by cerebro89; 12-14-2005 at 08:55 AM..
|
|
|
|
12-14-2005, 10:09 AM
|
|
Posts: 30
Location: Berlin Germany Europe World
|
hi celebro89,
maybe you're looking for 'foreach'
like this:
Code:
<?php
foreach ($_POST as $k => $v){
$_SESSION[$k] = $v;
}
?>
mfg
akratellio
__________________
I'm always lying...
Ich lüge immer...
Please login or register to view this content. Registration is FREE
|
|
|
|
12-14-2005, 10:14 AM
|
|
Posts: 70
|
thanks akratellio for your tip. i replaced the variable declarations with your fix and i think it will work. but there's still more stuff that needs cleaning up, so if i can get your help with this that would be greatly appreciated. surely there's a shorter way of writing this code out!!
|
|
|
|
12-14-2005, 01:50 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Here's a much shorter version that should work unless I've introduced any bugs while messing with it (possible!). Generally if you need to do anything more than about three times, build an array and use a loop of some sort. I've employed a variety of loops below, feel free to ask for clarification.
Can we see the code for the order class you use? By changing the functions in the class to take arrays, you could probably get rid of all the setThis($val) setThat($val) setTheother($val) with a single setData($array) function instead.
PHP Code:
// Start a session session_start(); header("Cache-control: private"); // Capture the variables from the posted form foreach( $HTTP_POST_VARS as $key => $value) { $$key = $value; }
// Import the class file into the php script include 'classorder.php'; // Assign variables to the class functions $newOrder = new order();
// <snip> this can definitely be shortened, can we see the code // for the order class?
// Assign session variables
$HTTP_SESSION_VARS = array_merge($HTTP_SESSION_VARS, $HTTP_POST_VARS);
// Set data types for product quantities settype($prod1quantity,int); settype($prod2quantity,int); // Validate form data foreach ($HTTP_POST_VARS as $key => $value) { $empty = ""; if($key == "country") $empty = "Select Your Country"; if($key == "cctype") $empty = "NULL"; if($key == "prod1quantity" || $key == "prod2quantity") continue; if($value == $empty) { header("Location: ../ordererror.php"); exit; } } if($prod1quantity == 0 && $prod2quantity == 0) { header("Location: ../ordererror.php"); exit; } // Set data types for text input
foreach( $HTTP_POST_VARS as $key => $value) { if($key != "prod1quantity" && $key == "prod2quantity") { settype($$key,string); } }
// Perform mathematical operations for total quantity of tubes $tubesquantity = $prod1quantity + $prod2quantity; // Perform mathematical operation for total cost of tubes if($tubesquantity == 1) {$totaltubes = 8.85;} elseif($tubesquantity == 2) {$totaltubes = $tubesquantity * 8.00;} elseif($tubesquantity <= 14) {$totaltubes = $tubesquantity * 7.50;} elseif($tubesquantity >= 15) {$totaltubes = $tubesquantity * 7.15;} // Conditions for GST if($country == "Australia") {$addgst = "Yes"; $totalgst = $totaltubes * 0.1;} else {$addgst = "No"; $totalgst = 0.00;} // Assigning numerical values to freight zones $zones = Array("Australia" => 1, "New Zealand" => 2, "Papua New Guinea" => 3, "Philippines" => 3, "Japan" => 3, "China" => 3, "Korea, Republic Of" => 3, "Thailand" => 4, "United States" => 4, "Canada" => 4, "Mexico" => 4, "United States Minor Outlying Islands" => 4);
if(array_key_exists($country,$zones)) { $freightzone = $zones[$country]; } else { $freightzone = 5; }
$ranges = Array(0,2,5,8,11,14,17,19,22,24,26);
// Conditions for freight charges in Australia if($freightzone == 1) { if($tubesquantity == 0) $totalfreight = 0; if($tubesquantity <= 2) $totalfreight = 4.5; if($tubesquantity <= 5) $totalfreight = 5.5; if($tubesquantity >= 6) $totalfreight = 9; } elseif($freightzone == 2) { $charges = Array(0,7,10,13.5,16.5,20,23,26.5,29.5,33.5,37.5); } elseif ($freightzone == 3) { $charges = Array(0,8,12,16.5,20.5,25,29,33.5,37.5,42.5,48); } elseif ($freightzone == 4) { $charges = Array(0,9,14,19.5,24.5,30,35,40.5,45.5,53,60.5); } else { //$freightzone must be 5 $charges = Array(0,10.5,16.5,23,29,35.5,41.5,48,54,63.5,73); }
for($i = 0; $i < count($ranges); $i++) { if($tubesquantity <= $ranges[$i]) { $totalfreight = $charges[$i]; break; } }
// Calculate the total cost of the order $totalcost = $totaltubes + $totalgst + $totalfreight; // Assign calculated values to session variables $HTTP_SESSION_VARS['tubesquantity'] = $tubesquantity; $HTTP_SESSION_VARS['totaltubes'] = $totaltubes; $HTTP_SESSION_VARS['totalgst'] = $totalgst; $HTTP_SESSION_VARS['totalfreight'] = $totalfreight; $HTTP_SESSION_VARS['totalcost'] = $totalcost; // Save the session session_write_close(); // Redirect the user to the desired page header("Location: ../ordercomplete.php"); exit;
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
Last edited by 0beron; 12-14-2005 at 02:06 PM..
|
|
|
|
12-14-2005, 06:43 PM
|
|
Posts: 70
|
here's the code for the class definition:
PHP Code:
// This is the start of the order form class definition class order { // Variable declarations. Note the keyword 'var' and // the list separated by a comma var $fname, $lname, $address, $city, $state, $country, $pcode, $email, $phone, $fax, $cctype, $ccnum, $ccexmonth, $ccexyear, $ccname, $custsig, $prod1quantity, $prod2quantity, $tubesquantity, $addgst, $freightzone, $totaltubes, $totalgst, $totalfreight, $totalcost; // All methods with their argument lists. Note the keyword '$this' and // the syntax to assign values function order() {// This is the constructor, setting values $this -> fname = ''; $this -> lname = ''; $this -> address = ''; $this -> city = ''; $this -> state = ''; $this -> country = ''; $this -> pcode = ''; $this -> email = ''; $this -> phone = ''; $this -> fax = ''; $this -> ccytpe = ''; $this -> ccnum = ''; $this -> ccexmonth = ''; $this -> ccexyear = ''; $this -> ccname = ''; $this -> custsig = ''; $this -> prod1quantity = ''; $this -> prod2quantity = ''; $this -> tubesquantity = ''; $this -> freightzone = ''; $this -> addgst = ''; $this -> totaltubes = ''; $this -> totalgst = ''; $this -> totalfreight = ''; $this -> totalcost = ''; } // All variables are: function getFname() {return $this -> fname;} function setFname($newFname) {$this -> fname = $newFname;} function getLname() {return $this -> lname;} function setLname($newLname) {$this -> lname = $newLname;} function getAddress() {return $this -> address;} function setAddress($newAddress) {$this -> address = $newAddress;} function getCity() {return $this -> city;} function setCity($newCity) {$this -> city = $newCity;} function getState() {return $this -> state;} function setState($newState) {$this -> state = $newState;} function getCountry() {return $this -> country;} function setCountry($newCountry) {$this -> country = $newCountry;} function getPcode() {return $this -> pcode;} function setPcode($newPcode) {$this -> pcode = $newPcode;} function getEmail() {return $this -> email;} function setEmail($newEmail) {$this -> email = $newEmail;} function getPhone() {return $this -> phone;} function setPhone($newPhone) {$this -> phone = $newPhone;} function getFax() {return $this -> fax;} function setFax($newFax) {$this -> fax = $newFax;} function getCctype() {return $this -> ccype;} function setCctype($newCctype) {$this -> cctype = $newCctype;} function getCcnum() {return $this -> ccnum;} function setCcnum($newCcnum) {$this -> ccnum = $newCcnum;} function getCcexmonth() {return $this -> ccexmonth;} function setCcexmonth($newCcexmonth) {$this -> ccexmonth = $newCcexmonth;} function getCcexyear() {return $this -> ccexyear;} function setCcexyear($newCcexyear) {$this -> ccexyear = $newCcexyear;} function getCcname() {return $this -> ccname;} function setCcname($newCcname) {$this -> ccname = $newCcname;} function getCustsig() {return $this -> custsig;} function setCustsig($newCustsig) {$this -> custsig = $newCustsig;} function getProd1quantity() {return $this -> prod1quantity;} function setProd1quantity($newProd1quantity) {$this -> prod1quantity = $newProd1quantity;} function getProd2quantity() {return $this -> prod2quantity;} function setProd2quantity($newProd2quantity) {$this -> prod2quantity = $newProd2quantity;} function getTubesquantity() {return $this -> tubesquantity;} function setTubesquantity($newTubesquantity) {$this -> tubesquantity = $newTubesquantity;} function getFreightzone() {return $this -> freightzone;} function setFreightzone($newFreightzone) {$this -> freightzone = $newFreightzone;} function getAddgst() {return $this -> addgst;} function setAddgst($newAddgst) {$this -> addgst = $newAddgst;} function getTotaltubes() {return $this -> totaltubes;} function setTotaltubes($newTotaltubes) {$this -> totaltubes = $newTotaltubes;} function getTotalgst() {return $this -> totalgst;} function setTotalgst($newTotalgst) {$this -> totalgst = $newTotalgst;} function getTotalfreight() {return $this -> totalfreight;} function setTotalfreight($newTotalfreight){$this -> totalfreight = $newTotalfreight;} function getTotalcost() {return $this -> totalcost;} function setTotalcost($newTotalcost) {$this -> totalcost = $newTotalcost;} } // End of class definition
|
|
|
|
12-15-2005, 07:56 AM
|
|
Posts: 70
|
A note to Oberon,
Thanks very much for your help with the cleanup. Evidently I still have a long way to go with regards to my PHP expertise. I think there is a bug in the form validation area of your suggested code. Even when all the details are correctly filled out, I'm still redirected to the error page. Why is that? If all the data is properly entered the loop shouldn't redirect me to the final page. Do you have any suggestions?
Regards,
cerebro89
|
|
|
|
12-15-2005, 09:18 AM
|
|
Posts: 310
Location: UK
|
hmm so you doing like a shopping cart idea?
i also take it you are taking payments on your website or are you sending the user to a payment colection site?
i think i can help you with this but i not to grate on php 
|
|
|
|
12-15-2005, 09:33 AM
|
|
Posts: 70
|
not exactly
there's no online shop, merely an order form with two products to choose from. The data can be emailed directly to the company or the user can print out the form and fax it to the company. there's no kind of payment gateways or anything of the like.
|
|
|
|
12-15-2005, 09:38 AM
|
|
Posts: 310
Location: UK
|
ok so there is no need for a database you just need to pass the info to another page were that page can collect the info needed and echo it for print or email to a email prvided
|
|
|
|
12-15-2005, 09:42 AM
|
|
Posts: 70
|
not at this point in time. my client wants to keep things simple, so that's how they shall be.
is there a way to clean up the code of my class definition? it's just that once i upload the files i don't know who else will be viewing the code and i don't what my client calling me saying that the code is too long!!
the class definition is already on this thread so i won't post it again, but any help would be greatly appreciated
|
|
|
|
12-15-2005, 09:51 AM
|
|
Posts: 310
Location: UK
|
Quote:
|
Originally Posted by cerebro89
not at this point in time. my client wants to keep things simple, so that's how they shall be.
is there a way to clean up the code of my class definition? it's just that once i upload the files i don't know who else will be viewing the code and i don't what my client calling me saying that the code is too long!!
the class definition is already on this thread so i won't post it again, but any help would be greatly appreciated
|
to be onist if the code is working and doing the job you want it to do then there is no point in messing
if your client has a problem over the lenth of the code but not wether it works or not then the client dosnt no what they are doing i would say if you are not going to use a database and you are passing everything on by sessions then it is going to be long i have seen php scripts out there that are longer than that but what needs to be done gets done
some of the other members have puts some good codes that will make yours smaller or maybe you could try a post or get metherd dont no how good that will work sorry i cant help you any more on the matter maybe showing the page that you have so others can see the form you are doing this might help them tell you better ways 
|
|
|
|
12-15-2005, 01:30 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Ah yes, you're right - I missed out various things from the data validation bit - it doesn't set the phone to undisclosed and checks ccexmonth and ccexyear when it shouldn't. You're probably better off with what you had before for that section.
As far as your class goes, what are you actually doing with it besides creating one instance of it, and then assigning data to its fields? Nothing else happens to it in the script - is it there so it can be used later on?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
|
« Reply to Must Clean Code
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|