Hey guys, Im trying to code a body shape calculator using PHP maths functions, its giving me a headache:confused:.

I have tried coding the body shape calculator with the following values, but it jus gives undesirable results:yawn:. Is it my code or the problem is with the ratios?

Apple: 0.8 waist/hip ratio with bust and hips within 2 inches of each other.
Hourglass: 0.7 waist/hip ratio with bust and hips within 2 inches of each other.
Strawberry: 0.8 waist/hip ratio with bust more than 2 inches bigger than hips.
Pear: 0.7 waist/hip ratio with hips more than 2 inches bigger than the bust.
Rectangle: waist/hip ratio same with bust

and here is the PHP code:

<?php //if(isset($_POST['submit'])){
		extract($_POST);
		$memberid = $_SESSION['sess_userID']; 
	//$loginid = $_SESSION['SESS_LOGIN'];
	//$membername = $_SESSION['SESS_member_NAME'];

	/*if ($_SESSION['sess_userID'] == NULL){
		redirect("../login.php");
	}*/
		$waist = $_POST['waist'];
		$hips = $_POST['hips'];
		$ratio = $waist/$hips;
		//print_r($_POST);
		if (($ratio == 0.8) && (abs($bust - $hips) <= 2)){
			$shape = 'apple';
			echo "Your bodyshape is apple";
		}
		elseif (($ratio == 0.8) && (abs($bust - $hips) > 2)){
			$shape = 'strawberry';
			echo "Your bodyshape is strawberry";
		}
		elseif (($ratio == 0.7) && (abs($bust - $hips) <= 2)){
			$shape = 'hourglass';
			echo "Your bodyshape is hourglass";
		}
		elseif (($ratio == 0.7) && (abs($hips - $bust) > 2)){
			$shape = 'pearl';
			echo "Your bodyshape is pearl";
		}
		else {
			$shape = 'rectangle';
			echo "Your bodyshape is rectangle";
			}
		if (isset($_SESSION['sess_userID']))
			$insert = $db->query("UPDATE member SET m_shape = '{$shape}' WHERE m_id = {$memberid}");
	//}?>

I recently came across the following Body shape ratios, Im thinking of using them also since the above ones seems to be failing me.

Apple:
WHR (Waist-to-Hip Ratio) on or above 0.8

Pear: 0.7
WHR (Waist-to-Hip Ratio) under 0.8

Hourglass:
Bust and hips are basically the same circumference - though the bust can be up to 1" larger than the hips. The waist is then 9" or more smaller than the bust.

Triangle
The bust is 3.6" larger than the hips and the waist is less than 9" smaller than the bust.

Rectangle:
Where the bust and hips are basically the same circumference. The waist is less than 9" smaller than the bust.

Hope someone will be of great help. Thanks

Just looking at the calculations you have, you're checking whether the ratio is exactly equal to 0.8 or 0.7 or whatever. The probability of a user entering measurements that would give you exactly that ratio is very remote.

Consider someone having a waist of 30" and hips of 32". That's a ratio of 0.93.
To get close to a ratio of 0.8, you need to have a pretty sizeable difference between the two measurements - obviously 20%. E.g.: 35" and 44". This still equals 0.79..., which wouldn't be matched by any of your criteria.

Could you not instead use a range for the ratio? E.g.: 0.65 - 0.75 = 0.7; 0.75 - 0.85 = 0.8, etc.

Apart from that, I cannot see any reason why this shouldn't work.

R.

Member Avatar for diafol

THis is a bit dodgy isn't it? :)

Apple and Pear are easy to test, but the others aren't too mathematical. As sizes get v large or v small, the ratios for a triangular shape for example may suit the definition, but the actual shape may not resemble the shape you have in mind.

I agree with blocblue - you need a range check.

Just looking at the calculations you have, you're checking whether the ratio is exactly equal to 0.8 or 0.7 or whatever. The probability of a user entering measurements that would give you exactly that ratio is very remote.

Consider someone having a waist of 30" and hips of 32". That's a ratio of 0.93.
To get close to a ratio of 0.8, you need to have a pretty sizeable difference between the two measurements - obviously 20%. E.g.: 35" and 44". This still equals 0.79..., which wouldn't be matched by any of your criteria.

Could you not instead use a range for the ratio? E.g.: 0.65 - 0.75 = 0.7; 0.75 - 0.85 = 0.8, etc.

Apart from that, I cannot see any reason why this shouldn't work.

R.

how can i implement the range check? this is tricky, even converting the statements to php maths functions is a hustle.

The simplest method would probably be:

if (($ratio => 0.75) && ($ratio <= 0.85) && (abs($bust - $hips) <= 2)){

R.

commented: thanks for your help blocblue..truly appreciate it. :-) +1

The simplest method would probably be:

if (($ratio => 0.75) && ($ratio <= 0.85) && (abs($bust - $hips) <= 2)){

R.

Worked like a charm!! Thank you so much blocblue!! the range checks did the trick..well i havnt yet sorted the ranges but im getting the correct results.

here is my modified code

<?php //if(isset($_POST['submit'])){
		extract($_POST);
		$memberid = $_SESSION['sess_userID']; 
	//$loginid = $_SESSION['SESS_LOGIN'];
	//$membername = $_SESSION['SESS_member_NAME'];

	/*if ($_SESSION['sess_userID'] == NULL){
		redirect("../login.php");
	}*/
		$waist = $_POST['waist'];
		$hips = $_POST['hips'];
		$bust = $_POST['bust'];
		$ratio = $waist/$hips;
		//print_r($_POST);
		if (($ratio >= 0.8) && ($ratio <= 0.99) && (abs($bust - $hips) <= 2)){
			$shape = 'apple';
			echo "Your bodyshape is apple";
		}
		elseif (($ratio >= 0.68) && ($ratio <= 0.69) && (abs($bust - $hips) > 2)){
			$shape = 'strawberry';
			echo "Your bodyshape is strawberry";
		}
		elseif (($ratio >= 0.2) && ($ratio <= 0.68) && (abs($bust - $hips) <= 2)){
			$shape = 'hourglass';
			echo "Your bodyshape is hourglass";
		}
		elseif (($ratio >= 0.69) && ($ratio <= 0.79) && (abs($bust - $hips) > 2)){
			$shape = 'pearl';
			echo "Your bodyshape is pearl";
		}
		else{
			$shape = 'rectangle';
			echo "Your bodyshape is rectangle";
			}
		if (isset($_SESSION['sess_userID']))
			$insert = $db->query("UPDATE member SET m_shape = '{$shape}' WHERE m_id = {$memberid}");
	//}?>

Great - glad it's working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.