Jump to content

find the min price from database array


AdRock

Recommended Posts

I have a list of products in database and some have the same itemid but a different price depending on size

 

What i want to do i if the itemid has more than one size, take the smallest price and then add the word "From" at the beginning.

 

I've tried using the MIN() function but can't get it to do what i want.

 

If there is only one size it works fine but if there is more than one it just says "From 0.00"

 

while ($row = $result->fetch()) {
if(!isset($items[$row['itemid']]))
{
	$items[$row['itemid']] = $row;
}
$items[$row['itemid']]['sizes'][$row['size']] = $row['price'];  ### CHANGED ###
}
foreach($items AS $subitem) {
list($prodid, $itemid, $item, $size, $description, $shortdesc, $image, $price) = $subitem;

//Create field for item name based upon record count	
if(count($subitem['sizes']) > 1)
{
	/*$item_name_field = "<ul>\n";
	foreach($subitem['sizes'] as $size => $price) ### CHANGED ###
	{*/

	//}
	//$item_name_field .= "<ul>\n";
	$myprice = explode(".",number_format(min($subitem['price']),2));
	$item_name_field .= "<li>From ".$myprice[0].".".$myprice[1]." {$size}</li>\n";
}
else
{
	$myprice = explode(".",number_format($subitem['price'],2));
	$item_name_field .= "<li><span class=\"ucprice\">&#163;".$myprice[0]."<?span><span class=\"lcprice\">.".$myprice[1]."</span></li>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/249587-find-the-min-price-from-database-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.