SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 49
  1. #1
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "if statement" question

    I am trying to give a variable a specific value via "if" but are not sure about the syntax. "$variable" can be a number between one and ten. Depending on the number I need to assign a value between 25 and 250 (USD $$$). I am thinking of 10 lines similar to the one below but only get a parse error. How would I need to set this syntax?

    if ($variable == 1) { $variable == 25.00; }

    Thanks for your help!

    Christoph

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    10 lines with if statements?
    No no, switch is the thing you should use

    About the error in
    if ($variable == 1) { $variable == 25.00; }
    If you want to assign the value 25 to $variable, then you should use =, not ==
    Your statement is equal to
    if ($variable == 1) { false; }


    BTW; "...assign a value between 25 and 250..."
    And it isn't this simple formula?
    $variable = 25 * $variable; // 1=>25, 2=>50 ... 10=>250

  3. #3
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help!

    About the last part of your posting - can this set the same way in a "switch" statement or is that "if" specific?

    Christoph

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Christoph
    About the last part of your posting - can this set the same way in a "switch" statement or is that "if" specific?
    Not sure I understood that question?...

  5. #5
    PHP Programmer QReyes's Avatar
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    698
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thnk it can be done with the switch statement. Why do you have the trailing zeroes after 25. Can't it be simply 25?
    The Star Circle Quest Community - Community website for SCQ fans.
    Telebisyon.net - Reference guide to TV series shown in the Philippines.
    Lyrics Server Online! - All the lyrics you can get on one website.

  6. #6
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jofa

    Not sure I understood that question?...
    Sorry, if I was not specific enough. I meant this part:

    $variable = 25 * $variable; // 1=>25, 2=>50 ... 10=>250

    Or is that just how I specify the value of the variable?

    Sorry, I not really knowledgeable with PHP and therefore ask sometimes 'dumb' questions.

    Christoph

  7. #7
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by friend_al_23
    I thnk it can be done with the switch statement. Why do you have the trailing zeroes after 25. Can't it be simply 25?
    It's a dollar amount - that's why I am used to use the zeroes. Will it be a problem if I would use it like this in case I have amounts like $14.75 (just as an example)?

    Christoph

  8. #8
    PHP Programmer QReyes's Avatar
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    698
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Christoph


    It's a dollar amount - that's why I am used to use the zeroes. Will it be a problem if I would use it like this in case I have amounts like $14.75 (just as an example)?

    Christoph
    Sometimes I need to use something like:
    PHP Code:
    val(14.75); 
    The Star Circle Quest Community - Community website for SCQ fans.
    Telebisyon.net - Reference guide to TV series shown in the Philippines.
    Lyrics Server Online! - All the lyrics you can get on one website.

  9. #9
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whether you use if() or switch() is up to you. There are advantages to using either depending upon the situation, though by in large it's simply up to the programmer's preference. As for using the zeroes, as long as you have a period (decimal point) in the value to denote it as a double (non-integer) value, you should be fine.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  10. #10
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by friend_al_23
    Sometimes I need to use something like:
    PHP Code:
    val(14.75); 
    val() doesn't appear to be a valid PHP function....

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  11. #11
    PHP Programmer QReyes's Avatar
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    698
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure? Maybe I was using C or something.
    The Star Circle Quest Community - Community website for SCQ fans.
    Telebisyon.net - Reference guide to TV series shown in the Philippines.
    Lyrics Server Online! - All the lyrics you can get on one website.

  12. #12
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great. I appreciate it.

    Anyway - I got the switch statement partly to work. I started by printing text to see how it works. However - is it possible to set another variable within the switch statement?

    I was trying to set

    PHP Code:
    $variable2 25 



    PHP Code:
    switch ($period) {
        case 
    0:
            print 
    "Please select a registration period!";
            break;
        case 
    1:
            print 
    "Registration fee: $25.00";
        
    $variable2 25;
            break; 
    Christoph

  13. #13
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's possible to do what you're doing, but you need to either escape the $ by placing a \ in front of it or use single quotes instead of double quotes for the String.

    Consequently, what would be most efficient would be something like:
    PHP Code:
    <?php
    switch($period){
         case 
    0:
              print 
    'Please select a registration period!';
              break;
         case 
    1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
              
    $variable2=$period*25.00;
              echo 
    'Registration fee: '.$variable2;
              break;
         default:
              
    // Equivalent to else
              // break; is optional
    }
    ?>


    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  14. #14
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Christoph
    However - is it possible to set another variable within the switch statement?
    Yep, the statement list for a case can be any valid block of php code

    About the "$variable = 25 * $variable;" question you had:
    Maybe more correct: $variable = 25 * $period;
    I was just wondering if the fee you want to calculate can be expressed as fee = $ 25 * period (then you only need an if to check that period isn't 0, and no switch for the cases 1..10)

  15. #15
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just thought of something. When programming Java, one must take into account scope of variables, meaning if a variable is declared inside of a particular function, it "exists" only in that function and any other function in that function, but not outside of the original function. Now, I'm not positive about how this works in PHP, but using the my example above, if you find you cannot access $variable2 outside of the switch() statement, try initializing $variable2 before the switch() statement with something like $variable2=0; and then the above should work because 0 will be overwritten by 25.00 or whatever value.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  16. #16
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Aes
    ... case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: ...
    You can write the case like this (bad coding style?):
    Code:
    switch($i)
    {
      case 0: 
        echo "No, no"; 
        break;
      case $i <= 10: 
        echo "OK"; 
        break;
      default: 
        echo "Ugh?";
    }

  17. #17
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jofa
    ... I was just wondering if the fee you want to calculate can be expressed as fee = $ 25 * period (then you only need an if to check that period isn't 0, and no switch for the cases 1..10)
    But what if the value of $period is more than 10? He needs some default to take that into account, which is what I did above.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  18. #18
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jofa
    You can write the case like this (bad coding style?):
    Code:
    switch($i)
    {
      case 0: 
        echo "No, no"; 
        break;
      case $i <= 10: 
        echo "OK"; 
        break;
      default: 
        echo "Ugh?";
    }
    I wasn't aware that could be done. However, case 1: case 2: ... case 10: is not bad coding style at all. Technically, that's the right way. But if either will work -- programmer's choice.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  19. #19
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone!

    It seems like this will work for me!!!!

    However - I am running into another problem now. This whole thing is for my credit card authorization process. I am calling another file that does the actual processing with authorize.net in the background. It seems like it does not accept the value that we have specified here earlier.

    For testing I am only playing with case 1. The amount is properly displayed on the web page before going to authorize.net.

    PHP Code:
    switch ($period) {
        case 
    0:
            print 
    'Please select a registration period';
            break;
    case 
    1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
              
    $x_Amount2=$period*25.00;
              echo 
    'Registration fee: '.$x_Amount2;
              break;

    In my process file for authorize.net I specify

    $total_cost = $x_Amount2;

    and then later on

    ,"x_Amount" => $total_cost (within an array)

    I did not actually write the process page but it works fine with hardcoded credit card information - so, I know it works. In my case now I get an error message from authorize.net "A valid amount is required" which means that the value I have specified seems to be not transmitted or ignored.

    Any idea why?

    Christoph

  20. #20
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Colin

    In my case $period will never be more than 10. So, for this case your concern should not matter. I just also noticed that the echo command displays the value as "25" and not as "25.00" - the second option looks much more business type of style. Is there another way to display it?

    Christoph

  21. #21
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the processing page equipped to handle the values from an array? We need to know a little more about it. Could you explain it again maybe? Like ... perhaps what variable is supposed to be passed to the processing script?

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  22. #22
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The process script looks like this (this is just the part for the variables)

    PHP Code:
    <?
        
        
    /*
            + + + AUTHORIZATION + + + 
            
            [1] Set Variables
            [2] Seed Data Array For POSTING
            [3] Convert Array to POST string (key_1=val_1&key_2=val_2...)
            [4] Securely POST to AuthorizeNet using "curl"
            [5] Handle Response FROM AuthorizeNet - Set $authorized = TRUE if transaction wass successful
            
            AuthorizeNet Documentation -> [url]http://www.authorizenet.com/support/online_documentation.pdf[/url]
            
        */
        
        // [1] Set Variables
        
        
    $error                       "";
        
    //$test_mode                   = false;
        
    $test_mode                   false;
        
    $authorized                  false;
        
    //$authorized                  = true;
        
    $data                        "";
        
    $response                    "";
        
    $x_Test_Request              "FALSE";
        
    $total_cost                  $x_Amount2;
        
        if (
    $test_mode) { //set above in step [1]
            
    $x_Test_Request              "TRUE";
            
    $credit_card_number          "4007000000027";
            
    $credit_card_expiration_date "12/05";
            
    $total_cost                  "100.00";
        }
        
        
    // [2] Seed Data Array For POSTING
        
    $post_array = array(
                         
    "x_Login"              => "myuserID"
                        
    ,"x_Password"           => "MyPassword"
                        
    ,"x_Type"               => "AUTH_CAPTURE"
                        
    ,"x_Method"             => "CC"
                        
    ,"x_Version"            => "3.1"
                        
    ,"x_Delim_Data"         => "TRUE"
           
    //             ,"x_ADC_URL"          => "FALSE"
                        
    ,"x_Test_Request"       => $x_Test_Request
                        
    ,"x_Amount"             => $total_cost
                        
    ,"x_Card_Num"           => $credit_card_number
                     
    //   ,"x_Exp_Date"         => $credit_card_expiration_date
                         
    ,"x_Exp_Date"           => $expmo.$expyr
                        
    ,"x_Email"               => $email
                        
    // next line manually added by christoph
                        
    ,"x_Card_Code"            => $card_code
                        
    ,"x_Phone"              => $day_phone
                        
    ,"x_First_Name"          => $first_name
                        
    ,"x_Last_Name"          => $last_name
                        
    ,"x_Company"            => $company
                        
    ,"x_Address"            => $address_1
                        
    ,"x_City"               => $city
                        
    ,"x_State"              => $state
                        
    ,"x_Zip"                => $zip
                        
    ,"x_Ship_To_First_Name" => $ship_first_name
                        
    ,"x_Ship_To_Last_Name"  => $ship_last_name
                        
    ,"x_Ship_To_Company"    => $ship_company
                        
    ,"x_Ship_To_Address"    => $ship_address_1
                        
    ,"x_Ship_To_City"       => $ship_city
                        
    ,"x_Ship_To_State"      => $ship_state
                        
    ,"x_Ship_To_Zip"        => $ship_zip
                        
    ,"pc_billing_address_2" => $address_2
                        
    ,"pc_ship_to_address_2" => $ship_address_2
                        
    ,"pc_eve_phone"         => $eve_phone
                        
    ,"pc_password"          => $password
                        
    ,"pc_cart_id"           => $cart_id
                        
    ,"pc_email_list"        => $email_list
                        
    ,"pc_referred_by"       => $referred_by
                    
    );
        
        
    // [3] Convert Array to POST string (key_1=val_1&key_2=val_2...)
        
    reset($post_array);
        while (list (
    $key$val) = each($post_array)) {
            
    $data .= $key "=" urlencode($val) . "&";
        }
    All the other information of my order form has already the correct variables set - matching authorize.net's variables. If I use the hardcoded information above I receive all information I provided in my order form in the conformation email. The switch function in the order form is within the form itself so it should be submitted with the other information.

    Christoph

  23. #23
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Christoph, if that's the case then you can do something like:
    PHP Code:
    <?php
    switch($period){
         case 
    0:
              print 
    'Please select a registration period!';
              break;
         default:
              
    $variable2=$period*25.00;
              echo 
    'Registration fee: '.$variable2;
    }
    ?>
    You say it's not displaying the cost as a double? that's odd.... Let me think.

    Additionally, I thought of something. Remember I said something about scope a moment ago? This could be your problem. Try something like this:
    PHP Code:
    <?php
    $x_Amount2
    =0.00;
    switch(
    $period){
         case 
    0:
              print 
    'Please select a registration period!';
              break;
         default:
              
    $x_Amount2=$period*25.00;
              echo 
    'Registration fee: '.$variable2;
    }
    // then assign $total_cost=$x_Amount2; and see what happens
    ?>
    Any difference?

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  24. #24
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another idea! Are you passing the variable correctly? Newer versions of PHP have register_globals set to off, so you need to pass variables across scripts with either $_GET['variablename'] or $_POST['variablename'] instead of just $variablename. Just another idea.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  25. #25
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I WISH I COULD THINK OF EVERYTHING AT THE SAME TIME.

    I know how to solve your problem with the 25.00 being turned to 25. Use settype() like so:
    PHP Code:
    <?php
    switch($period){
         case 
    0:
              print 
    'Please select a registration period!';
              break;
         default:
              
    $variable2=settype($period,"float")*25.00;
              echo 
    'Registration fee: '.$variable2;
    }
    ?>


    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


Bookmarks

Posting Permissions

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