Jump to content

Finding the highest and lowest number in an array?


merylvingien

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.