amg182 Posted June 27, 2011 Share Posted June 27, 2011 Hi guys. I have the following code that works out the distance from the postcode entered into a form and returns the distance in miles to a postcode stored within a database table. It works fine but only finds the distance between the postode entered and the first row of the table(ID 1). The table has approx 10 more rows of data each having there own postcode. So how can i get this script to find the distance between the postcode entered in the form and all the postcodes within the rows of my table? Here is the link to the webpage its on http://cars.netau.net/index.php <form action="php question.php" method="get"> Enter Your Postcode: (i.e. BT22)<input type="text" name="enter" /> <input type="submit" /> </form> Your Postcode: <?php echo $_GET["enter"]; ?>. <br></br> <?php $pcode=$_GET["enter"]; ?> <?php $pcode=$_GET["enter"]; $dbcnx = mysql_connect("host", "user", "pass"); if (!$dbcnx) { echo( " Unable to connect to the " . "database server at this time (this is a host connect problem). " ); exit(); } // Select the database if (! mysql_select_db("db_name", $dbcnx) ) { echo( " Unable to locate the " . "database at this time(this is a dbconnect problem). " ); exit(); } //finds value from database field $from = mysql_query("SELECT * FROM `tbl_name`"); while($row = mysql_fetch_array($from)) { $start=$pcode; $fin=$row['Location']; } { // Postcode enterd via input form $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'"); $row=mysql_fetch_array($result); $gridn[0]=$row['Grid_N']; $gride[0]=$row['Grid_E']; // Postcodes within table $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'"); $row=mysql_fetch_array($result); $gridn[1]=$row['Grid_N']; $gride[1]=$row['Grid_E']; // TAKE GRID REFS FROM EACH OTHER. $distance_n=$gridn[0]-$gridn[1]; $distance_e=$gride[0]-$gride[1]; // CALCULATE THE DISTANCE BETWEEN THE TWO POINTS $hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609; } ?> <table border="1"> <tr> <th>Your Postcode</th> <th>Destination</th> <th>Distance</th> </tr> <tr> <td><?php echo ''.$start.'' ?></td> <td><?php echo ''.$fin.'' ?></td> <td><?php echo ''.round($hypot/1000,2).' Miles';?></td> </tr> </table> Any help would be much appreciated! Quote Link to comment Share on other sites More sharing options...
amg182 Posted June 27, 2011 Author Share Posted June 27, 2011 Anyone :'( Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 27, 2011 Share Posted June 27, 2011 instead of $row=mysql_fetch_array($result); you need to loop though all the results with while($row=mysql_fetch_array($result)){ //... } otherwise you're only using 1 value from the database. Quote Link to comment Share on other sites More sharing options...
amg182 Posted June 28, 2011 Author Share Posted June 28, 2011 Hi thanks for reply. I tried changing this but still only echoes out one row.... here is what I have changed it to as you said. <form action="php question.php" method="get"> Enter Your Postcode: (i.e. BT22)<input type="text" name="enter" /> <input type="submit" /> </form> Your Postcode: <?php echo $_GET["enter"]; ?>. <?php //connect to DB $result = mysql_query("SELECT * FROM `cars`"); while($row = mysql_fetch_array($result)) { $start=$_GET["enter"]; $fin=$row['Location']; } { // Postcode enterd via input form $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'"); while($row=mysql_fetch_array($result)){ $gridn[0]=$row['Grid_N']; $gride[0]=$row['Grid_E']; } // Postcodes within table $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'"); while($row=mysql_fetch_array($result)){ $gridn[1]=$row['Grid_N']; $gride[1]=$row['Grid_E']; } // TAKE GRID REFS FROM EACH OTHER. $distance_n=$gridn[0]-$gridn[1]; $distance_e=$gride[0]-$gride[1]; // CALCULATE THE DISTANCE BETWEEN THE TWO POINTS $hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609; } ?> <table border="1"> <tr> <th>Your Postcode</th> <th>Destination</th> <th>Distance</th> </tr> <tr> <td><?php echo ''.$start.'' ?></td> <td><?php echo ''.$fin.'' ?></td> <td><?php echo ''.round($hypot/1000,2).' Miles';?></td> </tr> </table> Any idea why its only outputing the first row? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 this is untested, just off the top of my head... but try it out. Maybe it'll work... lol <table border="1"> <tr> <th>Your Postcode</th> <th>Destination</th> <th>Distance</th> </tr> <?php $start=$_GET["enter"]; $ends = array(); //finds value from database field $result = mysql_query("SELECT * FROM `cars`"); while($row = mysql_fetch_array($result)){ $ends[]=$row['Location']; } // Postcode enterd via input form $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'"); while($row=mysql_fetch_array($result)){ $gridn[0]=$row['Grid_N']; $gride[0]=$row['Grid_E']; } foreach($ends as $fin){ // Postcodes within table $result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'"); while($row=mysql_fetch_array($result)){ $gridn[1]=$row['Grid_N']; $gride[1]=$row['Grid_E']; } // TAKE GRID REFS FROM EACH OTHER. $distance_n=$gridn[0]-$gridn[1]; $distance_e=$gride[0]-$gride[1]; // CALCULATE THE DISTANCE BETWEEN THE TWO POINTS $hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609; echo '<tr> <td>'.$start.'</td> <td>'.$fin.'</td> <td>'.round($hypot/1000,2).' Miles</td> </tr>'; } ?> </table> Quote Link to comment Share on other sites More sharing options...
amg182 Posted June 28, 2011 Author Share Posted June 28, 2011 Hi, it works, but still only ouptuts one row... I had a look at the database and deleted the row it was using , but it just moves onto the next row.... it seems random what row to use. ? Quote Link to comment Share on other sites More sharing options...
mikosiko Posted June 28, 2011 Share Posted June 28, 2011 could you post the data that you have in your postcodes table? (a few rows should be fine) Quote Link to comment Share on other sites More sharing options...
amg182 Posted June 28, 2011 Author Share Posted June 28, 2011 Postcode Table: "Postcode_ID" "PCode" Grid_N Grid_E Latitude Longitude 1 AB10 392900 804900 57.135 -2.117 2 AB11 394500 805300 573138 -2.092 3 AB12 393300 801100 57.101 -2.111 4 AB13 385600 801900 57.108 -2.237 5 AB14 383600 801100 57.101 -2.27 Location Table: "ID" "Location" 1 ME14 2 ME16 3 L7 4 TN24 5 ME23 Thanks, Quote Link to comment Share on other sites More sharing options...
amg182 Posted June 28, 2011 Author Share Posted June 28, 2011 Opps Never mind, numbnuts here didnt inc database connect.... Thanks ever so much guys, true gentlemen! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.