merylvingien Posted October 29, 2010 Share Posted October 29, 2010 Hi folks, i have a complex problem (for me anyway) but i am sure with your help this can be sorted. Problem number 1: I have a simple while loop, but its only showing 4 results when i know there are 5 I am missing something very simple i know: $sql = ("SELECT * FROM postcode WHERE unid='1'")or die ('<p>There seems to be a problem, please try again soon.</p>'); $result = mysql_query($sql,$con); $row = @mysql_fetch_array($result); $i=0; while ($row = @mysql_fetch_assoc($result)){ echo "newpoints[". $i++ ."] = new Array({$row['lat']}, {$row['long']}, icon0, '{$row['postcodename']}', '{$row['postcodename']}'); \n"; } What am i missing here? Problem number 2: I have a bunch of latitude and longitude codes stored in the database. On each page there will be a varied amount of these codes used, but with a max of 20. I need to find the highest number and the lowest number of each lat and long then come up with a code that is in the middle, the idea of this is to center a map. So for example there will be an array like this: 50.852293 -1.76088 51.252938 -0.76128 51.259583 -0.727168 51.274 -0.837 51.123106 -0.970657 First i need to find the highest number of the first code (51.274) then the lowest number (50.852293) and then the highest number of the second code (-1.76088) then the lowest number (-0.727168) Then work out an inbetween code using the highest and lowest codes for each. Does anyone have a clue how to go about this? I am not the worlds best array technition or mathematician Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/ Share on other sites More sharing options...
litebearer Posted October 29, 2010 Share Posted October 29, 2010 since you 'called' row prior to your while loop you may need to reset the pointer Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/#findComment-1127991 Share on other sites More sharing options...
salathe Posted October 29, 2010 Share Posted October 29, 2010 Problem number 1 Remove $row = @mysql_fetch_array($result); Problem number 2 How is your array structured (in PHP code, please)? It's easy to find the max, min and average values from an array. Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/#findComment-1127992 Share on other sites More sharing options...
merylvingien Posted October 29, 2010 Author Share Posted October 29, 2010 Problem number 1 Remove $row = @mysql_fetch_array($result); Problem number 2 How is your array structured (in PHP code, please)? It's easy to find the max, min and average values from an array. Problem 1 sorted, i knew i had overlooked something, but couldnt see the wood for the trees. Problem 2 i am still working on getting the code sorted out, i am researching the max() function at the moment. But i am not the best with arrays. Thanks for your help though, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/#findComment-1127995 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 I have 3 questions - 1) Why do you have an or die() on the end of your $sql = "..." statement? Assigning a string to a variable will always be TRUE and the or die() will never do anything. The or die() should be on the mysql_query() statement. 2) Why do you have $row = @mysql_fetch_array($result); as the 3rd line in the posted code? It is fetching a row from your results set before you start looping over the result set. 3) Why do you have @ in front of the mysql_fetch_array() statements? That just hides errors. You need to know which statements are producing errors so that you can find the problems in your code. Hiding error messages does not fix your code. Your code still won't work AND you won't be getting any errors to help you find where and what the problem is. Don't put any @ in code (ever.) There is really no need. Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/#findComment-1127998 Share on other sites More sharing options...
merylvingien Posted October 29, 2010 Author Share Posted October 29, 2010 I have 3 questions - 1) Why do you have an or die() on the end of your $sql = "..." statement? Assigning a string to a variable will always be TRUE and the or die() will never do anything. The or die() should be on the mysql_query() statement. 2) Why do you have $row = @mysql_fetch_array($result); as the 3rd line in the posted code? It is fetching a row from your results set before you start looping over the result set. 3) Why do you have @ in front of the mysql_fetch_array() statements? That just hides errors. You need to know which statements are producing errors so that you can find the problems in your code. Hiding error messages does not fix your code. Your code still won't work AND you won't be getting any errors to help you find where and what the problem is. Don't put any @ in code (ever.) There is really no need. In answer to all of your questions, bad habits! What i know about php coding is what i have picked up from the internet, never been on courses or read any books, so i have picked up habits and bad coding along the way. But your comments have been taken on board. Quote Link to comment https://forums.phpfreaks.com/topic/217200-finding-the-highest-and-lowest-number-in-an-array/#findComment-1128002 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.