By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,586 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

Postcode Calculator

P: 3
Hi i am writing a balloon race site and i am having problems with some functions to calculate the distance the balloon travels firstly i am reading my info from the database using the fetch assoc function what i have is 2 table one for the race details and one for the postcode details what i am having problems with is listing all the races in the database then having a coloumn at the end which tell you how far the balloon has travelled i have the maths functions and in the race database i also have start and end postcodes what i need to know is how i can select a postcode from the race database based on row id and then match this with the postcode from the postcode database and also get the longitude and latitude details based on the postcode and is needs to do this for each row in the fetch assoc results this is what i have so far but i do know its not working and don't know how to correct it or if it is even possible.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $query = "SELECT * FROM `duchy`";
  3.  
  4. $result = mysql_query($query);
  5.  
  6. if ($result) {
  7.  
  8.     while ($array= mysql_fetch_assoc($result)) {
  9.                 echo $array['startcode']. " - ". $array['finderspostcode']. " - ". $array['ID'];
  10.     echo "<br />";
  11.  
  12.  
  13.     }
  14.  
  15. } else {
  16.  
  17.     print "<li>No results.</li>";
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24. ?>
  25. <?
  26. $sql = 'SELECT DISTINCT `lat` FROM postcode as P,duchy as T WHERE `code` LIKE T.startcode AND T.ID = $array[ID] '; 
  27. $result=mysql_query($sql);
  28. $lat1=mysql_fetch_assoc($result);
  29. $sql2 = 'SELECT DISTINCT `lon` FROM `postcode` WHERE `code` LIKE `$postcode` LIMIT 0 , 30';
  30. $result2=mysql_query($sql2);
  31. $lon1=mysql_result($result2,"lon");
  32. $sql3 = 'SELECT DISTINCT `lat` FROM `postcode` WHERE `code` LIKE `$endcode` LIMIT 0 , 30';
  33. $result3=mysql_query($sql3);
  34. $lat2=mysql_result($result3,"lat");
  35. $sql4 = 'SELECT DISTINCT `lon` FROM `postcode` WHERE `code` LIKE `$endcode` LIMIT 0 , 30';
  36. $result4=mysql_query($sql4);
  37. $lon2=mysql_result($result4,"lon");
  38.  
  39.  
  40. $theta = $lon1 - $lon2; 
  41.   $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  42.   $dist = acos($dist); 
  43.   $dist = rad2deg($dist); 
  44.   $miles = $dist * 60 * 1.1515;
  45.   echo "<td>";
  46.   echo " The value after formating = $miles<br></td></tr>";
  47.  
  48. ?>
  49. <?
  50.  
  51.  
  52.  
  53.  
  54.             echo '</table>'."\n";
  55.  
  56.  
  57.  
  58.  
  59. ?>
any help on this would be most appreciated.
Jul 4 '07 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi, billynastie2007, and welcome to TSDN!

First of all, they put a dot button on your keyboard for a reason! Please use it :)

I'm not sure what data you are working with. Could you show us the layout of those tables your using?
Jul 4 '07 #2

RedSon
Expert 5K+
P: 5,000
Hi, billynastie2007, and welcome to TSDN!

First of all, they put a dot button on your keyboard for a reason! Please use it :)

I'm not sure what data you are working with. Could you show us the layout of those tables your using?
You mean the period button right?
Jul 5 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, billynastie.

What is your code doing? Give an example.
What is your code supposed to be doing? Give an example.
Jul 7 '07 #4

