user2635574
user2635574

Reputation: 305

How do I print out the lowest value from the results of my query?

I have

var_dump($row[Price]);

which prints out all prices from my query ($query = "Select * FROM myTable WHERE...")

like this:

string(5) "37.00" string(5) "20.00" string(5) "23.00" string(5) "12.00" string(5) "10.00"

Now: I would like to print (echo) out just the Lowest Value which is "10.00" in this case.

How do I do this?

Upvotes: 0

Views: 72

Answers (3)

Bora
Bora

Reputation: 10717

while ($row = mysql_fetch_array($result))
{
  // Print out the contents of each row into a table
}

Instead of above codes, use following:

$list = mysql_fetch_array($result);

function _getPrice($array) {
  return $array['Price'];
}

$prices = array_map('_getPrice', $list);

echo min($prices);

Alternative [suggest you]

Or you can get MIN with SQL Query like @Teneff said:

SELECT MIN(price) FROM myTable WHERE...

Upvotes: 2

user12733
user12733

Reputation: 153

You will have to iterate over all strings, convert them to integers and than iterate again to find lowest value. It would be better to use some sort criterion with SQL query and then take first value.

Upvotes: 0

user849001
user849001

Reputation:

You could add ORDER BY to your query, ORDER BY prices ASC. Then print the first element of the array

Upvotes: 0

Related Questions