Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2012, 08:27 PM   PM User | #1
klipsch21
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
klipsch21 is an unknown quantity at this point
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.
klipsch21 is offline   Reply With Quote
Old 05-11-2012, 11:56 PM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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;

jmj001 is offline   Reply With Quote
Old 05-12-2012, 03:00 AM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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';}
mlseim is offline   Reply With Quote
Old 05-12-2012, 08:16 AM   PM User | #4
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
or you could...

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

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 AM.


Advertisement
Log in to turn off these ads.