Jump to content

PHP 101 | If else within if else


dubz99

Recommended Posts

As the topic states, I am obviously a beginner and my question should be an easy one.  I am attempting to have and if/else statement within and if else statement.  Is this possible if so how is it done, below is the code I am working with- it makes sense to me but I am getting errors.  Probably something basic that I am overlooking.

 

TIA

 

dubz

 

                $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
	$stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error());

	$p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . "WHERE " . stock_id . " = " . tep_get_prid($p['id']);
	$p_stock_id_result = mysql_query($p_stock_id_query) or die("<br />". mysql_error());

	if ($stock_id_query = " "){ //Check to see if blank
	if( mysql_num_rows($p_stock_id_result) > 0 ) 
	{
		 $row = mysql_fetch_assoc($p_stock_id_result);

   			$stock_id_display = $row['stock_id'];
		return;
   			
	} else{
  		print (mysql_error());
	}

	} else {


if( mysql_num_rows($stock_id_result) > 0 ) 
	{
		 $row = mysql_fetch_assoc($stock_id_result);

   			$stock_id_display = $row['products_code'];
   			
}
  		 
else
{
   print (mysql_error());
}
}

Link to comment
https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/
Share on other sites

When you're trying to debug mysql errors, it's always helpful to print out the query and the line number where it occured. For example (taken from your code):

<?php
$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
$stock_id_result = mysql_query($stock_id_query) or die("Problem with the query <span style='color:red'>$stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error());
?>

 

Also, your indentations are messed up so it's hard to see which code chunk belongs to which "if" or "while" block. This is also difficult, since you posted only a piece of a larger function.

 

Ken

 

Ken,

 

Understandable.  Like I said beginner. Well I got it to work without the error (missing a space between 'WHERE' and ")but the value is blank? maybe if I explain you could sum it up for me. 

Hopefully I can explain it well.

 

IF $A is blank

Then Do this (second if statement)

Then skip next lines

IF $A is not blank

Then Do this

 

Also is regards to the error the '=1991' is referring to the variable being pulled in this line.

$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);

or

$p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . stock_id . " = " . tep_get_prid($p['id']);

 

as they are the same number.

 

Would elseif work better here?

 

Thanks

dubz

 

 

 

Originally I had the following code which worked, but I need to look in another location if the first place it looks is blank.

 

To answer your question: TABLE_PRODUCTS_STOCK is a table name defined elsewhere and products_id a field within the TABLE.

 

$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
	$stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error());

if( mysql_num_rows($stock_id_result) > 0 ) 
	{
		 $row = mysql_fetch_assoc($stock_id_result);

   			$stock_id_display = $row['products_code'];
   			
}
  		 
else
{
   print (mysql_error());
}

 

dubz

After you do

<?php
$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
?>

echo the query

<?php
$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
echo '<pre>' . $stock_id_query . '</pre>';
?>

and see what's in the query.

Unless TABLE_PRODUCTS_STOCK and products_id are constants, there will be nothing in the query for those strings.

Perhaps you want:

<?php
$stock_id_query = "SELECT products_code FROM " . $TABLE_PRODUCTS_STOCK . " WHERE " . $products_id . " = " . tep_get_prid($p['id']);
?>

 

Ken

 

Ken,

 

OK simple mistake on my end, on Line 3 instead of stock_id it should have said products_id!

 

But now it seems to get stuck in a loop and nothing happening.

 

Here is the code, please feel free to chop it up, as I and many others can learn from your knowledge.

 

Thanks again

dubz

 

 $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
	$stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error());

	$p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
	$p_stock_id_result = mysql_query($p_stock_id_query) or die("<br />". mysql_error());

	if ($stock_id_query = " "){ //Check to see if blank
	if( mysql_num_rows($p_stock_id_result) > 0 ) 
	{
		 $row = mysql_fetch_assoc($p_stock_id_result);

   			$stock_id_display = $row['stock_id'];
		return;
   			
	} else{
  		print (mysql_error());
	}

	} else {


if( mysql_num_rows($stock_id_result) > 0 ) 
	{
		 $row = mysql_fetch_assoc($stock_id_result);

   			$stock_id_display = $row['products_code'];
   			
}
  		 
else
{
   print (mysql_error());
}
}

Hey Thanks for your help!

 

I was able to stop the loop by removing the return; and rather than having a complex if else statement I just made two individual ones.  Also I had to clear out some variables after it ran.

 

Below is the finished working code!

 

//Clear variables
$stock_id_query = "";					
$p_stock_id_query = "";
// Define id for item with attributes	
$stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
$stock_id_result = mysql_query($stock_id_query) or die("Problem with the query <span style='color:red'>$stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error());
//Define if for item without attributes
$p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . products_id . " = " . tep_get_prid($p['id']);
$p_stock_id_result = mysql_query($p_stock_id_query) or die("Problem with the query <span style='color:red'>$p_stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error());


		if( mysql_num_rows($p_stock_id_result) > 0 ) 
		{

			 $row = mysql_fetch_assoc($p_stock_id_result);
  			$stock_id_display_a = $row['stock_id'];
		//return;
   			
		} else {
  			print (mysql_error());
		}

		if( mysql_num_rows($stock_id_result) > 0 ) {

		$row = mysql_fetch_assoc($stock_id_result);
   			$stock_id_display_attr_a = $row['products_code'];
   			
		} else {
		print (mysql_error());

}
$stock_id_display = $stock_id_display_a . $stock_id_display_attr_a;
$stock_id_display_a = "";
$stock_id_display_attr_a = "";

 

Thanks again!

dubz

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.