Jump to content

[SOLVED] Getting the highest number from a while loop


JJohnsenDK

Recommended Posts

Hey

 

im trying to get the highest number from this while loop:

 

<?php
$stockQuery = dbquery("SELECT DISTINCT(color_no), products_id, units_out, units_in FROM ".DB_PREFIX."stock WHERE products_id = '".$product."' GROUP BY color_no");
	$i=1;
	while($stockRow = dbarray($stockQuery)){
		$stock = getMaxGarnStock($stockRow['products_id'], $stockRow['color_no']);

		if($stock < $items){
			$_SESSION['item_error'] = "Vi beklager, men vi har desværre kun $stock stk. på lager af den ønskede vare ud af dine $items ønskede stk.. Dette gør, at vi først kan sende det resterende antal når vi får leveret fra vores leverandør. Vil du bestille varen(e) alligevel?";
			$_SESSION['temp_items'] = $items;
			$_SESSION['temp_product'] = $product;
		}
		$i++;
	}
?>

 

The getMaxGarnStock() function returns the amout of items which is left at the stock of the perticuilar product.

 

So if we say the query/while loop gives me these four numbers: 4, 11, 7 and 19

 

How do i get the highest number of these four numbers?

Better to do it within your query if possible but something like....

 

<?php
$max = 0;
while($stockRow = dbarray($stockQuery)){
		$stock = getMaxGarnStock($stockRow['products_id'], $stockRow['color_no']);

		if($stock < $items){
			$_SESSION['item_error'] = "Vi beklager, men vi har desværre kun $stock stk. på lager af den ønskede vare ud af dine $items ønskede stk.. Dette gør, at vi først kan sende det resterende antal når vi får leveret fra vores leverandør. Vil du bestille varen(e) alligevel?";
			$_SESSION['temp_items'] = $items;
			$_SESSION['temp_product'] = $product;
		}
		$i++;
	}
  if ($stock > $max) {
    $max = $stock;
  }

  // $max now holds the highest number.

?>

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.