Jump to content

IF ELSE problem


chet139

Recommended Posts

I have the following code which works as desired.

 

$sql="select * from product";
$result = @mysql_query($sql);//runs query
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
		$productId = $row['productId'];
		$qty = $row['stockQuantity'];
		$reorder = $row['reOrderlevel'];
		$make = $row['productMake'];
		$model = $row['productModel'];

		if ($qty <= $reorder)
		{
			echo '<blink><b><font color="red">Stock Alert</font></b></blink><br/>';
			echo '**The stock level for Product <b>[' .$productId . '] '.$make.' '.$model.'</b> is low. A reorder maybe required.**<br/><br/>';
		}	



}

 

It basically gets information from product table compares the variable and echos if required.

 

What I tried to do with the bit of modified code below is is all stock issues are ok (ie if no current stock levels are = to or < then minimum amount then output OK.

 

$sql="select * from product";
$result = @mysql_query($sql);//runs query
$stockProblem = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
		$productId = $row['productId'];
		$qty = $row['stockQuantity'];
		$reorder = $row['reOrderlevel'];
		$make = $row['productMake'];
		$model = $row['productModel'];

		if ($qty <= $reorder)
		{
			$stockProblem= true;
			echo '<blink><b><font color="red">Stock Alert</font></b></blink><br/>';
			echo '**The stock level for Product <b>[' .$productId . '] '.$make.' '.$model.'</b> is low. A reorder maybe required.**<br/><br/>';
		}	



}
if ($stockProblem = false)
{
	echo"Stock Levels OK";
}

 

But if there are no problems ie stockProblem = false, then it does not output anything... any idea whats wrong...maybe its my logic??

 

i also tried if ($stockProblem == false) - then it just displays all the time...

Link to comment
Share on other sites

First of all, = is different than ==. The = operator, is the assignment operator and you use it like this:

 

$variable = "value";

 

the == is a comparison operator and it is used for checking/comparing variables. So your if statement should be:

 

if( $stocbProblem == false ){ //rest }

 

Then, if it runs everytime, try outputting the $reorder and $qty values to check whether $stockProblem is actually set to true.

Link to comment
Share on other sites

I was close here working code:

 

$stockProblem = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
		$productId = $row['productId'];
		$qty = $row['stockQuantity'];
		$reorder = $row['reOrderlevel'];
		$make = $row['productMake'];
		$model = $row['productModel'];

		if ($qty <= $reorder)
		{
			$stockProblem= true;
			echo '<blink><b><font color="red">Stock Alert</font></b></blink><br/>';
			echo '**The stock level for Product <b>[' .$productId . '] '.$make.' '.$model.'</b> is low. A reorder maybe required.**<br/><br/>';
		}	



}
if ($stockProblem == false)
{
	echo"Stock Levels OK";
}

 

[sOLVED] Thanks!

Link to comment
Share on other sites

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.