#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    PHP Query not quite working


    Hi and apologies for what may be a silly question.

    I have a small query in my web page to pull through the current stock level and display a message according to the level available.

    However for stock level of nil/zero the message should be "Currently Out of Stock - Orders received will be placed on backorder", but instead the message displayed is "Good Stock Levels available".

    Please would someone point me in the right direction?

    PHP Code:
    $qty tep_get_products_stock($product_info['products_id']);
    switch (
    $qty) {
    case (
    $qty 10): echo "Good Stock Levels available"; break;
    case (
    $qty 5): echo "Low Stock Levels"; break;
    case (
    $qty 0): echo "Very Low Stock Levels"; break;
    default: echo 
    "Currently Out of Stock - Orders received will be placed on backorder";

    Many thanks in advance.
    Paul
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    PHP Code:
    switch ($qty) { 
    case (
    $qty 10): 
    Can't do that. What you've told PHP to do is compare the value of $qty with the result of ($qty>10).

    You could use a switch for this, but use a regular if instead.
    PHP Code:
    if ($qty 10) { 
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0
    Thank you for the reply and apologies for my ignorance but I don't fully understand. Please would you be kind enough to explain or demonstrate?

    Many thanks once again.
    Paul
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    Use an if/elseif block instead of a switch block.
    PHP Code:
    if ($qty 10) { ... }
    else if (
    $qty 5) { ... }
    else if (
    $qty 0) { ... }
    else { ... } 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0
    Thank you so much for the reply.

    I installed the code below based on your kind suggestion but it resulted in a blank page.

    PHP Code:
    <td align="left" class="stocklevels">
    <?php
    $qty 
    tep_get_products_stock($product_info['products_id']);
    if (
    $qty 10) {Good Stock Levels available
    else if (
    $qty 5) {Low Stock Levels
    else if (
    $qty 0) {Very Low Stock Levels
    else {
    Currently Out of Stock Orders received will be placed on backorder}
    ?>
    </td>
    I'm sure I have done something stupid (although did try with and without speech marks / quotes but had the same result of a blank page?

    Any help would be much appreciated.

    Paul
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0
    Doh, At last the penny drops and I've got it!

    Correct code below and works like a charm.. Thank you again.

    PHP Code:
    <?php
    $qty 
    tep_get_products_stock($product_info['products_id']);
    if (
    $qty 10) echo "Good Stock Levels available";
    else if (
    $qty 5) echo "Low Stock Levels";
    else if (
    $qty 0) echo "Very Low Stock Levels"
    else echo 
    "Currently Out of Stock - Orders received will be placed on backorder"
    ?>
    Paul

IMN logo majestic logo threadwatch logo seochat tools logo