Jump to content

[SOLVED] Getting the highest number from a while loop


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.

?>

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.