A really easy one - I suspect :)

Not really a programmer, but need to change som php code from now to then.

I have a line of php code that shows the stock quantity of a products in my store. This line below shows the quantity directly from the database:

<?php echo (($flag_show_product_info_quantity == 1) ? '
  • ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '
  • ' : '') . "\n"; ?>

    I would like to change the code so that if the stock is more than 20, it says “20+” instead of the real number.

    Someone that can help me with this small problem?

    [php]

    <?php echo (($flag_show_product_info_quantity == 1) ? '
  • ' . ($products_quantity > 20) ? '20+' : $products_quantity . TEXT_PRODUCT_QUANTITY . '
  • ' : '') . "\n"; ?>

    [/php]

    Thanks - sadly it does not work. Just delivers 20+ no matter what and without the text that is supposed to follow (TEXT_PRODUCT_QUANTITY).

    Perhaps it will help, but this is the whole php section:
    [php]

    <?php if ( (($flag_show_product_info_model == 1 and $products_model != '') or ($flag_show_product_info_weight == 1 and $products_weight !=0) or ($flag_show_product_info_quantity == 1) or ($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name))) ) { ?>
      <?php echo (($flag_show_product_info_model == 1 and $products_model !='') ? '
    • ' . TEXT_PRODUCT_MODEL . $products_model . '
    • ' : '') . "\n"; ?> <?php echo (($flag_show_product_info_weight == 1 and $products_weight !=0) ? '
    • ' . TEXT_PRODUCT_WEIGHT . $products_weight . TEXT_PRODUCT_WEIGHT_UNIT . '
    • ' : '') . "\n"; ?> <?php echo (($flag_show_product_info_quantity == 1) ? '
    • ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '
    • ' : '') . "\n"; ?> <?php echo (($flag_show_product_info_quantity == 1) ? '
    • ' . ($products_quantity > 20) ? '20+' : $products_quantity . TEXT_PRODUCT_QUANTITY . '
    • ' : '') . "\n"; ?> <?php echo (($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name)) ? '
    • ' . TEXT_PRODUCT_MANUFACTURER . $manufacturers_name . '
    • ' : '') . "\n"; ?>

    <?php } ?> [/php]

    Try this
    [php]<?php echo (($flag_show_product_info_quantity == 1) ? '

  • ', ($products_quantity > 20) ? '20+' : $products_quantity . TEXT_PRODUCT_QUANTITY , '
  • ' : '') . "\n"; ?>[/php]

    The whole page disappeared :frowning:

    Any further info I can give to help?

    Sorry I 'm not at my development computer so it’s hard to check syntax and functionality. Try this.
    [php]

    <?php echo (($flag_show_product_info_quantity != 1) ? '' : (($products_quantity > 20) ? '
  • 20+'. TEXT_PRODUCT_QUANTITY.'
  • ' : '
  • '.$products_quantity . TEXT_PRODUCT_QUANTITY . '
  • ') . "\n"; ?>

    [/php]

    Really appriciate you spending time on me. This did not help either - the product page still disappears, so something is not as it should be :o

    Finally got to my normal computer. Looks like I missed a closing ). I tested this by setting the values of a couple vars and it works for me.
    [php]echo (($flag_show_product_info_quantity != 1) ? ‘test’ : (($products_quantity > 20) ? ‘

  • 20+’. TEXT_PRODUCT_QUANTITY.’
  • ’ : ‘
  • ’.$products_quantity . TEXT_PRODUCT_QUANTITY . ‘
  • ’) . “\n”);[/php]

    OK - so I added <?php in front of what you pasted in, but it did not work. You have changed the code - added “test”. I guess that should not be there… So I tried to exchange ‘test’ with ‘

  • ’, but still does not work.
  • Actually that “test” should just be empty. Not sure what else to tell ya, it worked for me. Here is what i used for testing. Maybe you need to determine what the actual values of some of your vars are and see where the issue is. Time to start doing some basic diagnostics :wink:
    [php]

    Untitled 1 <?php $flag_show_product_info_quantity = 1; $products_quantity = 21; define('TEXT_PRODUCT_QUANTITY', 'items'); echo (($flag_show_product_info_quantity != 1) ? 'test' : (($products_quantity > 20) ? '
  • 20+'. TEXT_PRODUCT_QUANTITY.'
  • ' : '
  • '.$products_quantity . TEXT_PRODUCT_QUANTITY . '
  • ') . "\n"); ?> [/php]

    Here you see what I get when I run your code unchanged on my server:

    http://altikost.no/test.html

    Your parse it as an html file instead of a php. Change the extension and try it again.

    Ok - I managed to get it to work by applying your method to make my version of the code. After many trial and errors I got it to work. Just don’t as me why this worked and not your code.

    [php]<?php echo (($flag_show_product_info_quantity == 1) ? '

  • ' . (($products_quantity > 20) ? '20+' : $products_quantity) . TEXT_PRODUCT_QUANTITY . '
  • ' : '') . "\n"; ?>[/php]

    Thank you very much again, now for pointing me in the right direction!

    Sponsor our Newsletter | Privacy Policy | Terms of Service