Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Always prefer a greater value using IF Statements

Posted on 2011-02-21
5
802 Views
Last Modified: 2012-08-13
Hello,

I'm after a bit of help with a script I have written, I'm pretty new to PHP and struggling to get my head around it.

I have a website which uses a shipping calculator, featuring the dimensions (width, length, height) and the weight (in kg) of a parcel.
I'm using 2 variables; $volumetric and $weight.
The $volumetric variable is calculated as such, (weight * length * height) / 5000.

Here's what the script is intended to do:-
1. Get values from an HTML form. In this case, 'volumetric' and 'weight'.
2. Compare the values to see which is the largest.
3. Return/redirect to the corresponding URL of the product from the web shop.

At the moment it always takes preference to the volumetric value and does not take the weight into consideration.

An example would be as follows:
Customer enters their dimensions of their box size, 10cm x 10cm x 10cm. This works out to be a volumetric weight value of 0.2KG.
If the customer enters the weight for their parcel as say 5KG, it should return the product for 5KG.

Essentially what I'm trying to do is return/redirect to the URL of a product which has the greater value. I'm currently using IF statements to do this, however, I'm always open to suggestions!

I have attached the code for your reference.

Many Thanks in advance for your reply.

Ben M. fetchProduct.php fetchProduct.php
0
Comment
Question by:Steve-UK
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 100 total points
ID: 34942662
Try this...  I think it does the same thing you want but with less code and will handle weights up to any value.  You could put an upper limit on it if you needed to.
 
<?php

// ALWAYS SET ERROR REPORTING SO YOU CAN SEE IF ANYTHING IS AMISS
error_reporting(E_ALL);

if (isset($_POST["width"]) && isset($_POST["length"]) && isset($_POST["height"]))
{
	// GET THE VARIABLES FROM THE QUOTE FORM
	$volumetric = ($_POST["width"] * $_POST["length"] * $_POST["height"]) / 5000;
	$dimensions = ($_POST["width"] * $_POST["length"] * $_POST["height"]);
	$weight = ($_POST["weight"]);
	
	
	// round both values up to next greatest integer
	$weight =  intval(ceil($weight));
	$volumetric = intval(ceil($volumetric));
	
	if ($weight > $volumetric){
		// This will create a string value
		$product = 'UPS-'.$weight.'KG';		
	} else {		
		$product = 'UPS-'.$volumetric.'KG-Volumetric';		
	}
	

		
	// LOAD THE SCRIPT - IT CAN USE THE VALUE IN $volumetric DIRECTLY
	include_once ('http://79.170.44.122/parcelsave.com/products.php');
	
	// RUN THE FUNCTION
	returnProduct($product, $volumetric, $dimensions);
}
?>

<?php 


function returnProduct($product $volumetric, $dimensions) {
	
	// CATCH INVALID PARCEL - Over 70KG OR Over Dimensions
	if ( ($volumetric > 70) || ($dimensions > 370) ) {
		header("Location: http://79.170.44.122/parcelsave.com/pages.php?pageid=24");
	} else {

		$location = 'Location: http://79.170.44.122/parcelsave.com/products.php?product='.$product;
		header($location);	
	}
	
}

?>

Open in new window

0
 
LVL 12

Accepted Solution

by:
Amick earned 400 total points
ID: 34942701
I don't currently have PHP installed, so this is from memory, but may be useful.
The idea is to take the larger number and rather than running through a number of conditional tests simply create the call.

I didn't handle out-of-bounds packages in my example, but you can easily add that back.

<?php

// ALWAYS SET ERROR REPORTING SO YOU CAN SEE IF ANYTHING IS AMISS
error_reporting(E_ALL);

if (isset($_POST["width"]) && isset($_POST["length"]) && isset($_POST["height"]))
{
	// GET THE VARIABLES FROM THE QUOTE FORM
	$volumetric = ($_POST["width"] * $_POST["length"] * $_POST["height"]) / 5000;
	$dimensions = ($_POST["width"] * $_POST["length"] * $_POST["height"]);
	$weight = ($_POST["weight"]);
	
	if ($weight >= $volumetric) {
		header("Location: http://79.170.44.122/parcelsave.com/products.php?product=UPS-".ceil($weight)."KG");
	else
	($weight >= $volumetric) {
		header("Location: http://79.170.44.122/parcelsave.com/products.php?product=UPS-".ceil($volumetric)."KG-Volumetric");
	}
	
?>

Open in new window

0
 
LVL 12

Expert Comment

by:Amick
ID: 34942727
There is at least one typo in my previous post.  Line 16 should be changed from "($weight >= $volumetric) {" to simply "{" .
0
 

Author Comment

by:Steve-UK
ID: 34942781
Hi guys,

Many thanks for the responses, much appreciated.

I have tried both versions so far and for some reason both are returning the 'Invalid' page no matter what values are entered.

The quote form for testing can be found at http://79.170.44.122/parcelsave.com/pages.php?pageid=19

Looks to me like either method should work, but looks like it's going wrong somewhere.

Thanks again,
Ben
0
 

Author Comment

by:Steve-UK
ID: 34942816
Hi again,

Ignore my last post...managed to get it working!

Just added a couple of braces in the right place.

All that's left is to add an upper limit.

Thank you both very much for all your help.

Regards,
Ben
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
Custom Wordpress Loop 22 38
PHP Echo with auto submit 8 28
AJAX Wordpress Not Reading Variable 2 27
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question