Forum Moderators: coopster
$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!
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.
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.
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 :)
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.
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 ;)
I am unsure about your code as I do not have a LOCATION_ARRAY so it will surely not bring anything up?
Cheers :)
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");
}
<?
$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");
}
}
?>
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.