Welcome to WebmasterWorld Guest from **23.22.19.253**

Hey

There is loads of literature on checkboxes in php but I am having major difficulties in finding exactly the solution to my problem.

I want to allow people to choose from a selection of locations using checkboxes. Each location has an x coordinate and y coordinate. I want to work out the distance between each of the locations checked, by using the following function:

$x_distance = array ("0", "3", "1", "3");

$y_distance = array ("1", "5", "3", "3");

$distance = 0;

for($i=0; $i<3; $i++) {

$distance = sqrt((($x_distance[$i] - $x_distance[$i-1])*

($x_distance[$i] - $x_distance[$i-1])) +

(($y_distance[$i] - $y_distance[$i-1])*

($y_distance[$i] - $y_distance[$i-1])));

}

So my question is, how do i write a php script to allow people to check as many checkboxes they like (depending on what locations they are going to) and then to work out the total distance between them?!

Can anyone help me please?

PS at present I have just made 4 locations and put random numbers into the array, as you can see!

Welcome to WebmasterWorld [webmasterworld.com] k8tie

I think if(isset($var)) works or you can just check if the value is set if($var == $value). Though I think the isset is what I use. I don't use checkboxes that often so I can't quite remember.

well, it depends on where you do your calculations.

You have a form with checkboxes on one page, people make their selections/enter info and then click submit.

The form is then processed by posting to a script on another/the same page. I assume that is where that function you put in is. Before you do your calculations you will need to know what was selected.

so would have

if(isset($checkbox1)) calculate();

if(isset($checkbox2)) calculate();

if(isset($checkbox3)) calculate();

if(isset($checkbox4)) calculate();

That is sloppy but does it help?

<added>by the way, it's not annoying :), if you don't understand then maybe you can give me a little more info on how you process the form and the logical flow of the setup right now.

Hey

So far I have the checkbox.php page

<HTML>

<HEAD>

<TITLE> Make your choice </TITLE>

</HEAD>

<BODY>

<FORM action="distance.php" method="post">

<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=1>Location 1<br>

<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=2>Location 2<br>

<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=3>3<br>

<INPUT TYPE = "checkbox" NAME="LOCATION_ARRAY[]" value=4>4<br>

<INPUT TYPE = "submit" value="Submit">

</FORM>

</BODY>

</HTML>

Then the distance.php is the following code

<?

$LOCATION_ARRAY = $_POST['LOCATION_ARRAY'];

$x_distance = array ("0", "3", "1", "3");

$y_distance = array ("1", "5", "3", "3");

for ($i=0; $i<sizeof($LOCATION_ARRAY); $i++)

{

if (isset($LOCATION_ARRAY[$i]))

{

$distance = 0;

for($i=0; $i<3; $i++) {

$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*

($x_distance[$i] - $x_distance[$i-1])) +

(($y_distance[$i] - $y_distance[$i-1])*

($y_distance[$i] - $y_distance[$i-1])));

}

echo ("$distance");

}

}

?>

The point should be to only work out the distances between the locations that are checked, but at the moment it still works out every location in the array!- So there is something wrong with my code! Except I am not sure what!

Thanks lots for your help :)

problem is you are setting the array every time, what you need to do is build the array based on what they have checked. I took out extra code so only have left what I changed. This is still going to be messy I have some lovely alternatives but want to keep it simple. :)

This is all assuming that based on these values

$x_distance = array ("0", "3", "1", "3");

$y_distance = array ("1", "5", "3", "3");

location1 = 0,1

location2 = 3,5

location3 = 1,3

location4 = 3,3

checkbox.php page

<INPUT TYPE = "checkbox" NAME="loc1" value=1>Location 1<br>

<INPUT TYPE = "checkbox" NAME="loc2" value=2>Location 2<br>

<INPUT TYPE = "checkbox" NAME="loc3" value=3>Location 3<br>

<INPUT TYPE = "checkbox" NAME="loc4" value=4>Location 4<br>

Then the distance.php

$count = 0;

if (isset($_POST['loc1'])) {

$x_distance[$count] = 0;

$y_distance[$count] = 1;

$count++;

}

if (isset($_POST['loc2'])) {

$x_distance[$count] = 3;

$y_distance[$count] = 5;

$count++;

}

if (isset($_POST['loc3'])) {

$x_distance[$count] = 1;

$y_distance[$count] = 3;

$count++;

}

if (isset($_POST['loc4'])) {

$x_distance[$count] = 3;

$y_distance[$count] = 3;

$count++;

}

that adds the location of each point to the array only if they have checked it. I didn't test any of this so it might not be quite right but the premise is sound.

I looked at the loop and couldn't figure it out quite what it was doing.

The thing I couldn't figure was exactly what the array was.

for ($i=0; $i<sizeof($LOCATION_ARRAY); $i++)