P: 3
Hi
Thankyou for your replies.
What i am doing is writing a simple website for a balloon race and what i am trying to do is run a php maths function on a database query for each row in the database.
I can get it to output the maths function results per row but what i am trying to get it to do is write the results of the maths function back to the database relating to each row.
Heres what i have so far.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $hostname = "localhost"; // The Thinkhost DB server. 
  3. $username = "tech0000_main"; // The username you created for this database. 
  4. $password = "admin"; // The password you created for the username. 
  5. $usertable = "test"; // The name of the table you made. 
  6. $dbName = "tech0000_balloon"; // This is the name of the database you made. 
  7.  
  8. MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
  9. @mysql_select_db( "$dbName") or die( "Unable to select database"); 
  10. ?> 
  11. <?php
  12. // Make a MySQL Connection
  13. $query = "SELECT * FROM duchy"; 
  14.  
  15. $result = mysql_query($query) or die(mysql_error());
  16.  
  17. echo "<table border='3' cellpadding='3' bordercolor='#000000'>\n<tr>\n" .
  18.                "\n\t<th>Distance Covered</th>" .
  19.  
  20.  
  21.           "\n</tr>";
  22. while($row = mysql_fetch_array($result)){
  23. echo "\n<tr>";
  24.  
  25.     $lat1=$row['startlat'];
  26. $lon1=$row['startlon'];
  27. $lat2=$row['endlat'];
  28. $lon2=$row['endlon'];
  29.  
  30. $theta = $lon1 - $lon2; 
  31.   $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  32.   $dist = acos($dist); 
  33.   $dist = rad2deg($dist); 
  34.   $miles = $dist * 60 * 1.1515;
  35.   echo"<td>" .number_format($miles,2);
  36.   ?>
  37.   <?
  38.   echo"- Miles</font></td>";
  39.  
  40.  
  41.     echo "<br />";
  42.  
  43. }
  44.  
  45. ?>
The bit that is underlined is the maths function this calculates the distance between 2 points based on longitude and latitude when this script is run against the database it outputs the mileage based on information in each row but i need to save the results this creates back to the database to create a leaderboard.

Here is the table struture for the mysql database.

-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 22, 2007 at 12:55 AM
-- Server version: 5.0.41
-- PHP Version: 5.2.2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `tech0000_balloon`
--

-- --------------------------------------------------------

--
-- Table structure for table `duchy`
--

CREATE TABLE `duchy` (
`kidsfirstname` varchar(255) collate latin1_general_ci NOT NULL,
`kidslastname` varchar(255) collate latin1_general_ci NOT NULL,
`findersfirstname` varchar(255) collate latin1_general_ci NOT NULL,
`finderslastname` varchar(255) collate latin1_general_ci NOT NULL,
`findersaddress` varchar(255) collate latin1_general_ci NOT NULL,
`finderspostcode` varchar(255) collate latin1_general_ci NOT NULL,
`findersemail` varchar(255) collate latin1_general_ci NOT NULL,
`endlat` varchar(255) collate latin1_general_ci NOT NULL,
`endlon` varchar(255) collate latin1_general_ci NOT NULL,
`endloc` varchar(255) collate latin1_general_ci NOT NULL,
`finderscontact` varchar(255) collate latin1_general_ci NOT NULL,
`startcode` varchar(255) collate latin1_general_ci default 'M6',
`startloc` varchar(255) collate latin1_general_ci NOT NULL default 'Salford',
`startlat` varchar(255) collate latin1_general_ci NOT NULL default '53.492',
`startlon` varchar(255) collate latin1_general_ci NOT NULL default '-2.297',
`miles` varchar(255) collate latin1_general_ci NOT NULL,
`ID` int(25) NOT NULL auto_increment,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=37 ;

--
-- Dumping data for table `duchy`
--

INSERT INTO `duchy` VALUES ('John', 'doe', 'Jane', 'Doe', '10', 'E17', '', '51.586', '-0.019', 'Walthamstow', '0161', 'm6', 'Salford', '53.492', '-2.297', '36.510334425', 23);
INSERT INTO `duchy` VALUES ('Emiy', 'Harrison', '', '', '', 'LS10', '', '53.762', '-1.531', 'Leeds', '', 'm6', 'Salford', '53.492', '-2.297', '36.51', 24);

The full working site can be found here Balloon Race race id is duchy.

Thanks for any help you can assist with its most appreciated.
Jul 21 '07 #5

P: 3
Heya, billynastie.

What is your code doing? Give an example.
What is your code supposed to be doing? Give an example.
Hi Ive re-issued the code and a description for the help i need.
Jul 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.