user2378026
user2378026

Reputation: 19

calculate distance between 2 points using php and mysql

i have a form that allow user to select from the database the city 's name fro the first point and then the city's name for the second point then submit the calculate button

the village table contain:

i did this in my code but it does not show anything and the var_dump show null

can anyone help me ?????

map.php

    <?php 
        if(isset($_POST['calculate']))
        {
            $pt1 = $_POST['pt1'];
            $pt2 = $_POST['pt2'];

            $sql = mysql_query("SELECT longitude, lattitude FROM village WHERE id = '$pt1' AND id = '$pt2'")or die(mysql_error());
            $num_row = mysql_num_rows($sql);
            if($num_row > 0)
            {
                while($row = mysql_fetch_array($sql))
                {
                    $lon_s = $row['longitude'];
                    $lon_e = $row['longitude'];
                    $lat_s = $row['lattitude'];
                    $lat_e = $row['lattitude'];

                    var_dump($lon_e);
                    var_dump($lon_s);
                    var_dump($lat_e);
                    var_dump($lat_s);

                    $R = 6371; //km
                    $A = pow(sin(($lat_e - $lat_s)/2), 2) + cos($lat_s) * cos($lat_e) * pow(sin(($lon_e - $lon_s)/2) , 2);
                    $C = 2 * atan2(sqrt($A), sqrt(1 - $A));
                    $D = $R * $C;

           echo $D;

                }
            }
        }

HTML code

<table width="30%" border="3" align="right">
                <form action="map.php" method="post">
                  <tr>
                    <td width="37%"> Location one: </td>
                    <td width="63%"><select id="location1" name="pt1">
                      <?php echo $opt->Showlocation() ?>
                      </select>
                      <br /></td>
                  </tr>
                  <tr>
                    <td>Location two:</td>
                    <td><select id="location2" name="pt2">
                      <option value="0">choose...</option>
                      </select></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="calculate" value="Calculate Distance" /></td>
                  </tr>
                </form>
              </table>

Upvotes: 1

Views: 280

Answers (1)

Lian
Lian

Reputation: 1629

It's because WHERE id = '$pt1' AND id = '$pt2'

you should have 1 on WHERE clause

WHERE id = '$pt1'

Upvotes: 1

Related Questions