Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question else if racking my brain

    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.
    Code:
     
    <?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

    Code:
    //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

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by douglasredvers View Post
    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.
    I can't really think of anything to write here now...

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    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

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi took your advice and looked at switches
    My question is how do I combine all the numbers with a - before them

    Code:
    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;
          }

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    case -1:
    case -2:
    case -3:
    //Do something
    break;
    I can't really think of anything to write here now...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •