Tahseen Khan
Tahseen Khan

Reputation: 19

get lowest price using php

Product |  bprice  |    fprice
abcd    |  89      |    65
ebcd    |  39      |    105
fbcd    |  23      |    45
gbcd    |  89      |    
hbcd    |  89      |    65
ibcd    |          |    65
jbcd    |  50      |    50

sql+php

how to  get low price of each product using php script after fetching mysql records like

for abcd $price = 65
for fbed $price = 23
for gbcd  $price = 89
etc

Upvotes: 2

Views: 375

Answers (3)

Keith Irwin
Keith Irwin

Reputation: 5668

min($bprice,$fprice) should do what you need.


Edit: Fix to handle null

if (is_null($bprice)) {
    $price = $fprice;
} elseif (is_null($fprice)) {
    $price = $bprice;
} else {
    $price = min($bprice,$fprice);
}

Upvotes: 5

Rohan Prabhu
Rohan Prabhu

Reputation: 7302

Use the group by statement:

SELECT MAX(bprice) AS maxbprice, MAX(fprice) AS maxfprice
FROM products
GROUP BY Product

Upvotes: 3

Vaishu
Vaishu

Reputation: 2363

$query = "SELECT * FROM product group by product";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
   if($row->bprice > $row->fprice)
   {
       $low=$row->fprice;
   } 
    else
   {
   $low=$row->bprice;
    }
}

Upvotes: 1

Related Questions