Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    If row contains certain value then = a number

    I am a total newbie at php. I have a script I am working with and need to change the way something works.
    Basically I need this script to look at the value of a certain column in a row and depending on what it is turn it into a number value.

    The column I am working with is called 'shippingsvc'. It will have one of these four values in it:
    Standard Shipping
    Local Pickup
    Free Shipping
    Economy Shipping

    So if the value is "Standard Shipping" I would like it to bring back '125'.
    If it is "Local Pickup" = '126'
    If it is "Free" = '127'
    If it is "Economy Shipping" = '128'

    I know this code below will probably not work but it might give you an idea of what I am looking to do.
    if($row['shippingsvc']="Standard Shipping" {$shippingservice='125';}
    if($row['shippingsvc']="Local Pickup" {$shippingservice='126';}
    if($row['shippingsvc']="Free Shipping" {$shippingservice='127';}
    if($row['shippingsvc']="Economy Shipping" {$shippingservice='128';}


    I would appreciate any help.

  2. #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    a switch statement will probably work best here

    http://php.net/manual/en/control-structures.switch.php

    PHP Code:
    switch ($row['shippingsvc']) {
        case 
    "Standard Shipping":
            
    $shippingservice '125';
            break;
        case 
    "Local Pickup":
            
    $shippingservice '126';
            break;
        case 
    "Free Shipping":
            
    $shippingservice '127';
            break;
        case 
    "Economy Shipping":
            
    $shippingservice '128';
            break;


  3. #3
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,031
    Thanks
    10
    Thanked 1,153 Times in 1,144 Posts
    Your "if" statements are perfectly OK, except the equal signs ... make them double ...

    and fix the missing parenthesis ...

    if($row['shippingsvc']=="Standard Shipping") {$shippingservice='125';}
    if($row['shippingsvc']=="Local Pickup") {$shippingservice='126';}
    if($row['shippingsvc']=="Free Shipping") {$shippingservice='127';}
    if($row['shippingsvc']=="Economy Shipping") {$shippingservice='128';}

  4. #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    or you could...

    PHP Code:
    $svcId = array("Standard Shipping"=>"125","Local Pickup"=>"126","Free Shipping"=>"127","Economy Shipping"=>"128");
    $shippingservice $svcId[$row['shippingsvc']]; 


 

Posting Permissions

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