Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Hi everyone,

    I'm befuddled.

    I can't figure out why I can't use multiple if statements in a function. This is not my work, but I am troubleshooting someone else's. What seems to happen is that the function only regards the 1st if-statement. So, even if all 3 if-statements are true ($discount should == 30), it will only be == 10.

    Here's what I've got:

    function discount_due($data) {
    $discount = 0;
    if ($data['stepup'] == 'yes') {
    if (in_array($data['grad'], array(2013, 2014, 2015, 2016))) {
    $discount += 10;
    }
    }

    if ($data['military'] == 'yes') {
    if (!empty($data['installation']) && !empty($data['jobTitle']) && !empty($data['rank'])) {
    $discount += 10;
    }
    }

    if ($data['imcea'] == 'yes') {
    if (!empty($data['imceaNumber'])) {
    $discount += 10;
    }

    }


    return $discount;
    }

    This value is called in this manner:

    <?php print $data['discount']; ?>

    I feel like the answer should be so obvious... but I don't really know how to search for the answer via Google. Also, if no one can answer this, I might just scrap this and work in my own variables using $_POST, which is something I understand.

    -R

  2.  

  3. #2
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    The issue isn't the if statements, it's what's in the data array being passed.

    Take this for example, the result should equal 3 and this is using your function without any modification.

    PHP Code:
    <?php

    /**
    * Returns how much discount is due
    */
    function discount_due($data) {

        
    $discount 0;

        if (
    $data['stepup'] == 'yes') {
            if (
    in_array($data['grad'], array(2013201420152016))) {
                
    $discount += 10;
            }
        }

        if (
    $data['military'] == 'yes') {
            if (!empty(
    $data['installation']) && !empty($data['jobTitle']) && !empty($data['rank'])) {
                
    $discount += 10;
            }
        }

        if (
    $data['imcea'] == 'yes') {
            if (!empty(
    $data['imceaNumber'])) {
                
    $discount += 10;
            }
        }

        return 
    $discount;
    }

    //Example input data
    $data = array(
            
    'stepup'      => 'yes',
            
    'grad'        => 2013,

            
    'military'    => 'yes',
            
    'installation'=> 'something',
            
    'jobTitle'    => 'something',
            
    'rank'        => 'something',

            
    'imcea'       => 'yes',
            
    'imceaNumber' => 123
        
    );

    //Grab the discount
    $discount discount_due($data);

    ?>

    <h1><?php echo $discount?></h1>
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  4. #3
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Ganners,

    You've saved me again. I think I should stop accepting jobs meant for developers -> :nerd:. SO frustrating asldkjfalsdjflasdflasdfj;asdfjaljfd.

    I thought this work I inherited was ridiculous because it's complicated (relative to what it needs to be for simple data sent through a form) and yet, the guy who made it wasn't able to get it to work properly, calculation-wise, or stylize the submission email (he's just dumping array data into printr).

    -R


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:31 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com