{

if (isset($LOCATION_ARRAY[$i]))

{

$distance = 0;

for($i=0; $i<3; $i++) {

$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*

($x_distance[$i] - $x_distance[$i-1])) +

(($y_distance[$i] - $y_distance[$i-1])*

($y_distance[$i] - $y_distance[$i-1])));

}

echo ("$distance");

}

}

you can take the isset if statement out of here and have to change the for statement, I was looking at taking away one of the for's and using the comparison as $i<sizeof($x_distance). Not totally sure though.

<added>oh, and quit apologizing ;)

Basically, the loop works out the distance between two points. It does this by giving each point a coordinate. Then by using pythagorus it works out the distance between them as with the coordinates you can work out two sides and pythagorus works out the remaining side which is the distance! Does this make sense!

I am unsure about your code as I do not have a LOCATION_ARRAY so it will surely not bring anything up?

Cheers :)

my bit just adds the coordinates of the checked location to the x and y arrays, if it isn't checked it isn't there.

Which means that if you use $i<sizeof($x_distance) to figure out how many coordinates are in there it should work. One question, is there a starting point of some kind? A point that should always be there?

What happens if they only selected one point?

<added>I would turn the above function into something like this

$distance = 0;

for ($i=0; $i<sizeof($x_distance); $i++)

{

$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*

($x_distance[$i] - $x_distance[$i-1])) +

(($y_distance[$i] - $y_distance[$i-1])*

($y_distance[$i] - $y_distance[$i-1])));

echo ("$distance");

}

I hadn't even thought about what would happen if there is only one point. I was just trying to get the other stuff working and then worry about that!

I am still unsure which bit of code you think that I should be putting in! Is it possible for you to paste it all together because we have talked about so much stuff I think I may be confusing bits which is why it isn't working!

At the moment I have nothing coming up- I think it is because the array LOCATION_ARRAY doesnt have anything in it

Yeah. Sorry, I didn't scroll far enough. I have tried it all again but still coming out with wrong answer! This is what i have now

<?

$count = 0;

if (isset($_POST['loc1'])) {

$x_distance[$count] = 0;

$y_distance[$count] = 1;

$count++;

}

if (isset($_POST['loc2'])) {

$x_distance[$count] = 3;

$y_distance[$count] = 5;

$count++;

}

if (isset($_POST['loc3'])) {

$x_distance[$count] = 1;

$y_distance[$count] = 3;

$count++;

}

if (isset($_POST['loc4'])) {

$x_distance[$count] = 3;

$y_distance[$count] = 3;

$count++;

}

for ($i=0; $i<3; $i++)

{

if (isset($x_distance[$i]))

{

$distance = 0;

for($i=0; $i<sizeof($x_distance); $i++) {

$distance += sqrt((($x_distance[$i] - $x_distance[$i-1])*

($x_distance[$i] - $x_distance[$i-1])) +

(($y_distance[$i] - $y_distance[$i-1])*

($y_distance[$i] - $y_distance[$i-1])));

}

echo ("$distance");

}

}

?>

what you have to do is understand what the first part is doing and then apply your loop to that.

The first part builds your two x and y arrays depending on what is checked. Therefore the loop has to work no matter how much data is in the arrays. You are having logic problems.

Your equation is right and I think the arrays are being built properly but you need to adapt the logic you were using in the first place to a dynamicly constructed array.

You have to have it understand any possible combination how many values and what they are.

- Facebook Can Now Send You PGP Encrypted Notifications
- Monitoring Bounce Rate from Google Serps
- Google Confirms "Buy Button" To Appear "Imminently" in SERPs
- 2015 Internet Trends
- Twitter Announces Audience Insights Tool For Advertisers
- Resurrecting Old Websites
- Mobile Is Not Destroying Desktop Traffic
- New and Improved WebmasterWorld.com website
- Bing App Indexing SEO
- Yandex Releases Beta Version Of Its Browser, With Privacy Turned On By Default

- May 2015 AdSense Earnings and Observations
- Google Updates and SERP Changes - May 2015
- 2015 Google On-page SEO Ranking Factors List (Including Deprecated Factors)
- June 2015 AdSense Earnings and Observations
- Google Confirms Algo Change: "Quality Update"
- Google Confirms "Buy Button" To Appear "Imminently" in SERPs
- Monitoring Bounce Rate from Google Serps
- Google Updates and SERP Changes - June 2015
- Resurrecting Old Websites
- Facebook Can Now Send You PGP Encrypted Notifications

- Google Updates and SERP Changes - May 2015
- May 2015 AdSense Earnings and Observations
- 2015 Google On-page SEO Ranking Factors List (Including Deprecated Factors)
- Google Confirms Algo Change: "Quality Update"
- Google Confirms "Buy Button" To Appear "Imminently" in SERPs
- Resurrecting Old Websites
- June 2015 AdSense Earnings and Observations
- Monitoring Bounce Rate from Google Serps
- Google Updates and SERP Changes - June 2015
- Mobile Is Not Destroying Desktop Traffic