...

View Full Version : else if racking my brain



douglasredvers
02-28-2012, 08:59 AM
Hi I have 2 pieces of code one works as it should the other doesn't but I cant understand why.
I expect the answer is very obvious but I just cant work it out

Here is the one that works.
The code displays little images depending on stock level.


<?php
$stock_level = tep_get_products_stock($products_new['products_id']);
if ((STOCK_CHECK == "true")&&($stock_level < 1)) {
echo '<img src="images/outstock.gif" alt="Out of Stock" width="30" height="30" style="margin-left:10px;margin-right:5px;">';
} elseif ((STOCK_CHECK == 'true')&&($stock_level > 3)) {
echo '<img src="images/instock.gif" alt="In Stock" width="30" height="30" style="margin-left:10px;margin-right:5px;">';
} elseif ((STOCK_CHECK == 'true')&&($stock_level == 3)) {
echo '<img src="images/threestock.gif" alt="Three in Stock" width="30" height="30" style="margin-left:10px;margin-right:5px;">';
} elseif ((STOCK_CHECK == 'true')&&($stock_level == 2)) {
echo '<img src="images/twostock.gif" alt="Two in Stock" width="30" height="30" style="margin-left:10px;margin-right:5px;">';

} else {
echo'<img src="images/fewleft.gif" alt="Only One Left" width="30" height="30"style="margin-left:10px;margin-right:5px;">';
}
?>

and here is the one that doesn't


//bof stock announcement
case 'PRODUCT_LIST_QUANTITY':
$lc_align = 'left';
if ((STOCK_CHECK == 'true')&&($listing['products_quantity'] < 1)) {
$lc_text = '<img src="images/outstock.gif" alt="Out of Stock" width="30" height="30" ">';
}
elseif ((STOCK_CHECK == 'true')&&($stock_level == 3)) {
$lc_text = '<img src="images/threestock.gif">';
}
elseif ((STOCK_CHECK == 'true')&&($stock_level == 2)) {
$lc_text = '<img src="images/twostock.gif">';

}
elseif ((STOCK_CHECK == 'true')&&($listing['products_quantity'] > 3)) {
$lc_text = '<img src="images/instock.gif" alt="In Stock" width="30" height="30">';
}
else {
$lc_text = '<img src="images/fewleft.gif" alt="Only One Left" width="30" height="30">';
}

break;
//eof stock announcement

Many thanks for your input

douglasredvers
02-28-2012, 09:08 AM
Hi
I answered my own question when i saw it all laid out
I needed to use $listing instead of $stock level
DOH

However is there a better more economical way to write these statements

Doug

tangoforce
02-28-2012, 10:20 AM
Hi
However is there a better more economical way to write these statements


Yes.

You're already inside one switch so you clearly know how to use them - so why on earh are you using lots of elseif's ? - Just use another switch.

douglasredvers
02-28-2012, 12:18 PM
Yes.

You're already inside one switch so you clearly know how to use them - so why on earh are you using lots of elseif's ? - Just use another switch.

Hi there
it is from code I have copied and altered so i don't know how to use switch statements yet.
Any clues greatly appreciated.
Is a switch better than elseif?
Doug

douglasredvers
02-29-2012, 10:25 AM
Hi took your advice and looked at switches
My question is how do I combine all the numbers with a - before them


function avail_products_stock($pID) {
switch(tep_get_products_stock($pID)) {

case 0:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -1:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -2:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -3:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -4:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -5:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -6:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -7:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;
case -8:
$in_stock = tep_image(DIR_WS_IMAGES . 'outstockbig.gif');
break;

case 2:
$in_stock = tep_image(DIR_WS_IMAGES . 'twostock.gif');
break;
case 1:
$in_stock = tep_image(DIR_WS_IMAGES . 'fewleft.gif');
break;


default:
$in_stock = tep_image(DIR_WS_IMAGES . 'instockbig.gif');
}
return $in_stock;
}

tangoforce
02-29-2012, 03:59 PM
case -1:
case -2:
case -3:
//Do something
break;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum