Thread: PHP coding.

Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2008
    Posts
    23
    Plugin Contributions
    0

    Default PHP coding.

    Hi !

    Anyone to gimme a hand ?

    I'm not used to PHP. Much better at Coldfusion.
    What would be the PHP code for the following ?

    if "X" ≥ 1 and "X" < 3
    set variable "D2"

    if "X" ≥ 3 and "X" < 5
    set variable "D3"

    if "X" ≥ 5 and "X" < 11
    set variable "D4"

    if "X" ≥ 11
    set variable "C1"

    if "X" is undefined
    set variable "C1"

  2. #2
    Join Date
    Apr 2006
    Location
    London, UK
    Posts
    10,569
    Plugin Contributions
    25

    Default Re: PHP coding.

    What do you want to set these variables to?

    What happens is X < 1?

    Is X of type integer or floating point (or something else)?
    Kuroi Web Design and Development | Twitter

    (Questions answered in the forum only - so that any forum member can benefit - not by personal message)

  3. #3
    Join Date
    Apr 2008
    Posts
    23
    Plugin Contributions
    0

    Default Re: PHP coding.

    X would be the number of product in my cart

    if X < 1 then X should be undefined

    I thought about something like that :

    Code:
    <?
    $ X = $_post ['X_from_a_form']
    
    if ( $ X >= 1 && $ X < 3 )
    {
    $ myvar = 'D2' ;
    }
    
    elseif ( $ X >= 3 && $ X < 5 )
    {
    $ myvar = 'D3' ;
    }
    
    elseif ( $ X >=5 && $ X < 11 )
    {
    $ myvar = 'D4' ;
    }
    
    elseif ( $ X >= 11 )
    {
    $ myvar = 'C1' ;
    }
    
    else ( $ X < 1 )
    {
    $ myvar = 'C1';
    }
    
    ?>
    Is it good ?

  4. #4
    Join Date
    Apr 2006
    Location
    London, UK
    Posts
    10,569
    Plugin Contributions
    25

    Default Re: PHP coding.

    This would be more elegant and more robust
    PHP Code:
    <?php
    if (!isset($_POST['X_from_a_form']) || (int)$_POST['X_from_a_form'] < || $_POST['X_from_a_form'] <= 11) {
      
    $myvar 'C1';
    } elseif((int)
    $_POST['X_from_a_form'] < 3) {
      
    $myvar 'D2';
    } elseif((int)
    $_POST['X_from_a_form'] < 5) {
      
    $myvar 'D3'
    } else {
      
    $myvar 'D4';
    }
    ?>
    Depending upon the wider context a switch statement might be more appropriate, though I doubt it in this case
    PHP Code:
    <?php
    if (isset($_POST['X_from_a_form'])) {
      switch ((int)
    $_POST['X_from_a_form']) {
        case 
    1:
        case 
    2$myvar 'D2'; break;
        case 
    3:
        case 
    4$myvar 'D3'; break;
        case 
    5:
        case 
    6:
        case 
    7:
        case 
    8:
        case 
    9:
        case 
    10$myvar 'D4'; break;
        default: 
    $myvar 'C1';
      }
    } else {
      
    $myvar 'C1';
    }
    ?>
    Kuroi Web Design and Development | Twitter

    (Questions answered in the forum only - so that any forum member can benefit - not by personal message)

  5. #5
    Join Date
    Apr 2008
    Posts
    23
    Plugin Contributions
    0

    Default Re: PHP coding.

    thank you kuroi !!

    I'll try these.

  6. #6
    Join Date
    Apr 2006
    Location
    London, UK
    Posts
    10,569
    Plugin Contributions
    25

    Default Re: PHP coding.

    Just spotted a missing (int) in the last condition of the first line in the first example. It's unlikely to be a problem, but hey, why take the risk.
    Kuroi Web Design and Development | Twitter

    (Questions answered in the forum only - so that any forum member can benefit - not by personal message)

 

 

Similar Threads

  1. PHP and other coding...
    By KrawlOff-Road in forum General Questions
    Replies: 5
    Last Post: 22 Oct 2010, 01:53 PM
  2. Genreal PHP Coding Error
    By NBordeau in forum General Questions
    Replies: 0
    Last Post: 1 Sep 2010, 05:57 AM
  3. php coding help please..
    By philip937 in forum General Questions
    Replies: 2
    Last Post: 20 Nov 2009, 11:25 PM
  4. Welcome Emails PHP coding issues...
    By ifdesigns in forum General Questions
    Replies: 2
    Last Post: 20 Nov 2006, 01:08 PM

Posting Permissions

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