Jump to content

Recommended Posts

hi,

 

i need help. i currently have a code that assigns a value for $zoom depending on the value of $dist1

but its very long and i was thinking of just converting it to math that will add increments on the

value of zoom with respect to the value of $dist1

my problem is, i dont know how to do it.

below is my current code

if ($dist1>=0)
{
if ($dist1<=600)
{
	$zoom=4000;
}
}
if ($dist1>=600)
{
if ($dist1<=1000)
{
	$zoom=8500;
}
}
if ($dist1>=1000)
{
if ($dist1<=1600)
{
	$zoom=10000;
}
}
if ($dist1>=1600)
{
if ($dist1<=2000)
{
	$zoom=15000;
}
}
if ($dist1>=2000)
{
if ($dist1<=3500)
{
	$zoom=20000;
}
}
if ($dist1>=3500)
{
if ($dist1<=4550)
{
	$zoom=25000;
}
}
if ($dist1>=4550)
{
if ($dist1<=5500)
{
	$zoom=30000;
}
}
if ($dist1>=5500)
{
if ($dist1<=6600)
{
	$zoom=35000;
}
}
if ($dist1>=6600)
{
if ($dist1<=7600)
{
	$zoom=40000;
}
}
if ($dist1>=7600)
{
if ($dist1<=8600)
{
	$zoom=45000;
}
}
if ($dist1>=8600)
{
if ($dist1<=9600)
{
	$zoom=50000;
}
}
if ($dist1>=9600)
{
if ($dist1<=10600)
{
	$zoom=55000;
}
}
if ($dist1>=10600)
{
if ($dist1<=11600)
{
	$zoom=60000;
}
}
if ($dist1>=11600)
{
if ($dist1<=12600)
{
	$zoom=65000;
}
}
if ($dist1>=12600)
{
if ($dist1<=13600)
{
	$zoom=70000;
}
}
if ($dist1>=13600)
{
if ($dist1<=14600)
{
	$zoom=75000;
}
}
if ($dist1>=14600)
{
if ($dist1<=15600)
{
	$zoom=80000;
}
}
if ($dist1>=15600)
{
if ($dist1<=16600)
{
	$zoom=85000;
}
}
if ($dist1>=16600)
{
$zoom=100000;
}

 

 

hope you could help me with this.

 

thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/59774-need-help-with-math-on-php/
Share on other sites

i can't see the logical increase..

how are you working out the zoom compared to the dist!

 

also

 

it would probably be easier to do

if ($dist1 >= 0 && $dist1<=600)
{
$zoom=4000;
}
if ($dist1 >= 601 && $dist1<=1000)
{
$zoom=8500;
}

 

quicker code being

if ($dist1 >= 0 && $dist1<=600)
{
$zoom=4000;
}else{
if ($dist1 >= 601 && $dist1<=1000)
{
	$zoom=8500;
}
}

hi,

 

infid3l  thanks you are right about it.

 

sorry for the cofusion

 

;)

 

However, your statements have no real formula to them. You might be able to do something like:

 

for($i=0;$i<=20*600;$i=$i+600){
if ($dist1>=$i)
{
	if ($dist1<=($i+600))
	{
		$zoom=($i+600)*4;
		break;
	}
}
}

 

It doesn't exactly fulfill your "formula" though.

<?php

$zoom_array = array(4000,8500,10000,15000);

$range_array = array('0-600','601-1000','1001-1600','1601-2000');

$dist1 = 800;

echo "This is the zoom value ".getzoom($dist1,$zoom_array,$range_array);

function getzoom($dist1,$zoom_array,$range_array){

foreach($range_array as $key => $value){

$range = explode("-",$value);

if($dist1 >= $range[0] and $dist1 <= $range[1]){

return $zoom_array[$key];

}

}

return "Nothing Found";

}

?>

  • 3 weeks later...

hi,

 

sorry i wasnt able to get back on this since the last reply. been busy on other projects

im confused on the array...

anyway,

how can i just simplify this code?

//computation for the scaling
if ($distance >= 0 && $distance<=1000)
{
$scale=$distance*21;
}
if ($distance >= 1001 && $distance<=2000)
{
$scale=$distance*20;
}
if ($distance >= 2001 && $distance<=3000)
{
$scale=$distance*19;
}
if ($distance >= 3001 && $distance<=4000)
{
$scale=$distance*18;
}
if ($distance >= 4001 && $distance<=5000)
{
$scale=$distance*18;
}
if ($distance >= 5001 && $distance<=6000)
{
$scale=$distance*16;
}
if ($distance >= 6001 && $distance<=7000)
{
$scale=$distance*16;
}
if ($distance >= 7001 && $distance<=8000)
{
$scale=$distance*16;
}
if ($distance >= 8001 && $distance<=9000)
{
$scale=$distance*16;
}
if ($distance >= 9001 && $distance<=10000)
{
$scale=$distance*16;
}

 

 

hope you could help me with this.

 

thanks!

<?php
$distance = 1500;
$main_array = array("21-0-1000","20-1001-2000","19-2001-3000");

echo " Scal is ".getscale($main_array,$distance);

function getscale($main_array,$distance){

foreach($main_array as $index => $n_arr_range){
	$val_ar = array();
	$val_ar = explode("-",$n_arr_range);
	if($distance >= $val_ar[1]  and $distance <= $val_ar[2]){
		return $distance*$val_ar[0];
	}
}
return " Not Found ";

}

?>

this is what i did

//computation for the scaling
$main_array = array("21-0-1000","20-1001-2000","19-2001-3000","18-3001-4000","18-4001-5000","16-5001-6000","16-6001-7000","16-7001-8000","16-8001-9000","16-9000-10000");

//echo " Scal is ".getscale($main_array,$distance);

function getscale($main_array,$distance){

foreach($main_array as $index => $n_arr_range){
	$val_ar = array();
	$val_ar = explode("-",$n_arr_range);
	if($distance >= $val_ar[1]  and $distance <= $val_ar[2]){
		return $distance*$val_ar[0];
		$scale=$distance*$val_ar[0];
	}
}
}

 

and i am getting this error

Fatal error: Cannot redeclare getscale() (previously declared in /var/www/html/myoochi/findit/propertyfindit.php:876) in /var/www/html/myoochi/findit/propertyfindit.php on line 876

 

 

hope you could help me with this.

 

thanks!

//computation for the scaling

$main_array = array("21-0-1000","20-1001-2000","19-2001-3000","18-3001-4000","18-4001-5000","16-5001-6000","16-6001-7000","16-7001-8000","16-8001-9000","16-9000-10000");

 

//echo " Scal is ".getscale_change($main_array,$distance);

 

function getscale_change($main_array,$distance){

 

foreach($main_array as $index => $n_arr_range){

$val_ar = array();

$val_ar = explode("-",$n_arr_range);

if($distance >= $val_ar[1]  and $distance <= $val_ar[2]){

return $distance*$val_ar[0];

$scale=$distance*$val_ar[0];

}

}

}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.